#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  vector<long long> v = {1, 2, 3, 4, 5};
  long long n, sum_e = 0;
  cin >> n;
  vector<long long> hero(n);
  for (long long i = 0; i < n; i++) {
    cin >> hero[i];
    sum_e += hero[i];
  }
  sort(hero.begin(), hero.end());
  long long m;
  cin >> m;
  while (m--) {
    long long x, y, cnt = 0;
    cin >> x >> y;
    auto it = lower_bound(hero.begin(), hero.end(), x);
    if (it == hero.end()) {
      long long e = hero[n - 1];
      cnt += x - e;
      long long s = sum_e - e;
      if (s < y) cnt += y - s;
    } else {
      long long ind = it - hero.begin();
      long long c1 = 0, c2 = 0;
      if (x > hero[ind]) c1 += x - hero[ind];
      if (x > hero[ind - 1]) c2 += x - hero[ind - 1];
      if (y > (sum_e - hero[ind])) c1 += y - (sum_e - hero[ind]);
      if (y > (sum_e - hero[ind - 1])) c2 += y - (sum_e - hero[ind - 1]);
      cnt = min(c1, c2);
    }
    cout << cnt << endl;
  }
  return 0;
}
