#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
#define FOR(i,a,b) for(ll i = (a);i <= (b);i++)
#define FOD(i,a,b) for(ll i = (a);i >= (b);i--)
#define el '\n'
#define ft first
#define sc second
const ll mang = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll MAX = 1e18;
const ll MAXMASK = (1 << 18);
ll n,m,d,q,k,k2;
vector<ll> a;
ll prek1[mang],prek2[mang];
ll sum = 0;
ll tinh(ll c,ll x,ll y){
    ll cl = sum - a[c];
    ll ans = 0;
    if(cl < y) ans += y - cl;
    if(a[c] < x) ans += x - a[c];
    return ans;
}
int main(){
    cin.tie(0) -> sync_with_stdio(0);
//    freopen("LEGO.INP","r",stdin);
//    freopen("LEGO.OUT","w",stdout);
    cin >> n;
    a.resize(n + 1);
    FOR(i,1,n) cin >> a[i],sum += a[i];
    sort(a.begin() + 1,a.end());
    a.push_back(MAX);
    cin >> m;
    FOR(i,1,m){
        ll x,y;
        cin >> x >> y;
        ll c = lower_bound(a.begin() + 1,a.end(),x) - a.begin();
        ll c1;
        if(c > 1) c1 = c - 1;
        else c1 = c;
        cout << min(tinh(c,x,y),tinh(c1,x,y)) << el;
    }
}
