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

import static java.util.Arrays.*;
import static java.lang.Math.*;
import static java.util.Collections.*;
import static java.lang.System.out;

public class Main {
	static boolean LOCAL = System.getSecurityManager() == null;
	Scanner in = new Scanner(System.in);

	void run() {
		while (in.hasNext()) 
			solve();
	}
	void solve() {
		int n = in.nextInt();
		int N = (1 << n);
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = in.nextInt();
		}
//		hash(A);
		long[] rs = new long[n], rrs = new long[n];
		int[] rA = reverse(A);
		int[] ts = new int[N];
		mergeSort(rs, n - 1, A, 0, N, ts);
		mergeSort(rrs, n - 1, rA, 0, N, ts);
		int m = in.nextInt();
		StringBuilder sb = new StringBuilder();
		while (m-- != 0) {
			int q = in.nextInt();
			for (int i = 0; i < q; i++) {long t = rs[i]; rs[i] = rrs[i]; rrs[i] = t;}
			long ans = 0;
			for (int i = 0; i < n; i++) ans += rs[i];
			sb.append(ans);
			sb.append(System.lineSeparator());
		}
		out.print(sb);
	}

	private void mergeSort(long[] rs, int ri, int[] is, int b, int e, int[] ts) {
		if (ri < 0) return ;
		int m = (b + e) / 2;
		mergeSort(rs, ri - 1, is, b, m, ts);
		mergeSort(rs, ri - 1, is, m, e, ts);
		for (int i = b, j = m, k = b; k < e; ) {
			if (j >= e || i < m && is[i] <= is[j]) ts[k++] = is[i++];
			else {
				ts[k++] = is[j++];
				rs[ri] += m - i;
			}
		}
		for (int i = b; i < e; i++) is[i] = ts[i];
	}

	private int[] reverse(int[] a) {
		int n = a.length;
		int[] r = new int[n];
		for (int i = 0; i < n; i++) r[i] = a[n - i - 1];
		return r;
	}

	private void hash(int[] a) {
		Map<Integer, Integer> map = new HashMap<>();
		int[] b = a.clone();
		sort(b);
		for (int i = 0; i < b.length; i++) map.put(b[i], i);
		for (int i = 0; i < a.length; i++) a[i] = map.get(a[i]);
	}

	void debug(Object... os) {
		System.err.println(deepToString(os));
	}

	public static void main(String[] args) {
		if (LOCAL) {
			try {
				System.setIn(new FileInputStream("./../in"));
			} catch (IOException e) {
				LOCAL = false;
			}
		}
		long start = System.nanoTime();
		new Main().run();
		if (LOCAL)
			System.err.printf("[Time %.6fs]%n",
					(System.nanoTime() - start) * 1e-9);
	}
}

class Scanner {
	BufferedReader br;
	StringTokenizer st;

	Scanner(InputStream in) {
		br = new BufferedReader(new InputStreamReader(in));
		eat("");
	}

	private void eat(String string) {
		st = new StringTokenizer(string);
	}

	String nextLine() {
		try {
			return br.readLine();
		} catch (IOException e) {
			return null;
		}
	}

	boolean hasNext() {
		while (!st.hasMoreTokens()) {
			String s = nextLine();
			if (s == null)
				return false;
			eat(s);
		}
		return true;
	}

	String next() {
		hasNext();
		return st.nextToken();
	}

	int nextInt() {
		return Integer.parseInt(next());
	}

	long nextLong() {
		return Long.parseLong(next());
	}

	double nextDouble() {
		return Double.parseDouble(next());
	}
}