#include<bits/stdc++.h>
using namespace std;
#define fastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
const int mod = 1e9 + 7;
const long long INF = 1e18L + 5;
const int mxn = 2e5 + 1;
int cnt = 1;
void solve()
{
ll n,x,y;
cin>>n;
vi v(n);
rep(i,0,n)
cin>>v[i];
sort(v.begin(),v.end());
vi pre = v;
rep(i,1,n)
pre[i]+=pre[i-1];
ll q;
cin>>q;
while(q--)
{
cin>>x>>y;
ll ans = INF;
ll pos = std::lower_bound(v.begin(),v.end(),x) - v.begin();
ll need = 0;
if(pos==n)
{
ans = x - v[pos-1] + max(ll(0) , y - pre[n-1] + v[pos-1]);
}
else if(pos!=0)
{
ans = min(max(ll(0) , x - v[pos]) + max(ll(0) , y - pre[n-1] + v[pos]) , max(ll(0) , x - v[pos-1]) + max(ll(0) , y - pre[n-1] + v[pos-1]));
}
else
{
ans = max(ll(0) , x - v[pos]) + max(ll(0) , y - pre[n-1] + v[pos]);
}
cout<<ans<<"\n";
}
}
int main()
{
fastIO;
int t = 1;
//cin>>t;
while(t--)
{
solve();
cnt++;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBmYXN0SU8gaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwojZGVmaW5lIEYgZmlyc3QKI2RlZmluZSBTIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIHJlcChpLGEsYikgZm9yKGludCBpPWE7aTxiO2krKykKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgdmVjdG9yPGxsPiB2aTsKdHlwZWRlZiBwYWlyPGxsLGxsPiBwaTsKY29uc3QgaW50IG1vZCA9IDFlOSArIDc7CmNvbnN0IGxvbmcgbG9uZyBJTkYgPSAxZTE4TCArIDU7CmNvbnN0IGludCBteG4gPSAyZTUgKyAxOwppbnQgY250ID0gMTsKdm9pZCBzb2x2ZSgpCnsKICBsbCBuLHgseTsKICBjaW4+Pm47CiAgdmkgdihuKTsKICByZXAoaSwwLG4pCiAgY2luPj52W2ldOwogIHNvcnQodi5iZWdpbigpLHYuZW5kKCkpOwogIHZpIHByZSA9IHY7CiAgcmVwKGksMSxuKQogIHByZVtpXSs9cHJlW2ktMV07CiAgbGwgcTsKICBjaW4+PnE7CiAgd2hpbGUocS0tKQogIHsKICAgIGNpbj4+eD4+eTsKICAgIGxsIGFucyA9IElORjsKICAgIGxsIHBvcyA9IHN0ZDo6bG93ZXJfYm91bmQodi5iZWdpbigpLHYuZW5kKCkseCkgLSB2LmJlZ2luKCk7CiAgICBsbCBuZWVkID0gMDsKICAgIGlmKHBvcz09bikKICAgIHsKICAgICAgYW5zID0geCAtIHZbcG9zLTFdICsgbWF4KGxsKDApICwgeSAtIHByZVtuLTFdICsgdltwb3MtMV0pOwogICAgfQogICAgZWxzZSBpZihwb3MhPTApCiAgICB7CiAgICAgIGFucyA9IG1pbihtYXgobGwoMCkgLCB4IC0gdltwb3NdKSArICBtYXgobGwoMCkgLCB5IC0gcHJlW24tMV0gKyB2W3Bvc10pICwgbWF4KGxsKDApICwgeCAtIHZbcG9zLTFdKSArICBtYXgobGwoMCkgLCB5IC0gcHJlW24tMV0gKyB2W3Bvcy0xXSkpOwogICAgfQogICAgZWxzZQogICAgewogICAgICBhbnMgPSBtYXgobGwoMCkgLCB4IC0gdltwb3NdKSArICBtYXgobGwoMCkgLCB5IC0gcHJlW24tMV0gKyB2W3Bvc10pOwogICAgfQogICAgY291dDw8YW5zPDwiXG4iOwogIH0KCiAKfQppbnQgbWFpbigpCnsKICAgIGZhc3RJTzsKICAgIGludCB0ID0gMTsKICAgIC8vY2luPj50OwogICAgd2hpbGUodC0tKQogICAgewogICAgICAgIHNvbHZlKCk7CiAgICAgICAgY250Kys7CiAgICB9CiAgICByZXR1cm4gMDsKfQo=