#include <bits/stdc++.h>
using namespace std;
int bs(long long int a[], int n, long long int key) {
  int i, j, mid;
  i = 0;
  j = n - 1;
  while (1) {
    mid = (i + j) / 2;
    if (a[mid] == key) {
      return mid;
    } else if (a[mid] < key) {
      i = mid + 1;
      if (i > j) return mid + 1;
    } else {
      j = mid - 1;
      if (i > j) {
        return mid;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int m, n;
  cin >> n;
  long long int a[n], s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  sort(a, a + n);
  cin >> m;
  for (int _t = 0; _t < m; _t++) {
    long long int attack, defence;
    long long int ans1, ans2;
    ans1 = 0;
    ans2 = 0;
    int second = 0;
    cin >> defence >> attack;
    int x = bs(a, n, defence);
    if (x >= n) x--;
    if (a[x] < defence) {
      ans1 += (defence - a[x]);
    }
    if ((s - a[x]) < attack) {
      ans1 += (attack - (s - a[x]));
    }
    x--;
    if (x >= 0) {
      second = 1;
      if (a[x] < defence) {
        ans2 += (defence - a[x]);
      }
      if ((s - a[x]) < attack) {
        ans2 += (attack - (s - a[x]));
      }
    }
    if (second == 1) {
      if (ans1 < ans2) {
        cout << ans1 << "\n";
      } else {
        cout << ans2 << "\n";
      }
    } else {
      cout << ans1 << "\n";
    }
  }
  return 0;
}
