#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct node
{
int num,org;
}p[50010];
struct QUERY
{
int qa,qb,org;
}q[50010];
int n,m,nm,siz,sum;
int v[50010],ans[50010],s[50010];
bool cmp1(node a,node b)
{
return a.num<b.num;
}
bool cmp2(QUERY a,QUERY b)
{
if((a.qa-1)/siz==(b.qa-1)/siz) return a.qb<b.qb;
return (a.qa-1)/siz<(b.qa-1)/siz;
}
void updata(int x,int val)
{
for(int i=x;i<=nm;i+=i&-i) s[i]+=val;
}
int query(int x)
{
int i,ret=0;
for(i=x;i;i-=i&-i) ret+=s[i];
return ret;
}
int main()
{
scanf("%d",&n);
int i;
siz=(int)sqrt((double)n);
for(i=1;i<=n;i++) scanf("%d",&p[i].num),p[i].org=i;
sort(p+1,p+n+1,cmp1);
p[0].num=-1;
for(i=1;i<=n;i++)
{
if(p[i].num>p[i-1].num) nm++;
v[p[i].org]=nm;
}
scanf("%d",&m);
for(i=1;i<=m;i++) scanf("%d%d",&q[i].qa,&q[i].qb),q[i].org=i;
sort(q+1,q+m+1,cmp2);
int l=1,r=0;
for(i=1;i<=m;i++)
{
while(r<q[i].qb) sum+=r-l+1-query(v[++r]),updata(v[r],1);
while(r>q[i].qb) sum-=r-l+1-query(v[r]),updata(v[r--],-1);
while(l>q[i].qa) sum+=query(v[--l]-1),updata(v[l],1);
while(l<q[i].qa) sum-=query(v[l]-1),updata(v[l++],-1);
ans[q[i].org]=sum;
}
for(i=1;i<=m;i++) printf("%d\n",ans[i]);
return 0;
}