#include <bits/stdc++.h>
using namespace std;
long long p = 1000000007;
long long powmodp(long long x, long long y) {
  long long res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void generatee(string s, long long l, long long r) {
  string s1 = s;
  if (l == 0) {
    for (long long i = 0; i < r; i++) {
      s1 += ')';
    }
    return;
  } else {
    if (l == r) {
      s1 += '(';
      generatee(s1, l - 1, r);
    } else {
      string s2 = s1;
      s1 += '(';
      generatee(s1, l - 1, r);
      s2 += ')';
      generatee(s2, l, r - 1);
    }
  }
}
set<long long> s[105];
void find_prime_factors(long long n, long long pos) {
  while (n % 2 == 0) {
    n /= 2;
    s[pos].insert(2);
  }
  for (long long i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      s[pos].insert(i);
      n /= i;
    }
  }
  if (n > 2) {
    s[pos].insert(n);
  }
}
long long cnt[20];
long long pre[20];
void dfs(long long x, vector<long long> v[], bool vis[]) {
  if (vis[x] == false) {
    vis[x] = true;
  }
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      pre[v[x][i]] = cnt[v[x][i]];
      cnt[v[x][i]] += (cnt[x] - pre[x]);
      dfs(v[x][i], v, vis);
    } else {
      pre[v[x][i]] = cnt[v[x][i]];
      cnt[v[x][i]] += (cnt[x] - pre[x]);
      dfs(v[x][i], v, vis);
    }
  }
}
long long fermat(long long n) { return powmodp(n, p - 2); }
long long nCrmodp(long long n, long long r) {
  r = min(r, n - r);
  long long num = 1, den = 1;
  for (long long i = 1; i <= r; i++) {
    den = (den * i) % p;
    num = (num * (n - i + 1)) % p;
  }
  long long ans = ((num) % p) * (fermat(den) % p);
  ans %= p;
  return ans;
}
long long cnt_fin = 0;
long long partitionn(long long a[], long long l, long long r) {
  long long st = l;
  for (long long i = l; i < r; i++) {
    if (a[i] <= a[r]) {
      cnt_fin += (i - st);
      swap(a[st], a[i]);
      st++;
    }
  }
  cnt_fin += (r - st);
  swap(a[st], a[r]);
  return st;
}
void quicksort(long long a[], long long l, long long r) {
  if (l < r) {
    long long pos = partitionn(a, l, r);
    quicksort(a, l, pos - 1);
    quicksort(a, pos + 1, r);
  }
}
long long tim;
map<char, long long> m1;
map<long long, char> m2;
long long m4[2005];
long long m3[2005];
vector<pair<long long, long long>> pairs;
void dfs(long long x, vector<long long> v[], bool vis[], long long sup_par) {
  if (vis[x] == false) vis[x] = true;
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      if (m3[v[x][i]]) {
        m4[sup_par]++;
        sup_par = v[x][i];
      }
      dfs(v[x][i], v, vis, sup_par);
    } else {
      if (m3[v[x][i]]) {
        m4[sup_par]++;
      }
    }
  }
}
bool check(long long m, long long prea[], long long preb[], long long n) {
  long long tot = m + n;
  long long val = tot - floor(double(tot) / double(4));
  long long rem = val - m;
  long long suma = min(val, m) * 100;
  long long sumb = 0;
  long long mi = min(val, n);
  sumb = preb[mi - 1];
  if (rem > 0) suma += prea[rem - 1];
  if (suma >= sumb)
    return true;
  else
    return false;
}
void dfs1(long long x, vector<long long> v[], bool vis[], long long par[],
          long long lev[]) {
  if (vis[x] == false) {
    vis[x] = true;
  }
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      lev[v[x][i]] = lev[x] + 1;
      par[v[x][i]] = x;
      dfs1(v[x][i], v, vis, par, lev);
    }
  }
}
long long dp[1001][1001];
long long par[1001][1001][2];
char a[1001][1001];
bool used[1001][1001];
bool vis[1001][1001];
long long dfsdp(long long x, long long y, long long n, long long m) {
  if (dp[x][y]) return dp[x][y];
  vector<pair<long long, long long>> dir;
  dir.push_back({1, 0});
  dir.push_back({0, 1});
  dir.push_back({-1, 0});
  dir.push_back({0, -1});
  for (long long i = 0; i < 4; i++) {
    long long xx = x + dir[i].first;
    long long yy = y + dir[i].second;
    if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
    if (a[xx][yy] != '.') continue;
    if (used[xx][yy]) continue;
    used[xx][yy] = true;
    long long val = dfsdp(xx, yy, n, m);
    used[xx][yy] = false;
    if (val >= dp[x][y]) {
      par[x][y][0] = xx;
      par[x][y][1] = yy;
    }
    dp[x][y] = max(dp[x][y], val);
  }
  return ++dp[x][y];
}
string dectobin(long long n) {
  string s = "";
  while (n > 1) {
    long long rem = n % 2;
    if (rem)
      s += '1';
    else
      s += '0';
    n /= 2;
  }
  if (n)
    s += '1';
  else
    s += '0';
  reverse(s.begin(), s.end());
  return s;
}
long long bintodec(string s) {
  reverse(s.begin(), s.end());
  long long ans = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      long long val = round(pow(2, i));
      ans = ans + val;
    }
  }
  return ans;
}
long long conn = 0;
void dfss(long long x, vector<long long> v[], bool vis[], long long lev[],
          long long req) {
  if (vis[x] == false) vis[x] = true;
  if (x == req) return;
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      lev[v[x][i]] = lev[x] + 1;
      dfss(v[x][i], v, vis, lev, req);
    }
  }
}
bool checkk(vector<pair<long long, long long>> v1, long long x) {
  for (long long i = 0; i < v1.size(); i++) {
    if (x < v1[i].first) return false;
    x = x + v1[i].second;
  }
  return true;
}
vector<long long> v[1000007];
bool check(long long mid, long long so, long long des, long long n,
           long long m[]) {
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  vis[des] = 1;
  long long dis[n + 1];
  memset(dis, n + 5, sizeof(dis));
  dis[des] = 0;
  queue<long long> q;
  q.push(des);
  while (!q.empty()) {
    long long val = q.front();
    cout << val << endl;
    cout << v[val].size() << endl;
    for (long long i = 0; i < v[val].size(); i++) {
      cout << "entered for " << endl;
      if (vis[v[val][i]] == false && m[v[val][i]] == 0) {
        dis[v[val][i]] = dis[val] + 1;
        if (dis[v[val][i]] <= mid) {
          q.push(v[val][i]);
          vis[v[val][i]] = true;
        }
      } else if (vis[v[val][i]] == false && m[v[val][i]] == 1) {
        dis[v[val][i]] = dis[val] + 1;
        if (dis[v[val][i]] <= mid) {
          q.push(v[val][i]);
          vis[v[val][i]] = true;
          dis[v[val][i]] = 0;
        }
      } else if (vis[v[val][i]] == true && dis[v[val][i]] > dis[val] + 1) {
        q.push(v[val][i]);
        dis[v[val][i]] = dis[val] + 1;
      }
    }
    cout << "here" << endl;
    q.pop();
  }
  if (dis[so] != (n + 5) && vis[so] == true) {
    return true;
  }
  cout << "here again" << endl;
  return false;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> v;
  long long x;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
    sum += x;
  }
  sort(v.begin(), v.end());
  long long m;
  cin >> m;
  long long a[m], b[m];
  for (long long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    auto it = lower_bound(v.begin(), v.end(), a[i]);
    if (it == v.end()) {
      long long temp = sum;
      temp -= v[n - 1];
      long long ans = a[i] - v[n - 1];
      if (temp < b[i]) {
        ans += (b[i] - temp);
      }
      cout << ans << endl;
    } else {
      long long ans = 0;
      long long pos = it - v.begin();
      long long temp1 = sum;
      temp1 -= v[pos];
      if (b[i] > temp1) {
        ans = (b[i] - temp1);
      }
      if (pos != 0) {
        long long temp2 = sum;
        pos--;
        temp2 -= v[pos];
        long long val;
        val = (a[i] - v[pos]);
        if (temp2 < b[i]) {
          val += (b[i] - temp2);
        }
        ans = min(ans, val);
      }
      cout << ans << endl;
    }
  }
}

