#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
template <typename T>
void print(std::vector<T>& A) {
  for (auto& e : A) {
    cout << e << " ";
  }
  cout << "\n";
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> A(n);
  long long sum = 0;
  for (auto& e : A) {
    cin >> e;
    sum += e;
  }
  sort((A).begin(), (A).end());
  long long m;
  cin >> m;
  while (m--) {
    long long x, y;
    cin >> x >> y;
    long long ans = 2e18;
    auto it = lower_bound((A).begin(), (A).end(), x);
    if (it != A.end()) {
      long long left = sum - (*it);
      long long req = ({
        __typeof__(0ll) _a = (0ll);
        __typeof__(y - left) _b = (y - left);
        _a > _b ? _a : _b;
      });
      ans = ({
        __typeof__(ans) _a = (ans);
        __typeof__(req) _b = (req);
        _a < _b ? _a : _b;
      });
    }
    if (it != A.begin()) {
      --it;
      long long left = sum - (*it);
      long long req = ({
        __typeof__(0ll) _a = (0ll);
        __typeof__(y - left) _b = (y - left);
        _a > _b ? _a : _b;
      });
      req += (x - (*it));
      ans = ({
        __typeof__(ans) _a = (ans);
        __typeof__(req) _b = (req);
        _a < _b ? _a : _b;
      });
    }
    cout << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  solve();
  ;
  return 0;
}
