//
// main.cpp
// 分块1
//
// Created by Candy on 2016/12/17.
// Copyright © 2016年 Candy. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=5e4+5;
inline int read(){
char c=getchar();int x=0,f=1;
while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();}
while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();}
return x*f;
}
int n,Q,a[N],bl,pos[N],mp[N],m;
inline int Bin(int v){
int l=1,r=m;
while(l<=r){
int mid=(l+r)>>1;
if(mp[mid]==v) return mid;
if(v<mp[mid]) r=mid-1;
else l=mid+1;
}
return -1;
}
struct ques{
int l,r,id;
bool operator <(const ques a)const{
if(pos[l]==pos[a.l]) return r<a.r;
else return pos[l]<pos[a.l];
}
}q[N];
int c[N];
ll ans,an[N];
inline int lowbit(int x){return x&-x;}
inline void add(int p,int v){for(int i=p;i<=n;i+=lowbit(i))c[i]+=v;}
inline int sum(int p){
int res=0;
for(int i=p;i;i-=lowbit(i)) res+=c[i];
return res;
}
inline int sum(int l,int r){
return sum(r)-sum(l-1);
}
void solve(){
int l=1,r=0;
for(int i=1;i<=Q;i++){
while(r<q[i].r) r++,ans+=sum(a[r]+1,m),add(a[r],1);
while(r>q[i].r) ans-=sum(a[r]+1,m),add(a[r],-1),r--;
while(l<q[i].l) ans-=sum(1,a[l]-1),add(a[l],-1),l++;
while(l>q[i].l) l--,ans+=sum(1,a[l]-1),add(a[l],1);
an[q[i].id]=ans;
}
}
int main(int argc, const char * argv[]) {
n=read();
bl=sqrt(n);
for(int i=1;i<=n;i++) a[i]=mp[i]=read(),pos[i]=(i-1)/bl+1;
sort(mp+1,mp+1+n);
m=1;
for(int i=2;i<=n;i++) if(a[i]!=a[i-1]) a[++m]=a[i];
for(int i=1;i<=n;i++) a[i]=Bin(a[i]);
Q=read();
for(int i=1;i<=Q;i++) q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+1,q+1+Q);
solve();
for(int i=1;i<=Q;i++) printf("%lld\n",an[i]);
return 0;
}