### Prompt

Please formulate a CPP solution to the following problem:

Recently, Petya learned about a new game "Slay the Dragon". As the name suggests, the player will have to fight with dragons. To defeat a dragon, you have to kill it and defend your castle. To do this, the player has a squad of n heroes, the strength of the i-th hero is equal to a_i.

According to the rules of the game, exactly one hero should go kill the dragon, all the others will defend the castle. If the dragon's defense is equal to x, then you have to send a hero with a strength of at least x to kill it. If the dragon's attack power is y, then the total strength of the heroes defending the castle should be at least y.

The player can increase the strength of any hero by 1 for one gold coin. This operation can be done any number of times.

There are m dragons in the game, the i-th of them has defense equal to x_i and attack power equal to y_i. Petya was wondering what is the minimum number of coins he needs to spend to defeat the i-th dragon.

Note that the task is solved independently for each dragon (improvements are not saved).

Input

The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — number of heroes.

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{12}), where a_i is the strength of the i-th hero.

The third line contains a single integer m (1 ≤ m ≤ 2 ⋅ 10^5) — the number of dragons.

The next m lines contain two integers each, x_i and y_i (1 ≤ x_i ≤ 10^{12}; 1 ≤ y_i ≤ 10^{18}) — defense and attack power of the i-th dragon.

