#include<stdio.h>
#include<algorithm>
#define bl 174
typedef long long ll;
#define gc getchar_unlocked
#define pc(x) putchar_unlocked(x);
inline void print(ll n)
{
ll N = n, rev, count = 0;
rev = N;
if (N == 0) { pc('0'); pc('\n'); return ;}
while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s
rev = 0;
while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;} //store reverse of N in rev
while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;}
while (count--) pc('0');
}
ll read_int() {
char c = gc();
while(c<'0' || c>'9') c = gc();
ll ret = 0;
while(c>='0' && c<='9') {
ret = 10 * ret + c - 48;
c = gc();
}
return ret;
}
struct node{
ll l,r,p;
}q[200006];
ll ar[30005],cnt[1000006],answer=0,ans[2000006];
bool sorting(node a,node b)
{
if((a.l/bl)!=(b.l)/bl)
{
return((a.l/bl)<(b.l/bl));
}
return (a.r<b.r);
}
void remove(ll p)
{
cnt[ar[p]]--;
if(cnt[ar[p]]==0)
{
answer--;
}
}
void add(ll p)
{
cnt[ar[p]]++;
if(cnt[ar[p]]==1)
answer++;
}
int main()
{
using namespace std;
ll i,m,n;
n=read_int();
for(i=0;i<n;i++)
ar[i]=read_int();
m=read_int();
for(i=0;i<m;i++)
{
q[i].l=read_int();
q[i].r=read_int();
q[i].l--;
q[i].r--;
q[i].p=i;
}
sort(q,q+m,sorting);
ll cl=0,cr=0,x,y;
for(i=0;i<m;i++)
{
x=q[i].l;
y=q[i].r;
while(cl<x)
{
remove(cl);
cl++;
}
while(cl>x)
{
add(cl-1);
cl--;
}
while(cr<=y)
{
add(cr);
cr++;
}
while(cr>y+1)
{
remove(cr-1);
cr--;
}
ans[q[i].p]=answer;
}
for(i=0;i<m;i++)
{
print(ans[i]);
printf("\n");
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8YWxnb3JpdGhtPgojZGVmaW5lIGJsIDE3NAp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKI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