#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MAXN = 3e4 + 5;
const int MAXM = 2e5 + 5;
const int MAXS = 1e6 + 5;
const int SZ = 200;
struct Q{
int x, y, id;
Q(int _x = 0, int _y = 0, int _id = 0): x(_x), y(_y), id(_id) {}
bool operator < (const Q &rhs) const
{
return y < rhs.y;
}
};
int n,m;
vector< vector<Q> > bckt;
int a[MAXN];
int ans[MAXM];
int cnt[MAXS];
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
bckt = vector< vector<Q> >(n / SZ + 1);
scanf("%d", &m);
for(int i = 0; i < m; ++i){
int x, y;
scanf("%d%d", &x, &y);
x--; y--;
bckt[x / SZ].push_back(Q(x, y, i));
}
for(auto& c: bckt) sort(c.begin(), c.end());
for(int i = 0; i < bckt.size(); ++i){
int l = i * SZ;
int r = l - 1;
int cur = 0;
for(auto &q: bckt[i]){
while(r < q.y){
++r;
cnt[a[r]]++;
if(cnt[a[r]] == 1){
++cur;
}
}
while(l < q.x){
cnt[a[l]]--;
if(cnt[a[l]] == 0){
--cur;
}
++l;
}
while(l > q.x){
--l;
cnt[a[l]]++;
if(cnt[a[l]] == 1){
++cur;
}
}
ans[q.id] = cur;
}
while(l <= r){
cnt[a[l]]--;
if(cnt[a[l]] == 0){
--cur;
}
++l;
}
}
for(int i = 0; i < m; ++i){
printf("%d\n", ans[i]);
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsZDsKY29uc3QgaW50IE1BWE4gPSAzZTQgKyA1Owpjb25zdCBpbnQgTUFYTSA9IDJlNSArIDU7CmNvbnN0IGludCBNQVhTID0gMWU2ICsgNTsKY29uc3QgaW50IFNaID0gMjAwOwoKc3RydWN0IFF7CglpbnQgeCwgeSwgaWQ7CglRKGludCBfeCA9IDAsIGludCBfeSA9IDAsIGludCBfaWQgPSAwKTogeChfe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