#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a.begin(), a.end());
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    auto lb = lower_bound(a.begin(), a.end(), x);
    if (lb == a.end()) lb--;
    long long ost = sum - *lb;
    long long ans = max(0ll, y - ost) + max(0ll, x - *lb);
    if (lb != a.begin()) {
      lb--;
      ost = sum - *lb;
      ans = min(ans, max(0ll, y - ost) + max(0ll, x - *lb));
    }
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
