#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=5e4+10,M=4e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;
/// 数组大小
struct AYT
{
int tree[N];
int lowbit(int x)
{
return x&-x;
}
void update(int x,int c)
{
while(x<N)
{
tree[x]+=c;
x+=lowbit(x);
}
}
int query(int x)
{
int ans=0;
while(x)
{
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
};
AYT tree;
int n,pos[N],k,a[N],b[N];
struct is
{
int l,r,now;
bool operator <(const is &b)const
{
if(pos[l]!=pos[b.l])
return pos[l]<pos[b.l];
return r<b.r;
}
}p[N];
ll out[N],ans;
int getpos(int x)
{
int pos=lower_bound(b+1,b+1+n,x)-b;
return pos;
}
void addp(int x)
{
int z=getpos(a[x]);
ans+=tree.query(z-1);
tree.update(z,1);
}
void addn(int x)
{
int z=getpos(a[x]);
ans+=tree.query(N-5)-tree.query(z);
tree.update(z,1);
}
void delp(int x)
{
int z=getpos(a[x]);
ans-=tree.query(z-1);
tree.update(z,-1);
}
void deln(int x)
{
int z=getpos(a[x]);
ans-=tree.query(N-5)-tree.query(z);
tree.update(z,-1);
}
int main()
{
scanf("%d",&n);
k=sqrt(n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),pos[i]=(i-1)/k+1,b[i]=a[i];
sort(b+1,b+n+1);
int q;
scanf("%d",&q);
for(int i=1;i<=q;i++)
scanf("%d%d",&p[i].l,&p[i].r),p[i].now=i;
sort(p+1,p+1+q);
int L=1,R=0;
for(int i=1;i<=q;i++)
{
while(L<p[i].l)
{
delp(L);
L++;
}
while(L>p[i].l)
{
L--;
addp(L);
}
while(R>p[i].r)
{
deln(R);
R--;
}
while(R<p[i].r)
{
R++;
addn(R);
}
out[p[i].now]=ans;
}
for(int i=1;i<=q;i++)
printf("%lld\n",out[i]);
return 0;
}