#include <bits/stdc++.h>
using namespace std;
int a[2][1 << 20], n;
long long cnt[21], all[21];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < 1 << n; ++i) scanf("%d", a[0] + i);
  for (int i = 0; i < n; ++i) {
    int *x = a[i & 1], *y = a[i & 1 ^ 1];
    all[i] = 1ll << n - 1 + i;
    for (int l = 0; l < 1 << n; l += 2 << i) {
      int m = l + (1 << i), r = m + (1 << i);
      int p = l, q = m, s = l;
      for (int v = m, h = l, t = l; v < r; ++v) {
        for (; h < m && x[h] < x[v];) ++h;
        for (; t < m && x[t] <= x[v];) ++t;
        all[i] -= t - h;
      }
      for (; p < m && q < r;)
        if (x[p] <= x[q])
          y[s++] = x[p++];
        else {
          cnt[i] += m - p;
          y[s++] = x[q++];
        }
      for (; p < m;) y[s++] = x[p++];
      for (; q < r;) y[s++] = x[q++];
    }
  }
  int m, q;
  for (scanf("%d", &m); m--;) {
    scanf("%d", &q);
    for (int i = 0; i < q; ++i) cnt[i] = all[i] - cnt[i];
    printf("%I64d\n", accumulate(cnt, cnt + n, 0ll));
  }
}
