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

/**
 * Created by hama_du on 2014/09/23.
 */
public class ProblemC {


    static void init(int l, int r, int idx, long[] rev, long[] diff, int[] a) {
        if (l+1 == r) {
            rev[idx] = 0;
            return;
        }
        int med = (l + r) / 2;
        init(l, med, (idx<<1)+1, rev, diff, a);
        init(med, r, (idx<<1)+2, rev, diff, a);
        rev[idx] = rev[(idx<<1)+1] + rev[(idx<<1)+2];
        diff[idx] = diff[(idx<<1)+1] + diff[(idx<<1)+2];

        int[] sub1 = Arrays.copyOfRange(a, l, med);
        int[] sub2 = Arrays.copyOfRange(a, med, r);
        Arrays.sort(sub1);
        Arrays.sort(sub2);
        int len = sub1.length;
        int j = 0;
        for (int i = 0 ; i < len ; i++) {
            while (j < len && sub1[i] > sub2[j]) {
                j++;
            }
            rev[idx] += j;
        }

        Map<Integer,Integer> m2 = new HashMap<>();
        for (int x : sub2) {
            if (!m2.containsKey(x)) {
                m2.put(x, 1);
            } else {
                m2.put(x, m2.get(x)+1);
            }
        }
        for (int i = 0 ; i < len ; i++) {
            if (m2.containsKey(sub1[i])) {
                diff[idx] += len-m2.get(sub1[i]);
            } else {
                diff[idx] += len;
            }
        }
    }

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int[] a = new int[1<<n];
        for (int i = 0 ; i < a.length ; i++) {
            a[i] = in.nextInt();
        }

        long[] diff = new long[1<<(n+1)];
        long[] rev = new long[1<<(n+1)];

        init(0, 1<<n, 0, rev, diff, a);

        long[] val = new long[n+1];
        long[] sum = new long[n+1];
        for (int i = 1 ; i <= n ; i++) {
            int f = (1<<(n-i))-1;
            int t = (1<<(n-i+1))-1;
            for (int j = f ; j < t ; j++) {
                val[i] += rev[j];
                sum[i] += diff[j];
            }
        }

        int m = in.nextInt();
        long ans = val[n];
        for (int i = 0 ; i < m ; i++) {
            int lv = in.nextInt();
            long all = sum[lv];
            long diffAns = -val[lv];
            val[lv] = all - val[lv];
            diffAns += val[lv];
            ans += diffAns;

            for (int k = lv+1 ; k <= n ; k++) {
                val[k] += diffAns;
            }
            for (int k = lv-1 ; k >= 0 ; k--) {
                val[k] = sum[k] - val[k];
            }
            out.println(ans);
        }
        out.flush();
    }

    public static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }



    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

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

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

        public int nextInt() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            long sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

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

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}