#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 1000005
namespace IO {
struct q_instream {
template < typename classT >
inline q_instream operator>>(classT &e) const {
e= 0;
classT f= 1, c= 0;
while(c < '0' || c > '9') {
if(c == '-') f= -1;
c= getchar();
}
while(c >= '0' && c <= '9') e= e * 10 + c - '0', c= getchar();
return e= e * f, (*this);
}
} in;
struct q_outstream {
template < typename classT >
inline q_outstream operator<<(const classT &e) const {
if(e < 0) {
putchar('-');
(*this) << -e;
}
else {
if(e > 9) (*this) << (e / 10);
putchar(e % 10 + '0');
}
return (*this);
}
inline q_outstream operator<<(const char &c) const {
return putchar(c), (*this);
}
} out;
}
using namespace IO;
int n,num[MAXN];
struct node{
int l,r;
int pos;//问题的位置
}block[MAXN];
ll BIT[MAXN];
ll lowbit(ll x){return x&(-x);}
void change(int x,ll val){
for(int i=x;i<=n;){
BIT[i]+=val;
i+=lowbit(i);
}
}
ll getSum(int x){
ll res=0;
for(int i=x;i;){
res+=BIT[i];
i-=lowbit(i);
}
return res;
}
bool cmp(node a,node b){
if(a.r<b.r)return true;
else if(a.r<b.r&&a.l>b.l)return true;
return false;
}
int ans[MAXN];
unordered_map<int,int> mp;//first最后一次出现的位置
int book[MAXN];
int main(){
in>>n;
for(int i=1;i<=n;i++){
in>>num[i];
}
int m;
in>>m;
for(int i=1;i<=m;i++){
in>>block[i].l;in>>block[i].r;
block[i].pos=i;
}
sort(block+1,block+m+1,cmp);
int nxt=1;
for(int i=1;i<=m;i++){
for(int j=nxt;j<=block[i].r;j++){
if(book[num[j]]){
change(book[num[j]],-1);
}
change(j,1);
book[num[j]]=j;
}
nxt=block[i].r+1;
ans[block[i].pos]=getSum(block[i].r)-getSum(block[i].l-1);
}
for(int i=1;i<=m;i++){
out<<ans[i];
puts("");
}
return 0;
}