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

public class E {
    
    
    static void count_inv(int[] src, int[] dest, int lo, int hi, long[] inv, long[] eq) {
        if(lo+1==hi) {
            return;
        }
        
        int mid=(lo+hi)>>1;
        count_inv(dest, src, lo, mid, inv, eq);
        count_inv(dest, src, mid, hi, inv, eq);
        
        int bit=0, v=hi-lo-1;
        while(v!=0) {
            bit++; v=v>>1;
        }
        
        for (int l=lo, r=mid, r2=r, off=l; l<mid||r<hi; ) {
            while(r2+1<hi&&src[r]==src[r2+1]) {
                r2++;
            }
            if(l<mid&&(r==hi||src[l]<=src[r])) {
                if(r<hi&&src[l]==src[r]) {
                    eq[bit]+=(r2-r+1);
                }
                dest[off++]=src[l++];
                inv[bit]+=r-mid;
            } else {
                dest[off++]=src[r++];
                inv[bit]+=mid-l;
            }
        }
    }

    static void count_inv(int[] a, long[] inv, long[] eq) {
        int[] src=a.clone(), dest=a.clone();
        count_inv(src, dest, 0, a.length, inv, eq);
        
        for (int i=0; i<inv.length; i++) {
            inv[i]=inv[i]>>1;
        }
    }
    
    static void solve(int n, int[] a, int[] q, long[] out) {
        long[] inv=new long[n+1], eq=new long[n+1];
        count_inv(a, inv, eq);
        
        sb.setLength(0);
        int m=q.length;
        for (int i=0; i<m; i++) {
            long ret=0;
            for (int bit=1; bit<=n; bit++) {
                if(q[i]>=bit) {
                    long cnt=1<<(bit-1), pcnt=cnt*(1<<n)>>1;
                    inv[bit]=(pcnt-eq[bit])-inv[bit];
                }
                
                ret+=inv[bit];
            }
            out[i]=ret;
        }
    }
    

    
    
    
    
    static StringBuilder sb=new StringBuilder();
    static void parse(String l, int[] here) {
        sb.setLength(0);
        int len=l.length(), j=0;
        for (int i=0; i<len; i++) {
            char c=l.charAt(i);
            if(c==' ') {
                here[j++]=Integer.parseInt(sb.toString());
                sb.setLength(0);
            } else {
                sb.append(c);
            }
        }
        if(sb.length()>0) {
            here[j++]=Integer.parseInt(sb.toString());
        }
    }

    static void run_stream(InputStream ins) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        int[] a=new int[1<<n];
        parse(br.readLine(), a);
        
        int m=Integer.parseInt(br.readLine());
        int[] q=new int[m]; long[] out=new long[m];
        parse(br.readLine(), q);
        
        solve(n, a, q, out);
        
        sb.setLength(0);
        for (int i=0; i<m; i++) {
            sb.append(out[i]+"\n");
        }
        
        System.out.println(sb.toString());
    }

    public static void main(String[] args) throws IOException {
        run_stream(System.in);
        
        //test(16, 1000);
    }

    
    
    static void test(int n, int m) {
        Random r=new Random();
        int[] a=new int[1<<n];
        //HashSet<Integer> tmp=new HashSet<Integer>();
        for (int i=0; i<a.length; i++) {
            int v=r.nextInt(10000);
            /*while(tmp.contains(v)) {
                v=r.nextInt();
            }
            tmp.add(v);*/
            a[i]=v;
        }
        
        int[] q=new int[m];
        for (int i=0; i<m; i++) {
            q[i]=r.nextInt(n+1);
        }
        
        int err_cnt=test(n, m, a, q);
        
        System.out.println("errors: "+err_cnt);
    }

    static int test(int n, int m, int[] a, int[] q) {
        long[] out=new long[m], inv=new long[n+1], eq=new long[n+1];
        solve(n, a, q, out);
        
        /*System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(q));
        System.out.println();*/
        
        int err_cnt=0;
        for (int i=0; i<m; i++) {
            int step=(1<<q[i]);
            for (int j=0; j+step<=a.length; j+=step) {
                for (int k=0; k<step>>1; k++) {
                    int tmp=a[j+k]; a[j+k]=a[j+step-k-1]; a[j+step-k-1]=tmp;
                }
            }
            
            Arrays.fill(inv, 0);
            Arrays.fill(eq, 0);
            count_inv(a, inv, eq);
            long ret=0;
            for (int j=0; j<inv.length; j++) {
                ret+=inv[j];
            }
            
            if(ret!=out[i]) {
                System.out.println("after query "+i+" expected "+ret+" got "+out[i]);
                //System.out.println(Arrays.toString(a));
                err_cnt++;
            }
        }
        return err_cnt;
    }

}