#include <bits/stdc++.h>
using namespace std;
int n, lim;
int a[1 << 20], tmp[1 << 20];
long long inversions[20][2];
long long merge(int* l, int* r, int sz) {
  long long ret = 0;
  for (int i = 0, j = 0, out = 0; out < 2 * sz; ++out) {
    if (i == sz) {
      tmp[out] = r[j++];
    } else if (j == sz || l[i] <= r[j]) {
      tmp[out] = l[i++];
      ret += j;
    } else {
      tmp[out] = r[j++];
    }
  }
  return ret;
}
void genInversions(int v) {
  if (v * 2 + 1 < lim - 1) {
    genInversions(v * 2 + 1);
    genInversions(v * 2 + 2);
  }
  int from = v + 1, lvl = n;
  while (from < lim) {
    from <<= 1;
    --lvl;
  }
  from -= lim;
  int sz = 1 << (n - lvl - 1);
  inversions[lvl][1] += merge(a + from + sz, a + from, sz);
  inversions[lvl][0] += merge(a + from, a + from + sz, sz);
  memcpy(a + from, tmp, sizeof(int) * 2 * sz);
}
int main() {
  scanf("%d", &n);
  lim = 1 << n;
  for (int i = 0; i < lim; ++i) {
    scanf("%d", a + i);
  }
  genInversions(0);
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    int q;
    scanf("%d", &q);
    for (q = n - q; q < n; ++q) swap(inversions[q][0], inversions[q][1]);
    long long ans = 0;
    for (int i = 0; i < n; ++i) ans += inversions[i][0];
    printf("%I64d\n", ans);
  }
  return 0;
}
