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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
  if (rsz <= 0) {
    ptr = 0;
    rsz = fread(bf, 1, bfsz, stdin);
    if (rsz <= 0) return EOF;
  }
  --rsz;
  return bf[ptr++];
}
void ga(char &c) {
  c = EOF;
  while (!isalpha(c)) c = gc();
}
int gs(char s[]) {
  int l = 0;
  char c = gc();
  while (isspace(c)) c = gc();
  while (c != EOF && !isspace(c)) {
    s[l++] = c;
    c = gc();
  }
  s[l] = '\0';
  return l;
}
template <class T>
bool gi(T &v) {
  v = 0;
  char c = gc();
  while (c != EOF && c != '-' && !isdigit(c)) c = gc();
  if (c == EOF) return false;
  bool neg = c == '-';
  if (neg) c = gc();
  while (isdigit(c)) {
    v = v * 10 + c - '0';
    c = gc();
  }
  if (neg) v = -v;
  return true;
}
bool exitInput = false;
int ntest = 1, itest = 1;
const int dx[4] = {-1, +0, +1, -0};
const int dy[4] = {+0, +1, +0, -1};
const long long Mod = 1e9 + 7;
const int maxn = (1 << 20) + 5;
const int maxv = 20 + 5;
const int maxe = 4 * (1 << 20) + 5;
int Logn, a[maxn], b[maxn], arr[maxv][maxn];
long long n, res, inversion_node[maxe], inversion_level[maxv],
    equal_pair_node[maxe], equal_pair_level[maxv];
long long totalChange;
void build(int node, int L, int R, int level) {
  if (L == R) {
    inversion_node[node] = equal_pair_node[node] = 0;
    arr[level][L] = a[L];
    return;
  }
  int mid = (L + R) >> 1;
  build((node << 1), L, mid, level - 1);
  build(((node << 1) | 1), mid + 1, R, level - 1);
  inversion_node[node] =
      inversion_node[(node << 1)] + inversion_node[((node << 1) | 1)];
  equal_pair_node[node] =
      equal_pair_node[(node << 1)] + equal_pair_node[((node << 1) | 1)];
  int i, j, k, cnt_smaller, cnt_equal;
  cnt_smaller = cnt_equal = 0;
  for (i = L, j = mid + 1, k = 0; i <= mid; ++i) {
    if (i == L || arr[level - 1][i] > arr[level - 1][i - 1]) {
      cnt_smaller += cnt_equal;
      for (; j <= R && arr[level - 1][j] < arr[level - 1][i];
           ++j, ++cnt_smaller) {
        b[k++] = arr[level - 1][j];
      }
      cnt_equal = 0;
      for (; j <= R && arr[level - 1][j] == arr[level - 1][i];
           ++j, ++cnt_equal) {
        b[k++] = arr[level - 1][j];
      }
    }
    inversion_node[node] += cnt_smaller;
    equal_pair_node[node] += cnt_equal;
    b[k++] = arr[level - 1][i];
  }
  for (; j <= R; ++j) b[k++] = arr[level - 1][j];
  inversion_level[level] += inversion_node[node];
  equal_pair_level[level] += equal_pair_node[node];
  for (i = 0; i <= R - L; ++i) {
    arr[level][L + i] = b[i];
  }
}
void pre() {}
void read() {
  gi(Logn);
  n = 1 << Logn;
  for (int i = 1; i <= n; ++i) {
    gi(a[i]);
  }
}
void solve() {
  build(1, 1, n, Logn);
  res = inversion_level[Logn];
  int Q, q, len, i;
  gi(Q);
  while (Q--) {
    gi(q);
    if (q > 0) {
      len = 1 << q;
      totalChange =
          n * (len - 1) / 2 - inversion_level[q] * 2 - equal_pair_level[q];
      res = res + totalChange;
      for (i = q; i >= 0; --i) {
        len = 1 << i;
        inversion_level[i] =
            n * (len - 1LL) / 2 - inversion_level[i] - equal_pair_level[i];
      }
      for (i = q + 1; i <= Logn; ++i) {
        inversion_level[i] += totalChange;
      }
    }
    printf("%lld\n", res);
  }
}
int main() {
  pre();
  for (itest = 1; itest <= ntest; ++itest) {
    read();
    if (exitInput) {
      break;
    }
    solve();
  }
  return 0;
}