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

public class Main {
       int n,a[];
       long dp[][],seg[];
       
       void m(ArrayList<Integer> al,ArrayList<Integer> al1,ArrayList<Integer> al2,int l,int node){
        int sz1=al1.size(),sz2=al2.size();
        int p1=0,p2=0;long v=0,ci=0,common=0;
        while(p1!=sz1&&p2!=sz2){
         int v1=al1.get(p1),v2=al2.get(p2);
         if(v1>v2){
          ++v;al.add(v2);++p2;
         }else if(v1<=v2){
          ci+=v;al.add(v1);++p1;
         }
        }
        while(p1!=sz1){
         int v1=al1.get(p1);
         ci+=v;al.add(v1);++p1;
        }
        while(p2!=sz2){
         int v2=al2.get(p2);
         al.add(v2);++p2;
        }
        seg[node]=seg[2*node+1]+seg[2*node+2]+ci;
        int pvt=-1;long c=1;
        for(int i=0;i<l;i++){
         int e=al.get(i);
         if(e==pvt){
          c++;
         }else{
          common+=c*(c-1)/2;c=1;pvt=e;
         }
        }common+=c*(c-1)/2;
        int t=(int)(Math.log(l)/Math.log(2));
        dp[t][1]+=seg[node];
        dp[t][2]+=common;
       }
       
       ArrayList<Integer> make(int n,int l,int r){
        ArrayList<Integer> al=new ArrayList<>();
        if(l==r){
         al.add(a[l]);return al;
        }
        int m=(l+r)>>1;
        ArrayList<Integer> al1=make(2*n+1,l,m);
        ArrayList<Integer> al2=make(2*n+2,m+1,r);
        m(al,al1,al2,r-l+1,n);
        al1.clear();al2.clear();
//        System.out.println(al);
        return al;
       }
       
    void submit() {
            n=nextInt();
            int sz=1<<n;
            a=new int[sz];
            for(int i=0;i<sz;i++)a[i]=nextInt();
            dp=new long[n+1][3];
            long m=1;
            for(int i=n;i>0;i--){
             long v=(1<<i);
             dp[i][0]=m*v*(v-1)/2;
             m*=2;
            }
            seg=new long[4*sz];
            make(0,0,sz-1);
            
            long ans=dp[n][1];
//            System.out.println(ans);
//for(int i=0;i<=n;i++)System.out.println(dp[i][0]+" "+dp[i][1]+" "+dp[i][2]);
            int q=nextInt();
            while(q-->0){
             int t=nextInt();
             long np=dp[t][0]-dp[t][1]-dp[t][2];
             ans=ans-dp[t][1]+np;
             for(int i=t+1;i<=n;i++){
              dp[i][1]=dp[i][1]-dp[t][1]+np;
             }
             for(int i=t-1;i>=0;i--){
              dp[i][1]=dp[i][0]-dp[i][1]-dp[i][2];
             }
             dp[t][1]=np;
             out.println(ans);
            }
	}
    /*
2
1 3 2 4
4
1 2 0 2
    */
	void stress() {
		for (int tst = 0;; tst++) {
			if (false) {
				throw new AssertionError();
			}
			System.err.println(tst);
		}
	}

	Main() throws IOException {
		is = System.in;
		out = new PrintWriter(System.out);
		submit();
		// stress();
		// test();
		out.close();
	}

	static final Random rng = new Random();
	static final int C = 5;

	static int rand(int l, int r) {
		return l + rng.nextInt(r - l + 1);
	}

	public static void main(String[] args) throws IOException {
		new Main();
	}

	private InputStream is;
	PrintWriter out;

	private byte[] buf = new byte[1 << 14];
	private int bufSz = 0, bufPtr = 0;

	private int readByte() {
		if (bufSz == -1)
			throw new RuntimeException("Reading past EOF");
		if (bufPtr >= bufSz) {
			bufPtr = 0;
			try {
				bufSz = is.read(buf);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			if (bufSz <= 0)
				return -1;
		}
		return buf[bufPtr++];
	}

	private boolean isTrash(int c) {
		return c < 33 || c > 126;
	}

	private int skip() {
		int b;
		while ((b = readByte()) != -1 && isTrash(b))
			;
		return b;
	}

	String nextToken() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	String nextString() {
		int b = readByte();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b) || b == ' ') {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

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

	char nextChar() {
		return (char) skip();
	}

	int nextInt() {
		int ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}

	long nextLong() {
		long ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}
}