#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector <int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define pb push_back
#define INF 100000000
#define mp make_pair
#define MOD 1000000007
#define F first
#define S second
int n;
ll a[100005];
ll total[200005];
ll ans[200005];
ll pref[200005];
ll suff[200005];
int main() {
ios::sync_with_stdio(false);
cin >> n;
int x = 1;
while (x < n) x *= 2;
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = n; i < x; i++) a[i] = -(INF*INF);
n = x;
for (int i = n; i < 2*n; i++) total[i] = a[i-n];
for (int i = n-1; i > 0; i--) total[i] = total[i*2]+total[i*2+1];
for (int i = n; i < 2*n; i++) pref[i] = a[i-n];
for (int i = n-1; i > 0; i--) pref[i] = max(pref[i*2],total[i*2]+pref[i*2+1]);
for (int i = n; i < 2*n; i++) suff[i] = a[i-n];
for (int i = n-1; i > 0; i--) suff[i] = max(suff[i*2+1],total[i*2+1]+suff[i*2]);
for (int i = n; i < 2*n; i++) ans[i] = a[i-n];
for (int i = n-1; i > 0; i--) ans[i] = max(suff[i*2]+pref[i*2+1],
max(ans[i*2],ans[i*2+1])
);
// for (int i = 0; i < 2*n; i++) cout << i << " - " << ans[i] << endl;
int m;
cin >> m;
while (m --){
ll l,r;
cin >> l >> r;
l --;
l += n;
r += n;
ll res = -(INF*INF);
ll prev = res;
vector<ll> R1,R2;
while (l < r){
if (l%2 == 1){
// res = max(res,ans[l++]);
l++;
R1.pb(l-1);
}
if (r%2 == 1){
// res = max(res,ans[--r]);
--r;
R2.pb(r);
}
// cout << l << " " << r << " " << res << endl;
l /= 2;
r /= 2;
}
for (int i = R2.size()-1; i >= 0; i --) R1.pb(R2[i]);
for (int i = 0; i < R1.size(); i++){
res = max(res,ans[R1[i]]);
res = max(res,prev+pref[R1[i]]);
prev = max(prev+total[R1[i]],suff[R1[i]]);
}
cout << res << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFj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KyspIHN1ZmZbaV0gPSBhW2ktbl07CiAgICBmb3IgKGludCBpID0gbi0xOyBpID4gMDsgaS0tKSBzdWZmW2ldID0gbWF4KHN1ZmZbaSoyKzFdLHRvdGFsW2kqMisxXStzdWZmW2kqMl0pOwogCiAgICBmb3IgKGludCBpID0gbjsgaSA8IDIqbjsgaSsrKSBhbnNbaV0gPSBhW2ktbl07CiAgICBmb3IgKGludCBpID0gbi0xOyBpID4gMDsgaS0tKSBhbnNbaV0gPSBtYXgoc3VmZltpKjJdK3ByZWZbaSoyKzFdLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG1heChhbnNbaSoyXSxhbnNbaSoyKzFdKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICk7CiAKICAgIC8vIGZvciAoaW50IGkgPSAwOyBpIDwgMipuOyBpKyspIGNvdXQgPDwgaSA8PCAiIC0gIiA8PCBhbnNbaV0gPDwgZW5kbDsKIAogICAgaW50IG07CiAgICBjaW4gPj4gbTsKICAgIHdoaWxlIChtIC0tKXsKICAgICAgICBsbCBsLHI7CiAgICAgICAgY2luID4+IGwgPj4gcjsKICAgICAgICBsIC0tOwogICAgICAgIGwgKz0gbjsKICAgICAgICByICs9IG47CiAgICAgICAgbGwgcmVzID0gLShJTkYqSU5GKTsKICAgICAgICBsbCBwcmV2ID0gcmVzOwogICAgICAgIHZlY3RvcjxsbD4gUjEsUjI7CiAKICAgICAgICB3aGlsZSAobCA8IHIpewogICAgICAgICAgICBpZiAobCUyID09IDEpewogICAgICAgICAgICAgICAgLy8gcmVzID0gbWF4KHJlcyxhbnNbbCsrXSk7CiAgICAgICAgICAgICAgICBsKys7CiAgICAgICAgICAgICAgICBSMS5wYihsLTEpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChyJTIgPT0gMSl7CiAgICAgICAgICAgICAgICAvLyByZXMgPSBtYXgocmVzLGFuc1stLXJdKTsKICAgICAgICAgICAgICAgIC0tcjsKICAgICAgICAgICAgICAgIFIyLnBiKHIpOwogICAgICAgICAgICB9CiAKICAgICAgICAgICAgLy8gY291dCA8PCBsIDw8ICIgIiA8PCByIDw8ICIgIiA8PCByZXMgPDwgZW5kbDsKIAogICAgICAgICAgICBsIC89IDI7CiAgICAgICAgICAgIHIgLz0gMjsKICAgICAgICB9CiAKICAgICAgICBmb3IgKGludCBpID0gUjIuc2l6ZSgpLTE7IGkgPj0gMDsgaSAtLSkgUjEucGIoUjJbaV0pOwogCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBSMS5zaXplKCk7IGkrKyl7CiAgICAgICAgICAgIHJlcyA9IG1heChyZXMsYW5zW1IxW2ldXSk7CiAgICAgICAgICAgIHJlcyA9IG1heChyZXMscHJlditwcmVmW1IxW2ldXSk7CiAgICAgICAgICAgIHByZXYgPSBtYXgocHJldit0b3RhbFtSMVtpXV0sc3VmZltSMVtpXV0pOwogICAgICAgIH0KIAogICAgICAgIGNvdXQgPDwgcmVzIDw8IGVuZGw7CiAgICB9Cn0=