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 Practice
  {
  //	static final long mod=7420738134811L;
  	static int mod=1000000007;
  	static final int size=1001;
  	static FastReader sc=new FastReader(System.in);
  //	static Reader sc=new Reader();
  	static  PrintWriter out=new PrintWriter(System.out);
  	static long[] factorialNumInverse;
  	static long[] naturalNumInverse;	
  	static long[] fact;
  	int abc;
  	public static void main(String[] args) throws IOException
  	{
  //		System.setIn(new FileInputStream("input.txt"));
  //		System.setOut(new PrintStream("output.txt"));
  //		naturalNumInverse=new long[size];
  //		factorialNumInverse=new long[size];
  //		fact=new long[size];
  //		factorial(mod);
  //		InverseofNumber(mod);
  //		InverseofFactorial(mod);
  		int t=1;
  //		t=sc.nextInt();
  		while(t-->0)
  		{
  			solve();
  //			sb.append("\n");
  		}
  //		out.print(sb);
  		out.close();
  		out.flush();
  	}
  	static void solve() throws IOException
  	{
  		int n=sc.nextInt();
  		TreeSet<Long> arr=new TreeSet<Long>();
  		long total=0;
  		for(int i=0;i<n;i++)
  		{
  			long x=sc.nextLong();
  			arr.add(x);
  			total+=x;
  		}
  		int m=sc.nextInt();
  		while(m-->0)
  		{
  			long def=sc.nextLong(),att=sc.nextLong();
  			Long lower=arr.floor(def);
  			Long upper=arr.ceiling(def);
  			long ans=Long.MAX_VALUE,sum=total;
  			if(lower!=null)
  			{
  				long temp=def-lower;
  				sum-=lower;
  				if(sum>=att)
  					ans=temp;
  				else
  					ans=att-sum+temp;
  			}
  			if(upper!=null)
  			{
  				sum=total;
  				sum-=upper;
  				if(sum>=att)
  					ans=0;
  				else
  					ans=Math.min(ans, att-sum);
  			}
  			out.println(ans);
  		}
  	}
  //	{
  //		int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),m=sc.nextInt();
  //		if(m>(a-1+b-1+c-1))
  //		{
  //			out.println("NO");return;
  //		}
  //		int A=Math.min(a, Math.min(c, b));
  //		int C=Math.max(a, Math.max(c, b));
  //		int B=(a+b+c)-(A+C);
  //		int rem=C-B-1;
  //		if(A<rem)
  //		{
  //			out.println("NO");return;
  //		}
  //		out.println("YES");
  //	}
  	static class Pair implements Cloneable
  	{
  		int x,y;
  		Pair(int a,int b)
  		{
  			this.x=a;
  			this.y=b;
  		}
  		@Override
  		public String toString() 
  		{
  			return "("+x+","+(y)+")";
  		}
  		@Override
  		protected Pair clone() throws CloneNotSupportedException {
  			return new Pair(this.x,this.y);
  		}
  	}
  	static class Tuple implements Cloneable
  	{
  		int x,y,z;
  		Tuple(int a,int b,int c)
  		{
  			this.x=a;
  			this.y=b;
  			this.z=c;
  		}
  		@Override
  		public String toString() 
  		{
  			return "("+x+","+y+","+z+")";
  		}
  		@Override
  		protected Tuple clone() throws CloneNotSupportedException {
  			return new Tuple(this.x,this.y,this.z);
  		}
  	}
  	static void arraySort(int arr[])
  	{
  		ArrayList<Integer> a=new ArrayList<Integer>();
  		for (int i = 0; i < arr.length; i++) {
  			a.add(arr[i]);
  		}
  		Collections.sort(a);
  		for (int i = 0; i < arr.length; i++) {
  			arr[i]=a.get(i);
  		}
  	}
  	static void arraySort(long arr[])
  	{
  		ArrayList<Long> a=new ArrayList<Long>();
  		for (int i = 0; i < arr.length; i++) {
  			a.add(arr[i]);
  		}
  		Collections.sort(a);
  		for (int i = 0; i < arr.length; i++) {
  			arr[i]=a.get(i);
  		}
  	}
  	public static void InverseofNumber(int p)
  	{
  	    naturalNumInverse[0] = naturalNumInverse[1] = 1;
  	    for(int i = 2; i < size; i++)
  	        naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p;
  	}
  	// Function to precompute inverse of factorials
  	public static void InverseofFactorial(int p)
  	{
  	    factorialNumInverse[0] = factorialNumInverse[1] = 1;
  	    // pre-compute inverse of natural numbers
  	    for(int i = 2; i < size; i++)
  	        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
  	}
  	// Function to calculate factorial of 1 to 200001
  	public static void factorial(int p)
  	{
  	    fact[0] = 1;
  	    for(int i = 1; i < size; i++)
  	        fact[i] = (fact[i - 1] * (long)i) % p;
  	}
  	 
  	// Function to return nCr % p in O(1) time
  	public static long Binomial(int N, int R, int p)
  	{
  	     
  	    // n C r = n!*inverse(r!)*inverse((n-r)!)
  	    long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;
  	     
  	    return ans;
  	}
  	static int findXOR(int x)	//from 0 to x
  	{
  		if(x<0)
  			return 0;
  		if(x%4==0)
  			return x;
  		if(x%4==1)
  			return 1;
  		if(x%4==2)
  			return x+1;
  		return 0;
  	}
  	static boolean isPrime(long x)
  	{
  		if(x==1)
  			return false;
  		if(x<=3)
  			return true;
  		if(x%2==0 || x%3==0)
  			return false;
  		for(int i=5;i<=Math.sqrt(x);i+=2)
  			if(x%i==0)
  				return false;
  		return true;
  	}
  	static long gcd(long a,long b)
  	{
  		return (b==0)?a:gcd(b,a%b);
  	}
  	static int gcd(int a,int b)
  	{
  		return (b==0)?a:gcd(b,a%b);
  	}
  	static class UnionFind
  	{
  		  private int size;
  		  private int[] sz;
  		  private int[] parent;
  		  private int numComponents;
  		  public UnionFind(int size) 
  		  {
  		    if (size <= 0) throw new IllegalArgumentException("Size <= 0 is not allowed");
  		    this.size = numComponents = size;
  		    sz = new int[size];
  		    parent = new int[size];
  		    for (int i = 0; i < size; i++) 
  		    {
  		      parent[i] = i; 									// Link to itself (self root)
  		      sz[i] = 1;								// Each component is originally of size one
  		    }
  		  }
  		  public int find(int p) 
  		  {
  		    int root = p;
  		    while (root != parent[root])
  		    	root = parent[root];
  		    while (p != root)
  		    {
  		      int next = parent[p];
  		      parent[p] = root;
  		      p = next;
  		    }
  		    return root;
  		  }
  		  public void makeParent(int p,int u)
  		  {
  			  parent[p]=u;
  		  }
  		  public boolean isConnected(int p, int q) 
  		  {
  		    return find(p) == find(q);
  		  }
  		  public int componentSize(int x) 
  		  {
  		    return sz[find(x)];
  		  }
  		  public int size() 
  		  {
  		    return size;
  		  }
  		  public int components() 
  		  {
  		    return numComponents;
  		  }
  		  public void unify(int p, int q) 
  		  {
  		    int root1 = find(p);
  		    int root2 = find(q);
  		    if(root1==root2)
  		    {
  		    	return;
  		    }
  		    else if (sz[root1] < sz[root2]) 
  		    {
  		    	parent[root1] = root2;
  		    	sz[root2] += sz[root1];
  		    	sz[root1] = 0;
  		  } 
  		    else 
  		    {
  		    	parent[root2] = root1;
  		    	sz[root1] += sz[root2];
  		    	sz[root2] = 0;
  		    }
  		    numComponents--;
  		  }
  		  void calc()
  		  {
  			  for(int i=0;i<size;i++)
  				  find(i);
  			  
  		  }
  	}
  	static class Node
  	{
  		int vertex,dp;
  		ArrayList<Edge> adj;
  		ArrayList<Node> parent;
  		Node(int v)
  		{
  			vertex=v;
  			adj=new ArrayList<Edge>();
  			parent=new ArrayList<Node>();
  			dp=0;
  		}
  		@Override
  		public String toString() 
  		{
  			return vertex+" ";
  		}
  	}
  	static class Edge
  	{
  		Node to;
  		int cost;
  		Edge(Node t,int c)
  		{
  			this.to=t;
  			this.cost=c;
  		}
  		@Override
  		public String toString() {
  			return "("+to.vertex+","+cost+") ";
  		}
  	}
  	static long power(long x, long y)
  	  {
  	    long res = 1; 
  	    x = x % mod; 
  	    if (x == 0)
  	      return 0; 
  	    while (y > 0)
  	    {
  	    	if ((y & 1) != 0)
  	    		res = (res * x) % mod;
  	    	
  	    	y = y >> 1; // y = y/2
  	    	x = (x * x) % mod;
  	    }
  	    return res;
  	  }
  	static long binomialCoeff(long n, long k)
      {
          long res = 1;
  //        if(k>n)
  //        	return 0;
          // Since C(n, k) = C(n, n-k)
          if (k > n - k)
              k = n - k;
   
          // Calculate value of
          // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
          for (long i = 0; i < k; ++i) {
              res *= (n - i);
              res /= (i + 1);
          }
   
          return res;
      }
  	static class FastReader
  	{
          byte[] buf = new byte[2048];
          int index, total;
          InputStream in;
   
          FastReader(InputStream is) 
          {
              in = is;
          }
   
          int scan() throws IOException 
          {
              if (index >= total) {
                  index = 0;
                  total = in.read(buf);
                  if (total <= 0) {
                      return -1;
                  }
              }
              return buf[index++];
          }
   
          String next() throws IOException 
          {
              int c;
              for (c = scan(); c <= 32; c = scan());
              StringBuilder sb = new StringBuilder();
              for (; c > 32; c = scan()) {
                  sb.append((char) c);
              }
              return sb.toString();
          }
   
          int nextInt() throws IOException 
          {
              int c, val = 0;
              for (c = scan(); c <= 32; c = scan());
              boolean neg = c == '-';
              if (c == '-' || c == '+') {
                  c = scan();
              }
              for (; c >= '0' && c <= '9'; c = scan()) {
                  val = (val << 3) + (val << 1) + (c & 15);
              }
              return neg ? -val : val;
          }
   
          long nextLong() throws IOException 
          {
              int c;
              long val = 0;
              for (c = scan(); c <= 32; c = scan());
              boolean neg = c == '-';
              if (c == '-' || c == '+') {
                  c = scan();
              }
              for (; c >= '0' && c <= '9'; c = scan()) {
                  val = (val << 3) + (val << 1) + (c & 15);
              }
              return neg ? -val : val;
          }
      }
  	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[64]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n') {
                      if (cnt != 0) {
                          break;
                      }
                      else {
                          continue;
                      }
                  }
                  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();
          }
          public void printarray(int arr[])
          {
          	for (int i = 0; i < arr.length; i++)
  				System.out.print(arr[i]+" ");
          	System.out.println();
          }
      }
  }
  //
  //int n=sc.nextInt(),m=sc.nextInt();
  //ArrayList<Pair> arr=new ArrayList<Pair>();
  //for(int i=0;i<n*m;i++)
  //	arr.add(new Pair(i,sc.nextInt()));
  //Collections.sort(arr,new Comparator<Pair>() {
  //	@Override
  //	public int compare(Practice.Pair a, Practice.Pair b) {
  //		int sightdiff=a.y-b.y;
  //		if(sightdiff!=0)
  //			return sightdiff;
  //		else
  //			return b.x-a.x;
  //	}
  //});
  //int pos[]=new int[n*m];
  //for(int i=0;i<n*m;i++)
  //	pos[arr.get(i).x]=i;
  //out.println(arr);
  //out.println(Arrays.toString(pos));
  //segment help=new segment(n*m);
  //long ans=0;
  //for(int i=0;i<n*m;i++)
  //{
  //	int start=(pos[i]/m)*m;
  //	int incov=help.find(start,pos[i]-1);
  ////	out.println(start+" "+arr.get(i).x+" "+incov);
  //	ans+=incov;
  //	help.doUP(pos[i]);
  //}
  //out.println(ans);
  
  
  
  
  
  
  
  