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.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;

public class Round240C {
	
	public static class Node {
		int[] sorted;
		int level;
		
		public Node(int level, int val) {
			sorted = new int[1];
			sorted[0] = val;
			this.level = level;
		}
		
		public Node(Node left, Node right, int level) {
			sorted = new int[left.sorted.length * 2];
			this.level = level;
			Merge(left, right);
			ADD_ANSWER(left, right);
		}
		
		public void Merge(Node left, Node right) {
			int[] l_tree = left.sorted;
			int[] r_tree = right.sorted;
			int l = l_tree.length;
			int i = 0, j = 0;
			int ptr = 0;
			while(i < l && j < l) {
				if(l_tree[i] > r_tree[j]) {
					sorted[ptr] = r_tree[j];
					j++;
				}else if(l_tree[i] < r_tree[j]) {
					sorted[ptr] = l_tree[i];
					i++;
				}else {
					sorted[ptr++] = l_tree[i];
					sorted[ptr] = r_tree[j];
					i++;
					j++;
				}
				ptr++;
			}
			while(i < l) {
				sorted[ptr++] = l_tree[i++];
			}
			while(j < l) {
				sorted[ptr++] = r_tree[j++];
			}
		}
		
		public void ADD_ANSWER(Node left, Node right) {
			int[] l_tree = left.sorted;
			int[] r_tree = right.sorted;
			int l = l_tree.length;
			long[] ans = new long[2];
			int ptr = 0;
			for(int i = 0; i < l; i++) {
				while(ptr < l && l_tree[i] > r_tree[ptr]) {
					ptr++;
				}
				ans[0] += ptr;
			}
			ptr = l - 1;
			for(int i = l - 1; i >= 0; i--) {
				while(ptr >= 0 && l_tree[i] < r_tree[ptr]) {
					ptr--;
				}
				ans[1] += l - 1 - ptr;
			}
			level_ans[level][0] += ans[0];
			level_ans[level][1] += ans[1];
		}
			
	}
	
	public static Node[] tree;
	
	public static void Build(int start, int end, int index, int[] arr, int level) {
		if(start == end) {
			tree[index] = new Node(level, arr[start]);
			return;
		}
		int mid = (start + end)>>1;
		Build(start, mid, index<<1, arr, level + 1);
		Build(mid + 1, end, index<<1|1, arr, level + 1);
		tree[index] = new Node(tree[index<<1], tree[index<<1|1], level);
	}

	public static void solve() {
		int n = s.nextInt();
		int[] arr = s.nextIntArray(1<<n);
		tree = new Node[4 * (1<<n) + 1];
		level_ans = new long[n + 1][2];
		Build(0, (1<<n) - 1, 1, arr, 0);
		int[] current_state = new int[n + 1];
		int q = s.nextInt();
		while(q-- > 0) {
			int change = s.nextInt();
			int level = n - Integer.toBinaryString(1<<change).length() ;
			long ans = 0;
			for(int i = 0; i < n; i++) {
				if(level < i) {
					current_state[i] ^= 1;
				}
				ans += level_ans[i][current_state[i]];
			}
			out.println(ans);
		}
	}
	
	public static long[][] level_ans;
	
	public static void main(String[] args) {
		new Thread(null, null, "Thread", 1 << 27) {
			public void run() {
				try {
					out = new PrintWriter(new BufferedOutputStream(System.out));
					s = new FastReader(System.in);
					solve();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(1);
				}
			}
		}.start();
	}

	public static PrintWriter out;
	public static FastReader s;

	public static class FastReader {

		private InputStream stream;
		private byte[] buf = new byte[4096];
		private int curChar, snumChars;

		public FastReader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (snumChars == -1) {
				throw new InputMismatchException();
			}
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException E) {
					throw new InputMismatchException();
				}
			}
			if (snumChars <= 0) {
				return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int number = 0;
			do {
				number *= 10;
				number += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			long sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long number = 0;
			do {
				number *= 10L;
				number += (long) (c - '0');
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextLong();
			}
			return arr;
		}

		public String next() {
			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 String nextLine() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndofLine(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public boolean isEndofLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

	}

	
}