#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 20;
const long long INFLL = 2e18;
const int MAX = 1e6;
const long double pi = 3.14159265358979323846;
void usaco(string filename) {
  freopen((filename + ".in").c_str(), "r", stdin);
  freopen((filename + ".out").c_str(), "w", stdout);
}
void solve() {
  long long n;
  cin >> n;
  long long sum = 0;
  vector<long long> a(n);
  for (auto i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a.begin(), a.end());
  long long qq;
  cin >> qq;
  while (qq--) {
    long long x, y;
    cin >> x >> y;
    long long ans = INFLL;
    auto it = lower_bound(a.begin(), a.end(), x);
    if (it != a.end()) {
      long long have = sum - (*it);
      long long add = max(0LL, y - have);
      ans = min(ans, add);
    }
    if (it != a.begin()) {
      long long have = sum - (*prev(it));
      long long add = max(0LL, y - have);
      add += x - (*prev(it));
      ans = min(ans, add);
    }
    cout << ans << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t = 1;
  for (long long i = 1; i <= t; i++) {
    solve();
  }
}
