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.*;
import java.util.*;

public class C implements Runnable{
	public static void main (String[] args) {new Thread(null, new C(), "_cf", 1 << 28).start();}

	long[] sums, sumr;
	
	public void run() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("Go!");

		int n = fs.nextInt();
		int m = (1 << (n+1))+10;
		int[] a = fs.nextIntArray(1<<n);
		long[] invCount = new long[m], rinvCount = new long[m];
		sums = new long[n+2]; sumr = new long[n+2];  
		go(1, 0, (1<<n)-1, a, invCount, rinvCount);
		
		int[] state = new int[n+1];
		int q = fs.nextInt();
		while(q-->0) {
			int qi = fs.nextInt();
			for(int i = n-qi; i <= n; i++) state[i] ^= 1;
			long res = 0;
			for(int i = 0; i < n; i++) {
				if(state[i] == 1) res += sumr[i];
				else res += sums[i];
			}
			out.println(res);
		}
		
		out.close();
	}

	int[] go(int node, int left, int right, int[] a, long[] invCount, long[] rinvCount) {
		if(left == right) {
			invCount[node] = rinvCount[node] = 0;
			return new int[] {a[left]};
		}
		int[] lf = go(2*node, left, (left+right)/2, a, invCount, rinvCount);
		int[] rg = go(2*node+1, (left+right)/2+1, right, a, invCount, rinvCount);
		for(int i = 0, j = 0; i < lf.length; i++) {
			while(j < rg.length && lf[i] > rg[j]) j++;
			invCount[node] += j;
		}
		for(int i = 0, j = 0; i < rg.length; i++) {
			while(j < lf.length && rg[i] > lf[j]) j++;
			rinvCount[node] += j;
		}
		sums[Integer.numberOfTrailingZeros(Integer.highestOneBit(node))] += invCount[node];
		sumr[Integer.numberOfTrailingZeros(Integer.highestOneBit(node))] += rinvCount[node];
		int[] res = new int[lf.length+rg.length];
		int p1 = 0, p2 = 0;
		while(p1 < lf.length || p2 < rg.length) {
			if(p1 == lf.length) res[p1+p2] = rg[p2++];
			else if(p2 == rg.length) res[p1+p2] = lf[p1++];
			else if(lf[p1] <= rg[p2]) res[p1+p2] = lf[p1++];
			else res[p1+p2] = rg[p2++];
		}
		return res;
	}

	class FastScanner {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
		
		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}
		
	}

}