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: //package codechef;
  
  import java.io.DataInputStream;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Map;
  import java.util.Set;
  import java.util.Stack;
  import java.util.TreeMap;
  
  
  
  
  
  
  public class cp_2 {
  	static int mod=(int)1e9+7;
  	
  		//static  Reader sc=new Reader();
  	   static FastReader sc=new FastReader(System.in);
  	 
  	
  	public static void main(String[] args) throws IOException {
  		// TODO Auto-generated method stub
  		//FastReader sc=new FastReader();
  			//int t=sc.nextInt();
  		int t=1;
  			while(t-->0)
  			{
  				int n=sc.nextInt();
  				long arr[]=new long[n];
  				long sum=0;
  				for (int i = 0; i < arr.length; i++) {
  					arr[i]=sc.nextLong();
  					sum+=arr[i];
  				}
  				sort(arr);
  				int m=sc.nextInt();
  				long dragon[][]=new long[m][2];
  				for (int i = 0; i < m; i++) {
  					dragon[i][0]=sc.nextLong();
  					dragon[i][1]=sc.nextLong();
  				}
  				
  				for(int i=0;i<m;i++)
  				{
  					int first=upperIndex(arr, n, dragon[i][0]);
  					if (first==-1) {
  						first=0;
  					}
  					long coins=0;
  					//out.println(arr[first]);
  					if(first<n-1)
  					{
  						long coins1=0;
  						long coins2=0;
  						if(arr[first]<dragon[i][0])
  						{
  							coins1+=dragon[i][0]-arr[first];
  						}
  						
  						if (sum-arr[first]<dragon[i][1]) {
  							coins1+=dragon[i][1]-sum+arr[first];
  						}
  						
  						first++;
  						if(arr[first]<dragon[i][0])
  						{
  							coins2+=dragon[i][0]-arr[first];
  						}
  						
  						if (sum-arr[first]<dragon[i][1]) {
  							coins2+=dragon[i][1]-sum+arr[first];
  						}
  						coins=Math.min(coins1, coins2);
  					}
  					else {
  						if(arr[first]<dragon[i][0])
  						{
  							coins+=dragon[i][0]-arr[first];
  						}
  						
  						if (sum-arr[first]<dragon[i][1]) {
  							coins+=dragon[i][1]-sum+arr[first];
  						}
  					}
  					
  					
  					out.println(coins);
  					
  				}
  				
  				
  			}
  			
  			out.flush();
  			out.close();
  			System.gc();
  	
  	}
  	
  	
  	
  
  
  	/*
  	   ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE...
   */
  	
  	
  	
  	
  	
  	static ArrayList<Integer> primes;
  	static boolean prime[];
  	static void sieveOfEratosthenes(int n)
      {
          // Create a boolean array
          // "prime[0..n]" and
          // initialize all entries
          // it as true. A value in
          // prime[i] will finally be
          // false if i is Not a
          // prime, else true.
         prime= new boolean[n + 1];
          for (int i = 2; i <= n; i++)
              prime[i] = true;
          
          for (int p = 2; p * p <= n; p++)
          {
              // If prime[p] is not changed, then it is a
              // prime
              if (prime[p] == true)
              {
                  // Update all multiples of p
                  for (int i = p * p; i <= n; i += p)
                      prime[i] = false;
              }
          }
   
          // Print all prime numbers
          for (int i = 2; i <= n; i++)
          {
              if (prime[i] == true)
                 primes.add(i);
          }
      }
  	
  	
  	
  	static long mod(long a, long b) {
  		  long c = a % b;
  		  return (c < 0) ? c + b : c;
  		}
  	
  	static void swap(int arr[],int i,int j)
  	{
  		int temp=arr[i];
  		arr[i]=arr[j];
  		arr[j]=temp;
  	}
  
  	
  	
  	static boolean util(int a,int b,int c)
  	{
  		if(b>a)util(b, a, c);
  		
  		while(c>=a)
  		{
  			c-=a;
  			if(c%b==0)
  				return true;
  		}
  		
  		
  		return (c%b==0);
  	}
  	
  	static boolean check(char arr[][], int i,int j)
  	{
  		int cnt=0;
  		for(int k=i;k<i+2;k++)
  		{
  			for(int l=j;l<j+2;l++)
  			{
  				if(arr[k][l]=='#')
  					cnt++;
  			}
  		}
  		
  		if(cnt!=2)
  			return true;
  		return false;
  		
  		
  	}
  	
  	static void flag(boolean flag)
  	   {
  	       out.println(flag ? "YES" : "NO");
  	       out.flush();
  	   }
  	   
  	static ArrayList<Long> luckNums;
  	static void luckyNum(long x,long p10)
  	{
  		luckNums.add(x);
  		if(x>(long)1e10)
  			return;
  		luckyNum(x+4*p10, p10*10);
  		luckyNum(x+7*p10, p10*10);
  		
  	}
  	
  	 /*                     
  	    Map<Long,Long> map=new HashMap<>();
  	             for(int i=0;i<n;i++)
  	               {
  	                  if(!map.containsKey(a[i]))
  	                   map.put(a[i],1);
  	                 else
  	                   map.replace(a[i],map.get(a[i])+1);
  	               }
  	     
  	    Set<Map.Entry<Long,Long>> hmap=map.entrySet();
  	             for(Map.Entry<Long,Long> data : hmap)
  	               {
  	          
  	               }
  	       
  	   Iterator<Integer> it = set.iterator();
  	          while(it.hasNext()) 
  	           { 
  	            int x=it.next();
  	           }
  	   */
  
  
  	
  	
  	static void print(int a[])
  	  {
  	     int n=a.length;
  	     for(int i=0;i<n;i++)
  	       {
  	          out.print(a[i]+" ");
  	       }
  	     out.println();
  	     out.flush();
  	  }
  	static void print(long a[])
  	  {
  	     int n=a.length;
  	     for(int i=0;i<n;i++)
  	       {
  	          out.print(a[i]+" ");
  	       }
  	     out.println();
  	     out.flush();
  	  } 
  	static void print_int(ArrayList<Integer> al)
  	  {
  	     int si=al.size();
  	     for(int i=0;i<si;i++)
  	       {
  	          out.print(al.get(i)+" ");
  	       }
  	     out.println();
  	     out.flush();
  	  }
  	static void print_long(ArrayList<Long> al)
  	  {
  	     int si=al.size();
  	     for(int i=0;i<si;i++)
  	       {
  	          out.print(al.get(i)+" ");
  	       }
  	     out.println();
  	     out.flush();
  	  }
  	  
  	static int LowerBound(int a[], int x) 
  	 { // x is the target value or key
  	  int l=-1,r=a.length;
  	  while(l+1<r) {
  	    int m=(l+r)>>>1;
  	    if(a[m]>=x) r=m;
  	    else l=m;
  	  }
  	  return r;
  	}
  	
  	
  	static int lowerIndex(int arr[], int n, int x)
      {
          int l = 0, h = n - 1;
          while (l <= h)
          {
              int mid = (l + h) / 2;
              if (arr[mid] >= x)
                  h = mid - 1;
              else
                  l = mid + 1;
          }
          return l;
      }
  	
  	
  	    // function to find last index <= y
      static int upperIndex(int arr[], int n, int y)
      {
          int l = 0, h = n - 1;
          while (l <= h)
          {
              int mid = (l + h) / 2;
              if (arr[mid] <= y)
                  l = mid + 1;
              else
                  h = mid - 1;
          }
          return h;
      }
      static int upperIndex(long arr[], int n, long y)
      {
      	int l = 0, h = n - 1;
      	while (l <= h)
      	{
      		int mid = (l + h) / 2;
      		if (arr[mid] <= y)
      			l = mid + 1;
      		else
      			h = mid - 1;
      	}
      	return h;
      }
       
  	
  	 static int UpperBound(int a[], int x)
  	  {// x is the key or target value
  	    int l=-1,r=a.length;
  	    while(l+1<r) {
  	       int m=(l+r)>>>1;
  	       if(a[m]<=x) l=m;
  	       else r=m;
  	    }
  	    return l+1;
  	 }
  	 static int UpperBound(long a[], long x)
  	 {// x is the key or target value
  		 int l=-1,r=a.length;
  		 while(l+1<r) {
  			 int m=(l+r)>>>1;
  	       if(a[m]<=x) l=m;
  	       else r=m;
  		 }
  		 return l+1;
  	 }
  	 
  	 
  	 
  	 static class DisjointUnionSets
  	 {
  	    int[] rank, parent;
  	    int n;
  	    // Constructor
  	    public DisjointUnionSets(int n)
  	    {
  	        rank = new int[n];
  	        parent = new int[n];
  	        this.n = n;
  	        makeSet();
  	    }
  	    // Creates n sets with single item in each
  	    void makeSet()
  	    {
  	        for (int i = 0; i < n; i++)
  	            parent[i] = i;
  	    }
  	    int find(int x)
  	    {
  	        if (parent[x] != x) {
  	            parent[x] = find(parent[x]);
  	        }
  	        return parent[x];
  	    }
  	    // Unites the set that includes x and the set
  	    // that includes x
  	    void union(int x, int y)
  	    {
  	        int xRoot = find(x), yRoot = find(y);
  	        if (xRoot == yRoot)
  	            return;
  	        if (rank[xRoot] < rank[yRoot])
  	            parent[xRoot] = yRoot;
  	        else if (rank[yRoot] < rank[xRoot])
  	         parent[yRoot] = xRoot;
  	        else // if ranks are the same
  	        {
  	         parent[yRoot] = xRoot;
  	            rank[xRoot] = rank[xRoot] + 1;
  	        }
  	    }
  	 }
  
  	 
  //	static class countClass{
  //		int cnt;
  //		public countClass() {
  //			this.cnt=0;
  //			// TODO Auto-generated constructor stub
  //		}
  //	}
  
  	 
  	 
  	static class Graph
  	  {
  	        int v;
  	        ArrayList<Integer> list[];
  	        Graph(int v)
  	        {
  	            this.v=v;
  	            list=new ArrayList[v+1];
  	            for(int i=1;i<=v;i++)
  	                list[i]=new ArrayList<Integer>();
  	        }
  	        void addEdge(int a, int b)
  	        {
  	            this.list[a].add(b);
  	        }
  	    }
  	
  //	static class GraphMap{
  //		Map<String,ArrayList<String>> graph;
  //		GraphMap() {
  //			// TODO Auto-generated constructor stub
  //			graph=new HashMap<String,ArrayList<String>>();
  //			
  //		}
  //		void addEdge(String a,String b)
  //		{
  //			if(graph.containsKey(a))
  //			this.graph.get(a).add(b);
  //			else {
  //				this.graph.put(a, new ArrayList<>());
  //				this.graph.get(a).add(b);
  //			}
  //		}
  //	}
  //	static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok)
  //	{
  //		vis.add(src);
  //		
  //		if(g.graph.get(src)!=null)
  //		{
  //			for(String each:g.graph.get(src))
  //			{
  //				if(!vis.contains(each))
  //				{
  //					dfsMap(g, vis, each, ok+1);
  //				}
  //			}
  //		}
  //		
  //		cnt=Math.max(cnt, ok);
  //	}
  	static double sum[];
  	static long cnt;
  	static void DFS(Graph g, boolean[] visited, int u)
  		{
  	        visited[u]=true;
  	        
  	        for(int i=0;i<g.list[u].size();i++)
  	        {
  	            int v=g.list[u].get(i);
  	            
  	            if(!visited[v])
  	            {
  	            	cnt=cnt*2;
  	            	DFS(g, visited, v);
  	            
  	            }
  	            
  	        }
  	       
  
  	  } 
  	
  	
  	  
  	 static class Pair
  	    {
  	       int x,y;
  	       Pair(int x,int y)
  	        {
  	           this.x=x;
  	           this.y=y;
  	        }
  	       
  	       
  	       
  	    }
  	   
  	static long sum_array(int a[])
  	 {
  	    int n=a.length;
  	    long sum=0;
  	    for(int i=0;i<n;i++)
  	     sum+=a[i];
  	    return sum;
  	 }
  	static long sum_array(long a[])
  	 {
  	    int n=a.length;
  	    long sum=0;
  	    for(int i=0;i<n;i++)
  	     sum+=a[i];
  	    return sum;
  	 }
  
  	 static void sort(int[] a) 
  	   {
  			ArrayList<Integer> l=new ArrayList<>();
  			for (int i:a) l.add(i);
  			Collections.sort(l);
  			for (int i=0; i<a.length; i++) a[i]=l.get(i);
  		}
  	static void sort(long[] a) 
  	   {
  			ArrayList<Long> l=new ArrayList<>();
  			for (long i:a) l.add(i);
  			Collections.sort(l);
  			for (int i=0; i<a.length; i++) a[i]=l.get(i);
  		}
  
  	static void reverse_array(int a[])
  	 {
  	    int n=a.length;
  	    int i,t; 
  	    for (i = 0; i < n / 2; i++) { 
  	            t = a[i]; 
  	            a[i] = a[n - i - 1]; 
  	            a[n - i - 1] = t; 
  	        } 
  	 }
  	static void reverse_array(long a[])
  	 {
  	    int n=a.length;
  	    int i; long t; 
  	    for (i = 0; i < n / 2; i++) { 
  	            t = a[i]; 
  	            a[i] = a[n - i - 1]; 
  	            a[n - i - 1] = t; 
  	        } 
  	 }
  	 static long modInverse(long a, long m)
  		    {
  		        long g = gcd(a, m);
  		       
  		          return   power(a, m - 2, m);
  		        
  		    }
  	static long power(long x, long y, long m)
  		    {
  		        if (y == 0)
  		            return 1;
  		        long p = power(x, y / 2, m) % m;
  		        p = (int)((p * (long)p) % m);
  		        if (y % 2 == 0)
  		            return p;
  		        else
  		            return (int)((x * (long)p) % m);
  		    }
  	static long gcd(long a, long b) 
  	    { 
  			
  	        if (a == 0) 
  	            return b; 
  	         //cnt+=a/b;
  	        return gcd(b%a,a); 
  	    } 
  	static int gcd(int a, int b) 
  	    { 
  	        if (a == 0) 
  	            return b; 
  	          
  	        return gcd(b%a, a); 
  	    } 
  
  	   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') 
  	                    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(); 
  	        } 
  	    }
  	  static  PrintWriter out=new PrintWriter(System.out);
  	  static int int_max=Integer.MAX_VALUE;
  	  static int int_min=Integer.MIN_VALUE;
  	  static long long_max=Long.MAX_VALUE;
  	  static long long_min=Long.MIN_VALUE;
  
  }
  
  
  
  
  