#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 20010
using namespace std;
int n,m,num,ans=0;
int val[MAXN],lsh[MAXN],root[MAXN];
inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
namespace CT{
int size=0;
struct Chairman_Tree{
int sum,lson,rson;
}a[MAXN*400];
void insert(int k,int v,int l,int r,int &rt){
a[++size]=a[rt];rt=size;
a[rt].sum+=v;
if(l==r)return;
int mid=l+r>>1;
if(k<=mid)insert(k,v,l,mid,a[rt].lson);
else insert(k,v,mid+1,r,a[rt].rson);
}
int sum(int lside,int rside,int l,int r,int rt){
if(lside<=l&&r<=rside)return a[rt].sum;
int mid=l+r>>1,ans=0;
if(lside<=mid)ans+=sum(lside,rside,l,mid,a[rt].lson);
if(mid<rside)ans+=sum(lside,rside,mid+1,r,a[rt].rson);
return ans;
}
inline int lowbit(int x){return x&(-x);}
void update(int x,int k,int v){for(;x<=n;x+=lowbit(x))insert(k,v,1,num,root[x]);}
int query(int l,int r,int lside,int rside){
int s=0;
for(;r;r-=lowbit(r))s+=sum(lside,rside,1,num,root[r]);
for(--l;l;l-=lowbit(l))s-=sum(lside,rside,1,num,root[l]);
return s;
}
}
void work(){
int x,y;
while(m--){
x=read();y=read();
if(x>y)swap(x,y);
if(val[x]>1)ans-=CT::query(x+1,y,1,val[x]-1);
if(val[y]<num)ans-=CT::query(x,y-1,val[y]+1,num);
if(val[x]>val[y])ans++;
CT::update(x,val[x],-1);
CT::update(y,val[y],-1);
swap(val[x],val[y]);
CT::update(x,val[x],1);
CT::update(y,val[y],1);
if(val[x]>1)ans+=CT::query(x+1,y,1,val[x]-1);
if(val[y]<num)ans+=CT::query(x,y-1,val[y]+1,num);
if(val[x]>val[y])ans--;
printf("%d\n",ans);
}
}
void init(){
n=read();
for(int i=1;i<=n;i++)val[i]=lsh[i]=read();
m=read();
sort(lsh+1,lsh+n+1);
num=unique(lsh+1,lsh+n+1)-lsh-1;
for(int i=1;i<=n;i++){
val[i]=lower_bound(lsh+1,lsh+num+1,val[i])-lsh;
CT::update(i,val[i],1);
if(val[i]<num)ans+=CT::query(1,i,val[i]+1,num);
}
printf("%d\n",ans);
}
int main(){
init();
work();
return 0;
}