//莫队模板题
//区间内不同数的个数:https://vjudge.net/problem/SPOJ-DQUERY
#include<bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10;
int s[M],num[M],vis[M],block,ans,m;
struct node{
int l,r,id;
bool operator < (const node &cmp) const{
if(l/block == cmp.l/block) return r < cmp.r;
return l/block < cmp.l/block;
}
}q[M];
void add(int x){
vis[s[x]]++;
if(vis[s[x]] == 1) ans++;
}
void del(int x){
vis[s[x]]--;
if(vis[s[x]] == 0) ans--;
}
int main() {
// freopen("a.txt","r",stdin);
// ios::sync_with_stdio(0);
int n;
scanf("%d",&n);
block = sqrt(n);
for(int i = 1;i <= n;i ++) scanf("%d",&s[i]);
scanf("%d",&m);
for(int i = 1;i <= m;i ++){
cin>>q[i].l>>q[i].r;
q[i].id = i;
}
sort(q+1,q+m+1);
int l = 1,r = 0;
for(int i = 1;i <= m;i ++){
while(r < q[i].r) r++,add(r);
while(r > q[i].r) del(r),r--;
while(l < q[i].l) del(l),l++;
while(l > q[i].l) l--,add(l);
num[q[i].id] = ans;
}
for(int i = 1;i <= m;i ++) printf("%d\n",num[i]);
return 0;
}