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.util.*;
  import java.util.Map.Entry;
  
  
  import java.io.*;
  import static java.util.Map.Entry.*;
  
  /* Name of the class has to be "Main" only if the class is public. */
  public class Main
  {
  	static long mod=(long) (1e9+7);  
  	public static void main (String[] args) throws Exception
  	{
  		
  		final long mod1=(long) 1e9+7;
  		Reader s=new Reader();
  		  PrintWriter pt=new PrintWriter(System.out);
  		  
  		  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  //		  int T=Integer.parseInt(br.readLine());
  //		  int T=s.nextInt();
  //		  System.out.println(T);
  		  int T=1;
  		  ol:while(T-- > 0) {
  			  int n=s.nextInt();
  			  long arr[]=new long[n];
  			  long sum=0;
  			  for(int i=0;i<n;i++) {
  				  arr[i]=s.nextLong();
  				  sum+=arr[i];
  			  }
  			  sort(arr, 0, n-1);
  			  long sumex[]=new long[n];
  			  for(int i=0;i<n;i++) {
  				  sumex[i]=sum-arr[i];
  			  }
  			  int m=s.nextInt();
  			  while(m-- > 0) {
  				  long x=s.nextLong();
  				  long y=s.nextLong();
  				  int pr=prev(arr, x);
  				  int ne=next(arr, x);
  				  // prev
  				  long pcost=Long.MAX_VALUE;
  				  if(pr>-1) {
  					  pcost=(arr[pr]>=x?0:x-arr[pr])+(sumex[pr]>=y?0:y-sumex[pr]);
  				  }
  				  // next
  				  long ncost=Long.MAX_VALUE;
  				  if(ne>-1) {
  					  ncost=(arr[ne]>=x?0:x-arr[ne])+(sumex[ne]>=y?0:y-sumex[ne]);
  				  }
  				  pt.println(Math.min(pcost, ncost));
  			  }
  			  
  		  }
  		  pt.close();
  	}
  	
  	static int next(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;  
      }  
  	static int prev(long[] arr, long target)  
      {  
          int start = 0, end = arr.length - 1;  
      
          int ans = -1;  
          while (start <= end) {  
              int mid = (start + end) / 2;  
      
              // Move to left side if target is  
              // lesser.  
              if (arr[mid] > target) {
                  end = mid - 1;  
              }  
      
              // Move right side.  
              else {  
                  ans = mid;  
                  start = mid + 1;  
              }  
          }  
          return ans;  
      } 
  	
  	static void f(int arr[], int i) {
  		if(arr[i]>arr[i+1]) {
  			int temp=arr[i];
  			arr[i]=arr[i+1];
  			arr[i+1]=temp;
  		}
  	}
  
  	static boolean isSorted(int arr[]) {
  		for(int i=0;i<arr.length-1;i++) {
  			if(arr[i]>arr[i+1]) {
  				return false;
  			}
  		}
  		return true;
  	}
  
  	/**
  	 * Seive
  	 *
  	 * @param n length
  	 * @param factors factors[num] is smallest prime divisor of num
  	 * @param ar list of primes
  	 */
  	static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar) 
      { 
          factors[1]=1;
          int p;
          for(p = 2; p*p <=n; p++) 
          { 
              if(factors[p] == 0) 
              { 
                  ar.add(p);
                  factors[p]=p;
                  for(int i = p*p; i <= n; i += p) 
                      if(factors[i]==0)
                          factors[i] = p; 
              } 
          }
          for(;p<=n;p++){
              if(factors[p] == 0) 
              { 
                  factors[p] = p;
                  ar.add(p);
              } 
          }
      }
  	
  	
  	static int binarySearch(int arr[], int x)
      {
          int l = 0, r = arr.length - 1;
          while (l <= r) {
              int m = l + (r - l) / 2;
    
              // Check if x is present at mid
              if (arr[m] == x)
                  return m;
    
              // If x greater, ignore left half
              if (arr[m] < x)
                  l = m + 1;
    
              // If x is smaller, ignore right half
              else
                  r = m - 1;
          }
    
          // if we reach here, then element was
          // not present
          return -1;
      }
  	
  	
  	public static int getFreq(int arr[], int n) {
  		int k=0;
  		for(int i=0;i<arr.length;i++) {
  			if(arr[i]==n)
  				k++;
  		}
  		return k;
  	}
  	
  	public static void primeFactors(int n)
      {
          // Print the number of 2s that divide n
  		HashMap<Integer, Integer> hm=new HashMap<Integer, Integer>();
          while (n%2==0)
          {
              hm.put(2, hm.getOrDefault(2, 0)+1);
              n/=2;
          }
   
          // n must be odd at this point.  So we can
          // skip one element (Note i = i +2)
          for (int i = 3; i <= Math.sqrt(n); i+= 2)
          {
              // While i divides n, print i and divide n
              while (n%i == 0)
              {
                  n /= i;
                  hm.put(i, hm.getOrDefault(i, 0)+1);
              }
          }
   
          // This condition is to handle the case whien
          // n is a prime number greater than 2
          if (n > 2)
          	hm.put(n, hm.getOrDefault(n, 0)+1);
      }
  
  
  	
  	static boolean isPartition(int arr[], int n) {
  		int sum = 0;
  	    int i, j;
  	 
  	    // Calculate sum of all elements
  	    for (i = 0; i < n; i++)
  	        sum += arr[i];
  	 
  	    if (sum % 2 != 0)
  	        return false;
  	 
  	    boolean part[][]=new boolean[sum/2+1][n+1];
  	 
  	    // initialize top row as true
  	    for (i = 0; i <= n; i++)
  	        part[0][i] = true;
  	 
  	    // initialize leftmost column, except part[0][0], as false
  	    for (i = 1; i <= sum / 2; i++)
  	        part[i][0] = false;
  	 
  	    // Fill the partition table in bottom up manner
  	    for (i = 1; i <= sum / 2; i++) {
  	        for (j = 1; j <= n; j++) {
  	            part[i][j] = part[i][j - 1];
  	            if (i >= arr[j - 1])
  	                part[i][j] = part[i][j]
  	                             || part[i - arr[j - 1]][j - 1];
  	        }
  	    }
  	    return part[sum / 2][n];
  	}
  	
  	static int setBit(int S, int j) { return S | 1 << j; }
  
  	static int clearBit(int S, int j) { return S & ~(1 << j); }
  
  	static int toggleBit(int S, int j) { return S ^ 1 << j; }
  
  	static boolean isOn(int S, int j) { return (S & 1 << j) != 0; }
  
  	static int turnOnLastZero(int S) { return S | S + 1; }
  	
  	static int turnOnLastConsecutiveZeroes(int S) { return S | S - 1; }
  	
  	static int turnOffLastBit(int S) { return S & S - 1; }
  	
  	static int turnOffLastConsecutiveBits(int S) { return S & S + 1; }
  
  	static int lowBit(int S) { return S & -S; }
  
  	static int setAll(int N) { return (1 << N) - 1; }
  
  	static int modulo(int S, int N) { return (S & N - 1); }		 //S%N, N is a power of 2
  
  	static boolean isPowerOfTwo(int S) { return (S & S - 1) == 0; }
  	
  	static boolean isWithin(long x, long y, long d, long k) {
  		return x*k*x*k + y*k*y*k <= d*d;
  	}
  	
  	static long modFact(long n, 
              long p) 
  	{ 
  		if (n >= p) 
  		 return 0; 
  		
  		long result = 1; 
  		for (int i = 1; i <= n; i++) 
  		 result = (result * i) % p; 
  		
  		return result; 
  	} 
  	static int sum(int[] arr, int n)
  	{
  	    int inc[]=new int[n+1];
  	    int dec[]=new int[n+1];
  	    inc[0] = arr[0];
  	    dec[0] = arr[0];
  
  	    for (int i = 1; i < n; i++) {
  	        for (int j = 0; j < i; j++) {
  	            if (arr[j] > arr[i]) {
  	                dec[i] = max(dec[i], inc[j] + arr[i]);
  	            }
  	            else if (arr[i] > arr[j]) {
  	                inc[i] = max(inc[i], dec[j] + arr[i]);
  	            }
  	        }
  	    }
  	    return max(inc[n - 1], dec[n - 1]);
  	}
  	static long nc2(long a) {
  		return a*(a-1)/2;
  	}
  	public static int numberOfprimeFactors(int n) 
      { 
          // Print the number of 2s that divide n 
  		HashSet<Integer> hs = new HashSet<Integer>();
          while (n%2==0) 
          { 
              hs.add(2);
              n /= 2; 
          } 
    
          // n must be odd at this point.  So we can 
          // skip one element (Note i = i +2) 
          for (int i = 3; i <= Math.sqrt(n); i+= 2) 
          { 
              // While i divides n, print i and divide n 
              while (n%i == 0) 
              { 
                  hs.add(i);
                  n /= i; 
              } 
          } 
    
          // This condition is to handle the case whien 
          // n is a prime number greater than 2 
          if (n > 2) 
              hs.add(n);
          return hs.size();
      } 
  	static long gcd(long a, long b) 
      { 
        if (b == 0) 
          return a; 
        return gcd(b, a % b);  
      } 
  
  
  
  
  	
  
  	static void reverse(int arr[],int start, int end) 
  	{ 
  		int temp; 
  		   
  		while (start < end) 
  		{ 
  		    temp = arr[start];  
  		    arr[start] = arr[end]; 
  		    arr[end] = temp; 
  		    start++; 
  		    end--; 
  		}  
  	}  
  	static void reverse(long arr[],int start, int end) 
  	{ 
  		long temp; 
  		   
  		while (start < end) 
  		{ 
  		    temp = arr[start];  
  		    arr[start] = arr[end]; 
  		    arr[end] = temp; 
  		    start++; 
  		    end--; 
  		}  
  	}  
  	static boolean isPrime(int n) 
      { 
          // Corner cases 
          if (n <= 1) return false; 
          if (n <= 3) return true; 
        
          // This is checked so that we can skip  
          // middle five numbers in below loop 
          if (n % 2 == 0 || n % 3 == 0) return false; 
        
          for (int i = 5; i * i <= n; i = i + 6) 
              if (n % i == 0 || n % (i + 2) == 0) 
              return false; 
        
          return true; 
      } 
  	static int p2(int n) {
  		int k=0;
  		while(n>1) {
  			if(n%2!=0)
  				return k;
  			n/=2;
  			k++;
  		}
  		return k;
  	}
  	static boolean isp2(int n) {
  		while(n>1) {
  			if(n%2==1)
  				return false;
  			n/=2;
  		}
  		return true;
  	}
  	static int binarySearch(int arr[], int first, int last, int key){  
  		   int mid = (first + last)/2;  
  		   while( first <= last ){  
  		      if ( arr[mid] < key ){  
  		        first = mid + 1;     
  		      }else if ( arr[mid] == key ){  
  		        return mid;  
  		      }else{  
  		         last = mid - 1;  
  		      }  
  		      mid = (first + last)/2;  
  		   }  
  		   return -1;
  		 }  
  
  	static void print(int a[][]) {
  		for(int i=0;i<a.length;i++)
  		{
  			for(int j=0;j<a[0].length;j++)
  				System.out.print(a[i][j]+" ");
  			System.out.println();
  		}
  	}
  	static int max (int x, int y) {
  		return (x > y)? x : y;
  	} 
  	static int search(Pair[] p, Pair pair) {
  		int l=0, r=p.length;
  		while (l <= r) { 
  	        int m = l + (r - l) / 2;
  	        if (p[m].compareTo(pair)==0) 
  	            return m; 
  	        if (p[m].compareTo(pair)<0) 
  	            l = m + 1; 
  	        else
  	            r = m - 1;
  	    }
  	    return -1;
  	}
  	static void pa(int a[])
  	{
  		for(int i=0;i<a.length;i++)
  			System.out.print(a[i]+" ");
  		System.out.println();
  		
  	}
  	static void pa(long a[])
  	{
  		for(int i=0;i<a.length;i++)
  			System.out.print(a[i]+" ");
  		System.out.println();
  		
  	}
  	static void reverseArray(int arr[], 
              int start, int end) 
  	{ 
  		int temp; 
  		   
  		while (start < end) 
  		{ 
  		    temp = arr[start];  
  		    arr[start] = arr[end]; 
  		    arr[end] = temp; 
  		    start++; 
  		    end--; 
  		}  
  	}     
  	static boolean isPalindrome(String s) {
  		int l=s.length();
  		for(int i=0;i<l/2;i++)
  		{
  			if(s.charAt(i)!=s.charAt(l-i-1))
  				return false;
  		}
  		return true;
  	}
  	static long nc2(long n, long m) {
  		return (n*(n-1)/2)%m;
  	}
  	static long c(long a) {
  		return a*(a+1)/2;
  	}
  	static int next(int[] arr, int 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;  
      } 
  	
  	static int prev(int[] arr, int target)  
      {  
          int start = 0, end = arr.length - 1;  
      
          int ans = -1;  
          while (start <= end) {  
              int mid = (start + end) / 2;  
      
              // Move to left side if target is  
              // lesser.  
              if (arr[mid] > target) {
                  end = mid - 1;  
              }  
      
              // Move right side.  
              else {  
                  ans = mid;  
                  start = mid + 1;  
              }  
          }  
          return ans;  
      } 
  
  	static long power(long x, long y, long p) 
      { 
          long res = 1; 
          x = x % p; 
                        
          while (y > 0) 
          { 
              if (y % 2 == 1) 
                  res = (res * x) % p; 
              y = y >> 1;
              x = (x * x) % p; 
          } 
          return  res; 
      } 
      static long modInverse(long n, long p) 
      { 
          return power(n, p-2, p); 
      } 
      static long nCrModP(long n, long r, 
                                      long p) 
      { 
          if(r>n)
          	return 0;
      	if (r == 0) 
              return 1; 
          long[] fac = new long[(int) (n+1)]; 
          fac[0] = 1; 
          for (int i = 1 ;i <= n; i++) 
              fac[i] = fac[i-1] * i % p;
          return (fac[(int) n]* modInverse(fac[(int) r], p) 
                  % p * modInverse(fac[(int) (n-r)], p) 
                                      % p) % p; 
      } 
  	static String reverse(String str)
  	{
  		return new StringBuffer(str).reverse().toString();
  	}
  
  
  
  	static long fastpow(long x, long y, long m)  
      { 
          if (y == 0) 
              return 1; 
            
          long p = fastpow(x, y / 2, m) % m; 
          p = (p * p) % m; 
        
          if (y % 2 == 0) 
              return p; 
          else
              return (x * p) % m; 
      } 
  	
  	static boolean isPerfectSquare(long l)
  	{
  		return Math.pow((long)Math.sqrt(l),2)==l;
  	}
  	
  	
  	static void merge(long[] arr, int l, int m, int r) 
      { 
          // Find sizes of two subarrays to be merged 
          int n1 = m - l + 1; 
          int n2 = r - m; 
    
          /* Create temp arrays */
          long L[] = new long [n1]; 
          long R[] = new long [n2]; 
    
          /*Copy data to temp arrays*/
          for (int i=0; i<n1; ++i) 
              L[i] = arr[l + i]; 
          for (int j=0; j<n2; ++j) 
              R[j] = arr[m + 1+ j]; 
    
    
          /* Merge the temp arrays */
    
          // Initial indexes of first and second subarrays 
          int i = 0, j = 0; 
    
          // Initial index of merged subarry array 
          int k = l; 
          while (i < n1 && j < n2) 
          { 
              if (L[i] <= R[j]) 
              { 
                  arr[k] = L[i]; 
                  i++; 
              } 
              else
              { 
                  arr[k] = R[j]; 
                  j++; 
              } 
              k++; 
          } 
    
          /* Copy remaining elements of L[] if any */
          while (i < n1) 
          { 
              arr[k] = L[i]; 
              i++; 
              k++; 
          } 
    
          /* Copy remaining elements of R[] if any */
          while (j < n2) 
          { 
              arr[k] = R[j]; 
              j++; 
              k++; 
          } 
      } 
    
      // Main function that sorts arr[l..r] using 
      // merge() 
      static void sort(int arr[], int l, int r) 
      { 
          if (l < r) 
          { 
              // Find the middle point 
              int m = (l+r)/2; 
    
              // Sort first and second halves 
              sort(arr, l, m); 
              sort(arr , m+1, r); 
    
              // Merge the sorted halves 
              merge(arr, l, m, r); 
          } 
      } 
  	static void merge(int arr[], int l, int m, int r) 
      { 
          // Find sizes of two subarrays to be merged 
          int n1 = m - l + 1; 
          int n2 = r - m; 
    
          /* Create temp arrays */
          int L[] = new int [n1]; 
          int R[] = new int [n2]; 
    
          /*Copy data to temp arrays*/
          for (int i=0; i<n1; ++i) 
              L[i] = arr[l + i]; 
          for (int j=0; j<n2; ++j) 
              R[j] = arr[m + 1+ j]; 
    
    
          /* Merge the temp arrays */
    
          // Initial indexes of first and second subarrays 
          int i = 0, j = 0; 
    
          // Initial index of merged subarry array 
          int k = l; 
          while (i < n1 && j < n2) 
          { 
              if (L[i] <= R[j]) 
              { 
                  arr[k] = L[i]; 
                  i++; 
              } 
              else
              { 
                  arr[k] = R[j]; 
                  j++; 
              } 
              k++; 
          } 
    
          /* Copy remaining elements of L[] if any */
          while (i < n1) 
          { 
              arr[k] = L[i]; 
              i++; 
              k++; 
          } 
    
          /* Copy remaining elements of R[] if any */
          while (j < n2) 
          { 
              arr[k] = R[j]; 
              j++; 
              k++; 
          } 
      } 
    
      // Main function that sorts arr[l..r] using 
      // merge() 
      static void sort(long arr[], int l, int r) 
      { 
          if (l < r) 
          { 
              // Find the middle point 
              int m = (l+r)/2; 
    
              // Sort first and second halves 
              sort(arr, l, m); 
              sort(arr , m+1, r); 
    
              // Merge the sorted halves 
              merge(arr, l, m, r); 
          } 
      } 
      static class Pair implements Comparable<Pair>{
          int a;
          int b;
          Pair(int a,int b){
              this.a=a;
   
              this.b=b;
          }   
          public int compareTo(Pair p){
              if(a>p.a)
                  return 1;
              if(a==p.a)
                  return (b-p.b);
              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 readLine() throws IOException 
          { 
              byte[] buf = new byte[128]; // 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 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(); 
          } 
      } 
  }