description: 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.
solution: 
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.*;
  
  public class Q1 {
  
    /**
     * Template @author William Fiset, william.alexandre.fiset@gmail.com
     *
     */
    static InputReader in = new InputReader(System.in);
  
    static class InputReader {
  
      private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
      private static final InputStream DEFAULT_STREAM = System.in;
      private static final int MAX_DECIMAL_PRECISION = 21;
      private int c;
      private byte[] buf;
      private int bufferSize, bufIndex, numBytesRead;
  
      private InputStream stream;
  
      private static final byte EOF = -1;
      private static final byte NEW_LINE = 10;
      private static final byte SPACE = 32;
      private static final byte DASH = 45;
      private static final byte DOT = 46;
  
      private char[] charBuffer;
  
      private static byte[] bytes = new byte[58];
      private static int[] ints = new int[58];
      private static char[] chars = new char[128];
  
      static {
        char ch = ' ';
        int value = 0;
        byte _byte = 0;
        for (int i = 48; i < 58; i++) {
          bytes[i] = _byte++;
        }
        for (int i = 48; i < 58; i++) {
          ints[i] = value++;
        }
        for (int i = 32; i < 128; i++) {
          chars[i] = ch++;
        }
      }
  
      private static final double[][] doubles = {
        {0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d, 0.0000000000d, 0.00000000000d, 0.000000000000d, 0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d, 0.00000000000000000d, 0.000000000000000000d, 0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d},
        {0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d, 0.0000000001d, 0.00000000001d, 0.000000000001d, 0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d, 0.00000000000000001d, 0.000000000000000001d, 0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d},
        {0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d, 0.0000000002d, 0.00000000002d, 0.000000000002d, 0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d, 0.00000000000000002d, 0.000000000000000002d, 0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d},
        {0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d, 0.0000000003d, 0.00000000003d, 0.000000000003d, 0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d, 0.00000000000000003d, 0.000000000000000003d, 0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d},
        {0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d, 0.0000000004d, 0.00000000004d, 0.000000000004d, 0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d, 0.00000000000000004d, 0.000000000000000004d, 0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d},
        {0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d, 0.0000000005d, 0.00000000005d, 0.000000000005d, 0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d, 0.00000000000000005d, 0.000000000000000005d, 0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d},
        {0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d, 0.0000000006d, 0.00000000006d, 0.000000000006d, 0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d, 0.00000000000000006d, 0.000000000000000006d, 0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d},
        {0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d, 0.0000000007d, 0.00000000007d, 0.000000000007d, 0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d, 0.00000000000000007d, 0.000000000000000007d, 0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d},
        {0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d, 0.0000000008d, 0.00000000008d, 0.000000000008d, 0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d, 0.00000000000000008d, 0.000000000000000008d, 0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d},
        {0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d, 0.0000000009d, 0.00000000009d, 0.000000000009d, 0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d, 0.00000000000000009d, 0.000000000000000009d, 0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d}
      };
  
      public InputReader() {
        this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);
      }
  
      public InputReader(int bufferSize) {
        this(DEFAULT_STREAM, bufferSize);
      }
  
      public InputReader(InputStream stream) {
        this(stream, DEFAULT_BUFFER_SIZE);
      }
  
      public InputReader(InputStream stream, int bufferSize) {
        if (stream == null || bufferSize <= 0) {
          throw new IllegalArgumentException();
        }
        buf = new byte[bufferSize];
        charBuffer = new char[128];
        this.bufferSize = bufferSize;
        this.stream = stream;
      }
  
      private byte read() throws IOException {
  
        if (numBytesRead == EOF) {
          throw new IOException();
        }
  
        if (bufIndex >= numBytesRead) {
          bufIndex = 0;
          numBytesRead = stream.read(buf);
          if (numBytesRead == EOF) {
            return EOF;
          }
        }
  
        return buf[bufIndex++];
      }
  
      private void doubleCharBufferSize() {
        char[] newBuffer = new char[charBuffer.length << 1];
        for (int i = 0; i < charBuffer.length; i++) {
          newBuffer[i] = charBuffer[i];
        }
        charBuffer = newBuffer;
      }
  
      private int readJunk(int token) throws IOException {
  
        if (numBytesRead == EOF) {
          return EOF;
        }
  
        do {
  
          while (bufIndex < numBytesRead) {
            if (buf[bufIndex] > token) {
              return 0;
            }
            bufIndex++;
          }
  
          numBytesRead = stream.read(buf);
          if (numBytesRead == EOF) {
            return EOF;
          }
          bufIndex = 0;
  
        } while (true);
  
      }
  
      public byte nextByte() throws IOException {
        return (byte) nextInt();
      }
  
      public int nextInt() throws IOException {
  
        if (readJunk(DASH - 1) == EOF) {
          throw new IOException();
        }
        int sgn = 1, res = 0;
  
        c = buf[bufIndex];
        if (c == DASH) {
          sgn = -1;
          bufIndex++;
        }
  
        do {
  
          while (bufIndex < numBytesRead) {
            if (buf[bufIndex] > SPACE) {
              res = (res << 3) + (res << 1);
              res += ints[buf[bufIndex++]];
            } else {
              bufIndex++;
              return res * sgn;
            }
          }
  
          numBytesRead = stream.read(buf);
          if (numBytesRead == EOF) {
            return res * sgn;
          }
          bufIndex = 0;
  
        } while (true);
  
      }
  
      public int[] nextIntArray(int n) throws IOException {
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
          ar[i] = nextInt();
        }
        return ar;
      }
  
      public void close() throws IOException {
        stream.close();
      }
  
      public long nextLong() throws IOException {
        if (readJunk(DASH - 1) == EOF) {
          throw new IOException();
        }
        int sgn = 1;
        long res = 0L;
        c = buf[bufIndex];
        if (c == DASH) {
          sgn = -1;
          bufIndex++;
        }
        do {
          while (bufIndex < numBytesRead) {
            if (buf[bufIndex] > SPACE) {
              res = (res << 3) + (res << 1);
              res += ints[buf[bufIndex++]];
            } else {
              bufIndex++;
              return res * sgn;
            }
          }
          numBytesRead = stream.read(buf);
          if (numBytesRead == EOF) {
            return res * sgn;
          }
          bufIndex = 0;
        } while (true);
      }
    }
  
    public static void main(String args[]) throws Exception {
      int t = 1;
      while (t-- != 0) {
        solve();
      }
    }
  
    static void solve() throws IOException {
      int n = in.nextInt();
      long[] arr = new long[n];
      long sum = 0;
      for (int i = 0; i < n; i++) {
        arr[i] = in.nextLong();
        sum += arr[i];
      }
      Arrays.sort(arr);
      int m = in.nextInt();
      for (int i = 0; i < m; i++) {
        long def = in.nextLong();
        long attack = in.nextLong();
        long coins = 0;
        int hero = Math.max(0, nextSmaller(arr, def + 1));
        coins += Math.max(0, def - arr[hero]) + Math.max(0, attack - sum + arr[hero]);
        hero = nextBigger(arr, def - 1);
        hero = hero == -1 ? n - 1 : hero;
        coins = Math.min(coins, Math.max(0, def - arr[hero]) + Math.max(0, attack - sum + arr[hero]));
        System.out.println(coins < 0 ? 0 : coins);
      }
    }
  
    private static int nextSmaller(long[] arr, long target) {
  
      int start = 0, end = arr.length - 1;
      // Minimum size of the array should be 1
      if (end == 0) {
        return -1;
      }
      // If target lies beyond the max element, than the index of strictly smaller
      // value than target should be (end - 1)
      if (target > arr[end]) {
        return end;
      }
  
      int ans = -1;
      while (start <= end) {
        int mid = (start + end) / 2;
  
        // Move to the left side if the target is smaller
        if (arr[mid] >= target) {
          end = mid - 1;
        } // Move right side
        else {
          ans = mid;
          start = mid + 1;
        }
      }
      return ans;
    }
  
    private static int nextBigger(long[] arr, long target) {
      int start = 0, end = arr.length - 1;
  
      int ans = -1;
      while (start <= end) {
        int mid = (start + end) / 2;
  
        // Move to right side if target is
        // greater.
        if (arr[mid] <= target) {
          start = mid + 1;
        } // Move left side.
        else {
          ans = mid;
          end = mid - 1;
        }
      }
      return ans;
    }
  }
  