#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fc first
#define sc second
#define endl '\n'
#define ll long long
#define forn(i, n) for(int i = 0; i < (int) (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int MAXN = (int) 3e5 + 7, MAXK = (int) 1e6 + 7;
const int MAXQ = (int) 2e5 + 7;
int ans[MAXN], a[MAXN], cnt[MAXK];
const int SZ = 555;
struct ev {
int i, l, r;
ev() {}
};
ev q[MAXQ];
bool cmp(ev a, ev b) {
if (a.l / SZ != b.l / SZ) {
return a.l / SZ < b.l / SZ;
} else {
return a.r < b.r;
}
}
int cur_ans = 0;
void add(int x) {
cnt[x]++;
if (cnt[x] == 1) {
cur_ans++;
}
}
void rem(int x) {
cnt[x]--;
if (cnt[x] == 0) {
cur_ans--;
}
}
int main() {
int n, m, l, r, nl, nr;
scanf("%d", &n);
forn (i, n) {
scanf("%d", &a[i]);
}
scanf("%d", &m);
forn (i, m) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].i = i;
}
sort(q, q + m, cmp);
l = r = 0;
add(a[0]);
forn (i, m) {
nl = q[i].l, nr = q[i].r;
nl--, nr--;
while (nl < l) {
l--;
add(a[l]);
}
while (nr > r) {
r++;
add(a[r]);
}
while (nl > l) {
rem(a[l]);
l++;
}
while (nr < r) {
rem(a[r]);
r--;
}
ans[q[i].i] = cur_ans;
}
forn (i, m) {
printf("%d\n", ans[i]);
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBmYyBmaXJzdAojZGVmaW5lIHNjIHNlY29uZAojZGVmaW5lIGVuZGwgJ1xuJwojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGZvcm4oaSwgbikgZm9yKGludCBpID0gMDsgaSA8IChpbnQpIChuKTsgaSsrKQojZGVmaW5lIGFsbCh4KSB4LmJlZ2luKCksIHguZW5kKCkKI2RlZmluZSByYWxsKHgpIHgucmJlZ2luKCksIHgucmVuZCgpCgpjb25zdCBpbnQgTUFYTiA9IChpbnQpIDNlNSArIDcsIE1BWEsgPSAoaW50KSAxZTYgKyA3Owpjb25zdCBpbnQgTUFYUSA9IChpbnQpIDJlNSArIDc7CmludCBhbnNbTUFYTl0sIGFbTUFYTl0sIGNudFtNQVhLXTsKY29uc3QgaW50IFNaID0gNTU1OwoKc3RydWN0IGV2IHsKICAgIGludCBpLCBsLCByOwogICAgZXYoKSB7fQp9OwoKZXYgcVtNQVhRXTsKCmJvb2wgY21wKGV2IGEsIGV2IGIpIHsKICAgIGlmIChhLmwgLyBTWiAhPSBiLmwgLyBTWikgewogICAgICAgIHJldHVybiBhLmwgLyBTWiA8IGIubCAvIFNaOwogICAgfSBlbHNlIHsKICAgICAgICByZXR1cm4gYS5yIDwgYi5yOwogICAgfQp9CgppbnQgY3VyX2FucyA9IDA7Cgp2b2lkIGFkZChpbnQgeCkgewogICAgY250W3hdKys7CiAgICBpZiAoY250W3hdID09IDEpIHsKICAgICAgICBjdXJfYW5zKys7CiAgICB9Cn0KCnZvaWQgcmVtKGludCB4KSB7CiAgICBjbnRbeF0tLTsKICAgIGlmIChjbnRbeF0gPT0gMCkgewogICAgICAgIGN1cl9hbnMtLTsKICAgIH0KfQoKCmludCBtYWluKCkgewogICAgaW50IG4sIG0sIGwsIHIsIG5sLCBucjsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGZvcm4gKGksIG4pIHsKICAgICAgICBzY2FuZigiJWQiLCAmYVtpXSk7CiAgICB9CiAgICBzY2FuZigiJWQiLCAmbSk7CiAgICBmb3JuIChpLCBtKSB7CiAgICAgICAgc2NhbmYoIiVkJWQiLCAmcVtpXS5sLCAmcVtpXS5yKTsKICAgICAgICBxW2ldLmkgPSBpOwogICAgfQogICAgc29ydChxLCBxICsgbSwgY21wKTsKICAgIGwgPSByID0gMDsKICAgIGFkZChhWzBdKTsKICAgIGZvcm4gKGksIG0pIHsKICAgICAgICBubCA9IHFbaV0ubCwgbnIgPSBxW2ldLnI7CiAgICAgICAgbmwtLSwgbnItLTsKICAgICAgICB3aGlsZSAobmwgPCBsKSB7CiAgICAgICAgICAgIGwtLTsKICAgICAgICAgICAgYWRkKGFbbF0pOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobnIgPiByKSB7CiAgICAgICAgICAgIHIrKzsKICAgICAgICAgICAgYWRkKGFbcl0pOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobmwgPiBsKSB7CiAgICAgICAgICAgIHJlbShhW2xdKTsKICAgICAgICAgICAgbCsrOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobnIgPCByKSB7CiAgICAgICAgICAgIHJlbShhW3JdKTsKICAgICAgICAgICAgci0tOwogICAgICAgIH0KICAgICAgICBhbnNbcVtpXS5pXSA9IGN1cl9hbnM7CiAgICB9CiAgICBmb3JuIChpLCBtKSB7CiAgICAgICAgcHJpbnRmKCIlZFxuIiwgYW5zW2ldKTsKICAgIH0KfQo=