#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
const long long inf = 0x3f3f3f3f3f3f3f3f;
long long a[maxn];
void solve() {
  long long n;
  cin >> n;
  long long res = 0;
  for (long long i = 0; i < n; i++) cin >> a[i], res += a[i];
  a[n] = inf;
  sort(a, a + n + 1);
  long long m;
  cin >> m;
  while (m--) {
    long long x, y;
    cin >> x >> y;
    long long l = upper_bound(a, a + n + 1, x) - a - 1;
    long long r = upper_bound(a, a + n + 1, x) - a;
    if (l == -1) {
      cout << max(0LL, y - (res - a[0])) << endl;
    } else if (r == n) {
      cout << x - a[n - 1] + max(0LL, y - (res - a[n - 1])) << endl;
    } else {
      long long k1 = (x - a[l]) + max(0LL, y - (res - a[l]));
      long long k2 = max(0LL, y - (res - a[r]));
      cout << min(k1, k2) << endl;
    }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
