#include <bits/stdc++.h>
using namespace std;
int g[1200000], z[1200000];
long long c[30], d[30];
int i, j, m, n, t;
long long s;
inline int fastscanf() {
  int t = 0;
  char c = getchar();
  while (!((c > 47) && (c < 58))) c = getchar();
  while ((c > 47) && (c < 58)) t = t * 10 + c - 48, c = getchar();
  return t;
}
inline void sort(int l, int r, int e) {
  int mid = (l + r) / 2;
  int pl = l, tl = mid, ll = l, pr = mid + 1, tr = r, rr = mid + 1, pz = l,
      tz = r;
  while ((pl <= tl) && (pr <= tr))
    if (g[pl] <= g[pr]) {
      for (; (g[rr] <= g[pl]) && (rr <= pr); rr++)
        ;
      z[pz++] = g[pl++];
      d[e] = d[e] + tr - rr + 1;
    } else {
      for (; (g[ll] <= g[pr]) && (ll <= pl); ll++)
        ;
      z[pz++] = g[pr++];
      c[e] = c[e] + tl - ll + 1;
    }
  while (pl <= tl) z[pz++] = g[pl++];
  while (pr <= tr) z[pz++] = g[pr++];
  for (int i = l; i <= r; i++) g[i] = z[i];
  return;
}
inline void merge_sort(int l, int r, int d) {
  if (l != r) {
    int mid = (l + r) / 2;
    merge_sort(l, mid, d - 1);
    merge_sort(mid + 1, r, d - 1);
    sort(l, r, d);
  }
  return;
}
int main() {
  n = fastscanf();
  for (i = 1; i <= (1 << n); i++) g[i] = fastscanf();
  merge_sort(1, 1 << n, n);
  m = fastscanf();
  for (i = 1; i <= m; i++) {
    t = fastscanf();
    for (j = 1; j <= t; j++) swap(c[j], d[j]);
    s = 0;
    for (j = 1; j <= n; j++) s = s + c[j];
    printf("%I64d\n", s);
  }
  return 0;
}