Output

Print m lines, i-th of which contains a single integer — the minimum number of coins that should be spent to defeat the i-th dragon.

Example

Input


4
3 6 2 3
5
3 12
7 9
4 14
1 10
8 7


Output


1
2
4
0
2

Note

To defeat the first dragon, you can increase the strength of the third hero by 1, then the strength of the heroes will be equal to [3, 6, 3, 3]. To kill the dragon, you can choose the first hero.

To defeat the second dragon, you can increase the forces of the second and third heroes by 1, then the strength of the heroes will be equal to [3, 7, 3, 3]. To kill the dragon, you can choose a second hero.

To defeat the third dragon, you can increase the strength of all the heroes by 1, then the strength of the heroes will be equal to [4, 7, 3, 4]. To kill the dragon, you can choose a fourth hero.

To defeat the fourth dragon, you don't need to improve the heroes and choose a third hero to kill the dragon.

To defeat the fifth dragon, you can increase the strength of the second hero by 2, then the strength of the heroes will be equal to [3, 8, 2, 3]. To kill the dragon, you can choose a second hero.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
long long p = 1000000007;
long long powmodp(long long x, long long y) {
  long long res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void generatee(string s, long long l, long long r) {
  string s1 = s;
  if (l == 0) {
    for (long long i = 0; i < r; i++) {
      s1 += ')';
    }
    return;
  } else {
    if (l == r) {
      s1 += '(';
      generatee(s1, l - 1, r);
    } else {
      string s2 = s1;
      s1 += '(';
      generatee(s1, l - 1, r);
      s2 += ')';
      generatee(s2, l, r - 1);
    }
  }
}
set<long long> s[105];
void find_prime_factors(long long n, long long pos) {
  while (n % 2 == 0) {
    n /= 2;
    s[pos].insert(2);
  }
  for (long long i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      s[pos].insert(i);
      n /= i;
    }
  }
  if (n > 2) {
    s[pos].insert(n);
  }
}
long long cnt[20];
long long pre[20];
void dfs(long long x, vector<long long> v[], bool vis[]) {
  if (vis[x] == false) {
    vis[x] = true;
  }
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      pre[v[x][i]] = cnt[v[x][i]];
      cnt[v[x][i]] += (cnt[x] - pre[x]);
      dfs(v[x][i], v, vis);
    } else {
      pre[v[x][i]] = cnt[v[x][i]];
      cnt[v[x][i]] += (cnt[x] - pre[x]);
      dfs(v[x][i], v, vis);
    }
  }
}
long long fermat(long long n) { return powmodp(n, p - 2); }
long long nCrmodp(long long n, long long r) {
  r = min(r, n - r);
  long long num = 1, den = 1;
  for (long long i = 1; i <= r; i++) {
    den = (den * i) % p;
    num = (num * (n - i + 1)) % p;
  }
  long long ans = ((num) % p) * (fermat(den) % p);
  ans %= p;
  return ans;
}
long long cnt_fin = 0;
long long partitionn(long long a[], long long l, long long r) {
  long long st = l;
  for (long long i = l; i < r; i++) {
    if (a[i] <= a[r]) {
      cnt_fin += (i - st);
      swap(a[st], a[i]);
      st++;
    }
  }
  cnt_fin += (r - st);
  swap(a[st], a[r]);
  return st;
}
void quicksort(long long a[], long long l, long long r) {
  if (l < r) {
    long long pos = partitionn(a, l, r);
    quicksort(a, l, pos - 1);
    quicksort(a, pos + 1, r);
  }
}
long long tim;
map<char, long long> m1;
map<long long, char> m2;
long long m4[2005];
long long m3[2005];
vector<pair<long long, long long>> pairs;
void dfs(long long x, vector<long long> v[], bool vis[], long long sup_par) {
  if (vis[x] == false) vis[x] = true;
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      if (m3[v[x][i]]) {
        m4[sup_par]++;
        sup_par = v[x][i];
      }
      dfs(v[x][i], v, vis, sup_par);
    } else {
      if (m3[v[x][i]]) {
        m4[sup_par]++;
      }
    }
  }
}
bool check(long long m, long long prea[], long long preb[], long long n) {
  long long tot = m + n;
  long long val = tot - floor(double(tot) / double(4));
  long long rem = val - m;
  long long suma = min(val, m) * 100;
  long long sumb = 0;
  long long mi = min(val, n);
  sumb = preb[mi - 1];
  if (rem > 0) suma += prea[rem - 1];
  if (suma >= sumb)
    return true;
  else
    return false;
}
void dfs1(long long x, vector<long long> v[], bool vis[], long long par[],
          long long lev[]) {
  if (vis[x] == false) {
    vis[x] = true;
  }
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      lev[v[x][i]] = lev[x] + 1;
      par[v[x][i]] = x;
      dfs1(v[x][i], v, vis, par, lev);
    }
  }
}
long long dp[1001][1001];
long long par[1001][1001][2];
char a[1001][1001];
bool used[1001][1001];
bool vis[1001][1001];
long long dfsdp(long long x, long long y, long long n, long long m) {
  if (dp[x][y]) return dp[x][y];
  vector<pair<long long, long long>> dir;
  dir.push_back({1, 0});
  dir.push_back({0, 1});
  dir.push_back({-1, 0});
  dir.push_back({0, -1});
  for (long long i = 0; i < 4; i++) {
    long long xx = x + dir[i].first;
    long long yy = y + dir[i].second;
    if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
    if (a[xx][yy] != '.') continue;
    if (used[xx][yy]) continue;
    used[xx][yy] = true;
    long long val = dfsdp(xx, yy, n, m);
    used[xx][yy] = false;
    if (val >= dp[x][y]) {
      par[x][y][0] = xx;
      par[x][y][1] = yy;
    }
    dp[x][y] = max(dp[x][y], val);
  }
  return ++dp[x][y];
}
string dectobin(long long n) {
  string s = "";
  while (n > 1) {
    long long rem = n % 2;
    if (rem)
      s += '1';
    else
      s += '0';
    n /= 2;
  }
  if (n)
    s += '1';
  else
    s += '0';
  reverse(s.begin(), s.end());
  return s;
}
long long bintodec(string s) {
  reverse(s.begin(), s.end());
  long long ans = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      long long val = round(pow(2, i));
      ans = ans + val;
    }
  }
  return ans;
}
long long conn = 0;
void dfss(long long x, vector<long long> v[], bool vis[], long long lev[],
          long long req) {
  if (vis[x] == false) vis[x] = true;
  if (x == req) return;
  for (long long i = 0; i < v[x].size(); i++) {
    if (vis[v[x][i]] == false) {
      vis[v[x][i]] = true;
      lev[v[x][i]] = lev[x] + 1;
      dfss(v[x][i], v, vis, lev, req);
    }
  }
}
bool checkk(vector<pair<long long, long long>> v1, long long x) {
  for (long long i = 0; i < v1.size(); i++) {
    if (x < v1[i].first) return false;
    x = x + v1[i].second;
  }
  return true;
}
vector<long long> v[1000007];
bool check(long long mid, long long so, long long des, long long n,
           long long m[]) {
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  vis[des] = 1;
  long long dis[n + 1];
  memset(dis, n + 5, sizeof(dis));
  dis[des] = 0;
  queue<long long> q;
  q.push(des);
  while (!q.empty()) {
    long long val = q.front();
    cout << val << endl;
    cout << v[val].size() << endl;
    for (long long i = 0; i < v[val].size(); i++) {
      cout << "entered for " << endl;
      if (vis[v[val][i]] == false && m[v[val][i]] == 0) {
        dis[v[val][i]] = dis[val] + 1;
        if (dis[v[val][i]] <= mid) {
          q.push(v[val][i]);
          vis[v[val][i]] = true;
        }
      } else if (vis[v[val][i]] == false && m[v[val][i]] == 1) {
        dis[v[val][i]] = dis[val] + 1;
        if (dis[v[val][i]] <= mid) {
          q.push(v[val][i]);
          vis[v[val][i]] = true;
          dis[v[val][i]] = 0;
        }
      } else if (vis[v[val][i]] == true && dis[v[val][i]] > dis[val] + 1) {
        q.push(v[val][i]);
        dis[v[val][i]] = dis[val] + 1;
      }
    }
    cout << "here" << endl;
    q.pop();
  }
  if (dis[so] != (n + 5) && vis[so] == true) {
    return true;
  }
  cout << "here again" << endl;
  return false;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> v;
  long long x;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
    sum += x;
  }
  sort(v.begin(), v.end());
  long long m;
  cin >> m;
  long long a[m], b[m];
  for (long long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    auto it = lower_bound(v.begin(), v.end(), a[i]);
    if (it == v.end()) {
      long long temp = sum;
      temp -= v[n - 1];
      long long ans = a[i] - v[n - 1];
      if (temp < b[i]) {
        ans += (b[i] - temp);
      }
      cout << ans << endl;
    } else {
      long long ans = 0;
      long long pos = it - v.begin();
      long long temp1 = sum;
      temp1 -= v[pos];
      if (b[i] > temp1) {
        ans = (b[i] - temp1);
      }
      if (pos != 0) {
        long long temp2 = sum;
        pos--;
        temp2 -= v[pos];
        long long val;
        val = (a[i] - v[pos]);
        if (temp2 < b[i]) {
          val += (b[i] - temp2);
        }
        ans = min(ans, val);
      }
      cout << ans << endl;
    }
  }
}

```