#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(),x.end()
#define REACH cerr<<"reached line "<<__LINE__<<endl
#define DBG(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl

using pii=pair<int,int>;
using vi=vector<int>;
using ld=long double;

template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
	os<<"[";
	REP(i,(int)v.size()){
		if(i)os<<",";
		os<<v[i];
	}
	os<<"]";
	return os;
}

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

void printSpace(){
	printf(" ");
}

void printEoln(){
	printf("\n");
}

void print(int x,int suc=1){
	printf("%lld",x);
	if(suc==1)
		printEoln();
	if(suc==2)
		printSpace();
}

string readString(){
	static char buf[3341919];
	scanf("%s",buf);
	return string(buf);
}

char* readCharArray(){
	static char buf[3341919];
	static int bufUsed=0;
	char* ret=buf+bufUsed;
	scanf("%s",ret);
	bufUsed+=strlen(ret)+1;
	return ret;
}

template<class T,class U>
void chmax(T& a,U b){
	if(a<b)
		a=b;
}

template<class T,class U>
void chmin(T& a,U b){
	if(a>b)
		a=b;
}

template<class T>
T Sq(const T& t){
	return t*t;
}

struct ConvexHullTrick{
	struct Line{
		int a,b;
		int Eval(int x){
			return a*x+b;
		}
	};
	vector<Line> data;
	void Add(int a,int b){
		while(int(data.size())>=2){
			Line p=data[data.size()-2];
			Line q=data[data.size()-1];
			if((b-q.b)*(p.a-q.a)>=(q.b-p.b)*(q.a-a))
				data.pop_back();
			else
				break;
		}
		data.PB(Line{a,b});
	}
	int Get(int x){
		while(int(data.size())>=2)
			if(data[data.size()-1].Eval(x)<=data[data.size()-2].Eval(x))
				data.pop_back();
			else
				break;
		return data.back().Eval(x);
	}
};

vi Wafrelka(int n,vi t,bool flip){
	if(flip)
		reverse(ALL(t));
	vi dp(n+1,0),ac(n+1,0);
	REP(i,n)
		ac[i+1]=ac[i]+t[i];
	ConvexHullTrick cht;
	REP(i,n+1){
		if(i)
			dp[i]=max(dp[i-1],i*(i+1)/2-ac[i]+cht.Get(i));
		cht.Add(-i,i*(i-1)/2+ac[i]+dp[i]);
	}
	if(flip)
		reverse(ALL(dp));
	return dp;
}

void Chokudai(const vi& ac,const vi& a,const vi& b,vi& dst,vi& tmp,int n,int l,int r,bool flip){
	if(r-l<=1)
		return;
	int mid;
	if(flip){
		r=n-r;
		l=n-l;
		mid=(l+r)/2;
		r=n-r;
		l=n-l;
		mid=n-mid;
	}else
		mid=(l+r)/2;
	ConvexHullTrick cht;
	FOR(i,l,mid+1)
		cht.Add(-i,i*(i-1)/2+ac[i]+a[i]);
	FOR(i,mid,r+1)
		tmp[i]=i*(i+1)/2-ac[i]+cht.Get(i)+b[i];
	for(int i=r-1;i>=mid;i--)
		chmax(tmp[i],tmp[i+1]);
	for(int i=r-1;i>=mid;i--)
		chmax(dst[i],tmp[i+1]);
	FOR(i,mid,r+1)
		tmp[i]=LLONG_MIN;
	Chokudai(ac,a,b,dst,tmp,n,l,mid,flip);
	Chokudai(ac,a,b,dst,tmp,n,mid,r,flip);
}

signed main(){
	int n=read();
	assert(1<=n&&n<=300000);
	vi t;
	int tsum=0;
	REP(i,n){
		int w=read();
		assert(1<=w&&w<=1000000000);
		assert(1<=w);
		tsum+=w;
		t.PB(w);
	}
	assert(tsum<=1000000000000LL);
	vi a=Wafrelka(n,t,false),b=Wafrelka(n,t,true);
	vi dst(n,LLONG_MIN/2),tmp(n+1,LLONG_MIN/2);
	REP(_,2){
		vi ac(n+1,0);
		REP(i,n)
			ac[i+1]=ac[i]+t[i];
		Chokudai(ac,a,b,dst,tmp,n,0,n,_);
		reverse(ALL(t));
		reverse(ALL(a));
		reverse(ALL(b));
		reverse(ALL(dst));
		swap(a,b);
	}
	int m=read();
	assert(1<=m&&m<=300000);
	REP(_,m){
		int p=read(),x=read();
		assert(1<=p&&p<=n);
		p--;
		assert(1<=x&&x<=1000000000);
		int no=a[p]+b[p+1],use=dst[p];
		use-=(x-t[p]);
		print(max(no,use));
	}
}


Measured memory_bytes: 18332