/*
还是莫队的模板
刚开始我考虑转移的时候,一直往O(1)那里想,后经某大神提醒,可以用线段树或后缀数组转移,还是做题少啊,
如果O(1)能转移的话,求逆序对不就O(n)了吗!!!
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#define N 100010
#define lon long long
using namespace std;
int n,m;
int a[N],b[N],bl[N];
lon t[N],ans[N];
struct node{int l,r,id;}q[N];
bool cmp(const node&s1,const node&s2){
if(bl[s1.l]==bl[s2.l]) return s1.r<s2.r;
return bl[s1.l]<bl[s2.l];
}
void add(int x,int val){
for(int i=x;i<=n;i+=i&-i)
t[i]+=val;
}
lon query(int x){
lon sum=0;
for(int i=x;i;i-=i&-i)
sum+=t[i];
return sum;
}
void solve(){
lon now=0;int l=1,r=0;
for(int i=1;i<=m;i++){
while(l<q[i].l)
add(a[l],-1),now-=query(a[l]-1),l++;
while(r>q[i].r)
add(a[r],-1),now-=r-l-query(a[r]),r--;
while(l>q[i].l)
l--,add(a[l],1),now+=query(a[l]-1);
while(r<q[i].r)
r++,add(a[r],1),now+=r-l+1-query(a[r]);
ans[q[i].id]=now;
}
}
int main(){
scanf("%d",&n);int len=sqrt(n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
bl[i]=(i-1)/len+1;
}
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+n+1,a[i])-b;
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
sort(q+1,q+m+1,cmp);
solve();
for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
return 0;
}