#include<bits/stdc++.h>
using namespace std;
#define N 200001
#define lc k<<1
#define rc k<<1|1
#define mid (l+r>>1)
struct ss{
int maxn,minn,ans1,ans2;
}a[N<<2];
int xl[N],n,m;
inline void updata(int k){
a[k].maxn=max(a[lc].maxn,a[rc].maxn);
a[k].minn=min(a[lc].minn,a[rc].minn);
int t=a[rc].maxn-a[lc].minn;
int c=a[lc].maxn-a[rc].minn;
a[k].ans1=max(t,max(a[lc].ans1,a[rc].ans1));
a[k].ans2=max(c,max(a[rc].ans2,a[lc].ans2));
}
void build(int k,int l,int r){
if(l==r){
a[k].maxn=a[k].minn=xl[l];
a[k].ans1=a[k].ans2=0;
return;
}
build(lc,l,mid);
build(rc,mid+1,r);
updata(k);
}
ss query(int k,int l,int r,int x,int y){
if(y<l||x>r) return (ss){-1,100000001,-1,-1};
if(x<=l&&y>=r) return a[k];
ss tmp1=query(lc,l,mid,x,y),tmp2=query(rc,mid+1,r,x,y),tmp;
tmp.ans1=max(tmp1.ans1,max(tmp2.ans1,tmp2.maxn-tmp1.minn));
tmp.ans2=max(tmp1.ans2,max(tmp2.ans2,tmp1.maxn-tmp2.minn));
tmp.minn=min(tmp1.minn,tmp2.minn);
tmp.maxn=max(tmp1.maxn,tmp2.maxn);
return tmp;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&xl[i]);
build(1,1,n);
scanf("%d",&m);
for(int i=1,l,r;i<=m;i++){
scanf("%d%d",&l,&r);
if(l<=r)
printf("%d\n",query(1,1,n,l,r).ans1);
else
printf("%d\n",query(1,1,n,r,l).ans2);
}
return 0;
}