#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int anss = 0, ans[200005], a[30005], bs, frq[1000005];
struct st
{
int l, r, t;
};
bool cmp(const st &ss, const st &sk)
{
int x_block = ss.l, y_block = sk.l;
if (x_block == y_block)
{
if (x_block & 1)
return ss.r > sk.r;
return ss.r < sk.r;
}
else
return x_block < y_block;
}
void add(int x)
{
frq[a[x]]++;
if (frq[a[x]] == 1)
anss++;
}
void del(int x)
{
frq[a[x]]--;
if (frq[a[x]] == 0)
anss--;
}
int main()
{
int i, n, ll, rr, j, k, l, q, lt = 1, rt = 0;
cin >> n;
bs = (int)sqrt(n);
for (i = 1; i <= n; i++)
{
cin >> a[i];
}
cin >> q;
st b[200005];
for (i = 0; i < q; i++)
{
cin >> b[i].l >> b[i].r;
b[i].t = i;
}
sort(b, b + q, cmp);
for (i = 0; i < q; i++)
{
ll = b[i].l, rr = b[i].r;
while (rr > rt)
{
add(++rt);
}
while (ll < lt)
{
add(--lt);
}
while (ll > lt)
{
del(lt++);
}
while (rr < rt)
{
del(rt--);
}
ans[b[i].t] = anss;
}
for (i = 0; i < q; i++)
cout << ans[i] << "\n";
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBhbnNzID0gMCwgYW5zWzIwMDAwNV0sIGFbMzAwMDVdLCBicywgZnJxWzEwMDAwMDVdOwpzdHJ1Y3Qgc3QKewogICAgaW50IGwsIHIsIHQ7Cn07CmJvb2wgY21wKGNvbnN0IHN0ICZzcywgY29uc3Qgc3QgJnNrKQp7CiAgICBpbnQgeF9ibG9jayA9IHNzLmwsIHlfYmxvY2sgPSBzay5sOwogICAgaWYgKHhfYmxvY2sgPT0geV9ibG9jaykKICAgIHsgICAKICAgICAgICBpZiAoeF9ibG9jayAmIDEpCiAgICAgICAgICAgIHJldHVybiBzcy5yID4gc2sucjsKCiAgICAgICAgcmV0dXJuIHNzLnIgPCBzay5yOwogICAgfQogICAgZWxzZQogICAgICAgIHJldHVybiB4X2Jsb2NrIDwgeV9ibG9jazsKfQp2b2lkIGFkZChpbnQgeCkKewogICAgZnJxW2FbeF1dKys7CiAgICBpZiAoZnJxW2FbeF1dID09IDEpCiAgICAgICAgYW5zcysrOwp9CnZvaWQgZGVsKGludCB4KQp7CiAgICBmcnFbYVt4XV0tLTsKICAgIGlmIChmcnFbYVt4XV0gPT0gMCkKICAgICAgICBhbnNzLS07Cn0KCmludCBtYWluKCkKewogICAgaW50IGksIG4sIGxsLCByciwgaiwgaywgbCwgcSwgbHQgPSAxLCBydCA9IDA7CiAgICBjaW4gPj4gbjsKICAgIGJzID0gKGludClzcXJ0KG4pOwogICAgZm9yIChpID0gMTsgaSA8PSBuOyBpKyspCiAgICB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICB9CiAgICBjaW4gPj4gcTsKICAgIAogICAgc3QgYlsyMDAwMDVdOwoKICAgIGZvciAoaSA9IDA7IGkgPCBxOyBpKyspCiAgICB7CiAgICAgICAgICAgIGNpbiA+PiBiW2ldLmwgPj4gYltpXS5yOwoKICAgICAgICAgICAgYltpXS50ID0gaTsKICAgIH0KCiAgICBzb3J0KGIsIGIgKyBxLCBjbXApOwogICAgZm9yIChpID0gMDsgaSA8IHE7IGkrKykKICAgIHsKICAgICAgICBsbCA9IGJbaV0ubCwgcnIgPSBiW2ldLnI7CgogICAgICAgIHdoaWxlIChyciA+IHJ0KQogICAgICAgIHsKICAgICAgICAgICAgYWRkKCsrcnQpOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobGwgPCBsdCkKICAgICAgICB7CiAgICAgICAgICAgIGFkZCgtLWx0KTsKICAgICAgICB9CgogICAgICAgIHdoaWxlIChsbCA+IGx0KQogICAgICAgIHsKICAgICAgICAgICAgZGVsKGx0KyspOwogICAgICAgIH0KICAgICAgICB3aGlsZSAocnIgPCBydCkKICAgICAgICB7CiAgICAgICAgICAgIGRlbChydC0tKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgYW5zW2JbaV0udF0gPSBhbnNzOwogICAgfQogICAgZm9yIChpID0gMDsgaSA8IHE7IGkrKykKICAgICAgICBjb3V0IDw8IGFuc1tpXSA8PCAiXG4iOwp9