Problem: Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.

You have an array a of length 2n and m queries on it. The i-th query is described by an integer qi. In order to perform the i-th query you must:

  * split the array into 2n - qi parts, where each part is a subarray consisting of 2qi numbers; the j-th subarray (1 ≤ j ≤ 2n - qi) should contain the elements a[(j - 1)·2qi + 1], a[(j - 1)·2qi + 2], ..., a[(j - 1)·2qi + 2qi]; 
  * reverse each of the subarrays; 
  * join them into a single array in the same order (this array becomes new array a); 
  * output the number of inversions in the new a. 



Given initial array a and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries.

Input

The first line of input contains a single integer n (0 ≤ n ≤ 20). 

The second line of input contains 2n space-separated integers a[1], a[2], ..., a[2n] (1 ≤ a[i] ≤ 109), the initial array.

The third line of input contains a single integer m (1 ≤ m ≤ 106). 

The fourth line of input contains m space-separated integers q1, q2, ..., qm (0 ≤ qi ≤ n), the queries.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

Output m lines. In the i-th line print the answer (the number of inversions) for the i-th query.

Examples

Input

2
2 1 4 3
4
1 2 0 2


Output

0
6
6
0


Input

1
1 2
3
0 1 1


Output

0
1
0

Note

If we reverse an array x[1], x[2], ..., x[n] it becomes new array y[1], y[2], ..., y[n], where y[i] = x[n - i + 1] for each i.

The number of inversions of an array x[1], x[2], ..., x[n] is the number of pairs of indices i, j such that: i < j and x[i] > x[j].

Solution (in JAVA):

Solution:
		                                import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Iterator;
 
 
 
 
 
public class Solution1 implements Runnable
{
    static final long MAX = 464897L;
    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 
        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }
        
        public int read()
        {
            if (numChars==-1) 
                throw new InputMismatchException();
            
            if (curChar >= numChars)
            {
                curChar = 0;
                try 
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }
                
                if(numChars <= 0)                
                    return -1;
            }
            return buf[curChar++];
        }
     
        public String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();
            
            while(isSpaceChar(c)) 
                c = read();
            
            int sgn = 1;
            
            if (c == '-') 
            {
                sgn = -1;
                c = read();
            }
            
            int res = 0;
            do 
            {
                if(c<'0'||c>'9') 
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c)); 
            
            return res * sgn;
        }
        
        public long nextLong() 
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') 
            {
                sgn = -1;
                c = read();
            }
            long res = 0;
            
            do 
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
                return res * sgn;
        }
        
        public double nextDouble() 
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') 
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') 
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') 
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) 
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }
        
        public String readString() 
        {
            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 boolean isSpaceChar(int c) 
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
     
        public String next() 
        {
            return readString();
        }
        
        public interface SpaceCharFilter 
        {
            public boolean isSpaceChar(int ch);
        }
    }
     
    public static void main(String args[]) throws Exception
    {
        new Thread(null, new Solution1(),"Solution",1<<26).start();
    }   
    static long gcd(long a, long b) 
    { 
      if (b == 0) 
        return a; 
      return gcd(b, a % b);  
    } 
    
    long MOD = 1000000007;
    
    ArrayList<Integer> adj[];
    ArrayList<Pair> adj2[];
    public void run() 
    {
        
            //InputReader sc= new InputReader(new FileInputStream("input.txt"));
            //PrintWriter w= new PrintWriter(new FileWriter("output.txt"));
            InputReader sc = new InputReader(System.in);
            PrintWriter w = new PrintWriter(System.out);
            int n = sc.nextInt();
            long[] arr = new long[1<<n];
            for(int i = 0;i < (1<<n);i++) {
            	arr[i] = sc.nextLong();
            }
            int temp = n+1;
            segTree = new Node[(1<<temp) - 1];
            build(0,(1<<n)-1,0,arr);
            int[] state = new int[n+1];
            long[][] val = new long[n+1][2];
            int curr = 0;
            for(int i = 0;i < n+1;i++) {
            	
            	for(int j = 0;j < (1<<i);j++) {
            		val[i][0] += segTree[curr].val[0];
            		val[i][1] += segTree[curr].val[1];
            		curr++;
            	}
            }
            Arrays.fill(state,1);
            long ans = 0;
            for(int i = 0;i <= n;i++) {
            	ans += val[i][1];
            }
            int m = sc.nextInt();
            for(int i = 0;i < m;i++) {
            	int x = sc.nextInt();
            	for(int j = n-x;j <= n;j++) {
            		ans -= val[j][state[j]];
            		state[j] ^= 1;
            		ans += val[j][state[j]];
            	}
            	w.println(ans);
            }
            w.close();		                                                           
    }
    Node segTree[];
    void build(int l,int r,int pos,long[] arr) {
    	if(l == r) {
    		segTree[pos] = new Node();
    		segTree[pos].ar.add(arr[l]);
    		return;
    	}
    	int mid = (l + r)/2;
    	build(l,mid,2*pos+1,arr);
    	build(mid+1,r,2*pos+2,arr);
    	segTree[pos] = merge(segTree[2*pos+1],segTree[2*pos+2]);
    }
    Node merge(Node one,Node two) {
    	ArrayList<Long> o = one.ar;
    	ArrayList<Long> s = two.ar;
    	ArrayList<Long> navu = new ArrayList();
    	int i = 0;
    	int j = 0;
    	long[] nav = new long[2];
    	while(i < o.size() && j < s.size()) {
    		if(o.get(i) <= s.get(j)) {
    			navu.add(o.get(i));
    			//nav[0] += (s.size() - j);
    			i++;
    		}
    		else {
    			navu.add(s.get(j));
    			nav[1] += (o.size() - i);
    			j++;
    		}
    	}
    	while(i < o.size()) {
    		navu.add(o.get(i));
    		i++;
    	}
    	while(j < s.size()) {
    		navu.add(s.get(j));
    		j++;
    	}
    	i = 0;
    	j = 0;
    	while(i < o.size() && j < s.size()) {
    		if(o.get(i) < s.get(j)) {
    			nav[0] += (s.size() - j);
    			i++;
    		}else {
    			j++;
    		}
    	}
    	
    	
    	return new Node(navu,nav);
    }
    static class Node{
    	ArrayList<Long> ar;
    	long[] val;
    	Node(){
    		ar = new ArrayList();
    		val = new long[2];
    	}
    	Node(ArrayList<Long> ar,long[] val) {
    		this.ar = ar;
    		this.val = val;
    	}
    }
    static long power(long a,long b,long mod) {
		long ans = 1;
		a = a % mod;
		while(b != 0) {
			if(b % 2 == 1) {
				ans = (ans * a) % mod;
			}
			a = (a * a) % mod;
			b = b/2;
		}
		return ans;
	}
    class Pair implements Comparable<Pair>{
        long a;
        int b;
        int c;
        //int c;
        Pair(long a,int b,int c){
            this.b = b;
            this.a = a;
            this.c = c;
        }
        public boolean equals(Object o) {
            Pair p = (Pair)o;
            return this.a == p.a && this.b == p.b;
        }
        public int hashCode(){
            return Long.hashCode(a)*27 + Long.hashCode(b)* 31;
        }
        public int compareTo(Pair p) {
            return Long.compare(this.a,p.a);
        }
    }
}