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.*;
  import java.util.*;
  public class check5  {
  
      public static long find(long sum,Set<Long> hs, long[] arr,int n,long attack,long defend)
      {
  
          int s=0,e=n-1;
  //        int ind=0;
          while(s<e)
          {
              int mid = s+(e-s)/2;
              if(arr[mid]==attack){
                  s=e=mid;
                  break;
              }
              if(arr[mid]<attack) s=mid+1;
              else e=mid;
          }
          long ans1 = Long.MAX_VALUE;
  
          long t1 = (attack>=arr[s])?attack-arr[s]:0;
          long t2 = (sum-arr[s]);
          long t3 = (defend>=t2)?defend-t2:0;
          ans1 = t1 + t3;
  
  
          long ans2 = Long.MAX_VALUE;
          if(s-1>=0)
          {
              t1 = (attack>=arr[s-1])?attack-arr[s-1]:0;
              t2 = (sum-arr[s-1]);
              t3 = (defend>=t2)?defend-t2:0;
  
              ans2 = t1 + t3;
          }
          return Math.min(ans1,ans2);
  
  
      }
      public static void main(String[] args) throws IOException{
  
          Reader sc=new Reader();
          PrintWriter out = new PrintWriter(System.out);
  
          int n = sc.nextInt();
          int arr[]=new int[n];
          Set<Long> hs = new HashSet<>();
          long sum=0;
          for(int i=0;i<n;i++)
          {
              long t1 = sc.nextLong();
              sum+=t1;
              hs.add(t1);
          }
          int m = hs.size();
          long a[]=new long[m];
          int ind=0;
          Iterator<Long> it= hs.iterator();
          while(it.hasNext())
          {
              a[ind++]=it.next();
          }
          int y=sc.nextInt();
          Arrays.sort(a);
          for(int i=0;i<y;i++)
          {
              long attack = sc.nextLong();
              long defend = sc.nextLong();
              out.println(find(sum,hs,a,m,attack,defend));
          }
  
          out.flush();
      }
  
      public static long power(long x,long power)
      {
          if(power==0) return 1;
  
          long ans1 = power(x,power/2);
          ans1=ans1*ans1;
  
          if(power%2==0) return ans1;
          return x*ans1;
      }
  
      //    (a*a_inverse) = 1(mod m)
  //    finding a_inverse
      public static long modInverse(long a,long m)
      {
  //        works only when m is prime
          long g=gcd(a,m);
          if(g==1) return power(a,m-2,m);
          else return -1;
      }
  
      //    (x^power)mod(m)
      public static long power(long x,long power,long m)
      {
          if(power==0) return 1;
  
          long p = power(x,power/2,m)%m;
          p = (p*p)%m;
  
          if(power%2==0) return p;
          return (x*p)%m;
      }
      public static long gcd(long small,long large)
      {
          if(small==0) return large;
          return gcd(large % small,small);
      }
  
      public static boolean isprime(long no)
      {
          if(no<=1) return false;
          if(no<=3) return true;
          if(no%2==0 || no%3==0) return false;
          // 6k+1 6k+5 can be prime
          for(long i=5;i*i<=no;i+=6)
          {
              if(no%(i)==0 || no%(i+2)==0)
                  return false;
          }
          return true;
      }
  
      //    prime no smaller than or equal to n
      public static boolean[] prime_nos_till_no(int no)
      {
          boolean prime[]=new boolean[no+1];
  //        prime[i]== true means prime
  //        prime[i]== false means not a prime
  //        initialize prime array as true
  
          Arrays.fill(prime,true);
          prime[0]=false;
          for(int i=2;i*i<=no;i++)
          {
              if(prime[i]==true)
              {
                  for(int j=i*i;j<=no;j+=i)
                  {
                      prime[j]=false;
                  }
              }
          }
          return prime;
  
      }
      public static void shufflearray(long arr[])
      {
  
  
          int n=arr.length;
          Random rand=new Random();
          for(int i=0;i<n;i++)
          {
              long temp=arr[i];
              int randomPos=i+rand.nextInt(n-i);
              arr[i]=arr[randomPos];
              arr[randomPos]=temp;
          }
      }
      //       Arrays.sort(arr, new Comparator<pair>() {
  //        //@Override
  //        public int compare(pair o1, pair o2) {
  //            long l1=o1.a-o2.a;
  //            if(l1<0L) return -1;
  //            if(l1==0) return 0;
  //            return 1;
  //        }
  //    });
      static class Reader {
          final private int BUFFER_SIZE = 1 << 16;
          private DataInputStream din;
          private byte[] buffer;
          private int bufferPointer, bytesRead;
  
          public Reader() {
              din = new DataInputStream(System.in);
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
  
          public Reader(String file_name) throws IOException {
              din = new DataInputStream(new FileInputStream(file_name));
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
  
          public String nextLine() throws IOException {
              byte[] buf = new byte[64]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n')
                      break;
                  buf[cnt++] = (byte) c;
              }
              return new String(buf, 0, cnt);
          }
          public boolean isSpaceChar(int c) {
              return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
          }
  
          public String next() throws IOException{
              int c = read();
              while (isSpaceChar(c)) c = read();
              StringBuilder res = new StringBuilder();
              do {
                  res.appendCodePoint(c);
                  c = read();
              } while (!isSpaceChar(c));
              return res.toString();
          }
  
  
          public int nextInt() throws IOException {
              int ret = 0;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
  
              if (neg)
                  return -ret;
              return ret;
          }
  
          public long nextLong() throws IOException {
              long ret = 0;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
              if (neg)
                  return -ret;
              return ret;
          }
  
          public double nextDouble() throws IOException {
              double ret = 0, div = 1;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
  
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
  
              if (c == '.') {
                  while ((c = read()) >= '0' && c <= '9') {
                      ret += (c - '0') / (div *= 10);
                  }
              }
  
              if (neg)
                  return -ret;
              return ret;
          }
  
          private void fillBuffer() throws IOException {
              bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
              if (bytesRead == -1)
                  buffer[0] = -1;
          }
  
          private byte read() throws IOException {
              if (bufferPointer == bytesRead)
                  fillBuffer();
              return buffer[bufferPointer++];
          }
  
          public void close() throws IOException {
              if (din == null)
                  return;
              din.close();
          }
  
      }
  
  }