#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define MAXN 50010
#define MAXT 3200000
#define MAXB1 240
#define INF 0x3f3f3f3f
int n,m;
struct sgt_node
{
int lch,rch;
int t;
}sgt[MAXT];
int topt=0;
void Add_sgt(int &now,int base,int l,int r,int pos)
{
now=++topt;
sgt[now]=sgt[base];
sgt[now].t++;
if (l==r)return ;
if (pos<=((l+r)>>1))
Add_sgt(sgt[now].lch,sgt[base].lch,l,(l+r)>>1,pos);
else
Add_sgt(sgt[now].rch,sgt[base].rch,((l+r)>>1)+1,r,pos);
}
int Qry_sgt(int now1,int now2,int l,int r,int x,int y)
{
if (!now1 && !now2)return 0;
if (l==x && r==y)
return sgt[now1].t-sgt[now2].t;
int mid=(l+r)>>1;
if (y<=mid)
return Qry_sgt(sgt[now1].lch,sgt[now2].lch,l,mid,x,y);
else if (mid<x)
return Qry_sgt(sgt[now1].rch,sgt[now2].rch,mid+1,r,x,y);
else
return Qry_sgt(sgt[now1].lch,sgt[now2].lch,l,mid,x,mid)
+ Qry_sgt(sgt[now1].rch,sgt[now2].rch,mid+1,r,mid+1,y);
}
int roof[MAXN];
int Qry_tot(int rl,int rr,int x,int y)
{
return Qry_sgt(roof[rr],roof[rl-1],1,m,x,y);
}
int num[MAXN];
int bs[MAXB1],bt[MAXB1];
int bres1[MAXB1][MAXN];
struct aaa
{
int pos,val;
}a[MAXN];
bool cmp_pos(aaa a1,aaa a2)
{
return a1.pos<a2.pos;
}
bool cmp_val(aaa a1,aaa a2)
{
return a1.val<a2.val;
}
int tarr[MAXN];
void Add_val(int pos)
{
while (pos<MAXN)
{
tarr[pos]++;
pos+=pos&(-pos);
}
}
int Qry_sum(int pos)
{
int ret=0;
while (pos)
{
ret+=tarr[pos];
pos-=pos&(-pos);
}
return ret;
}
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
scanf("%d",&n);
int i,j,k;
int x,y,z;
for (i=1;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].pos=i;
}
sort(a+1,a+n+1,cmp_val);
m=0;
for (i=1;i<=n;i++)
{
if (i==1 || a[i].val!=x)
{
x=a[i].val;
a[i].val=++m;
}else
{
a[i].val=m;
}
}
sort(a+1,a+n+1,cmp_pos);
for (i=1;i<=n;i++)
num[i]=a[i].val;
int sb=(int)sqrt(n);
if (sb>n)sb=n;
for (i=1;i<=n;i++)
{
Add_sgt(roof[i],roof[i-1],1,m,num[i]);
}
memset(bs,INF,sizeof(bs));
memset(bt,0,sizeof(bt));
for (i=1;i<=n;i++)
{
bs[i/sb]=min(bs[i/sb],i);
bt[i/sb]=max(bt[i/sb],i);
}
int tp=n/sb;
int res=0;
for (i=0;i<=tp;i++)
{
memset(tarr,0,sizeof(tarr));
for (j=bs[i];j<=n;j++)
{
bres1[i][j]=bres1[i][j-1]+j-bs[i]-Qry_sum(num[j]);
Add_val(num[j]);
}
}
int q;
scanf("%d",&q);
res=0;
int t;
for (i=0;i<q;i++)
{
scanf("%d%d",&x,&y);
x^=res;y^=res;
if (abs(x/sb - y/sb)<=0)
{
res=0;
for (j=x+1;j<=y;j++)
{
res+=Qry_tot(x,j-1,num[j]+1,m);
}
printf("%d\n",res);
}else
{
res=bres1[x/sb+1][y];
for (j=x;j<=bt[x/sb];j++)
{
res+=Qry_tot(j+1,y,1,num[j]-1);
}
printf("%d\n",res);
}
}
}