#include <bits/stdc++.h>
using namespace std;
long long n, cou[21][2], a[(1 << 20) + 10];
void init() {
  int i;
  for (i = 0; i <= n; i++) cou[i][0] = cou[i][1] = 0;
}
void build(int l, int r, int s) {
  if (s == n) return;
  int m = (l + r) >> 1, i;
  build(l, m, s + 1);
  build(m + 1, r, s + 1);
  for (i = l; i <= m; i++)
    cou[s][1] += lower_bound(a + m + 1, a + r + 1, a[i]) - (a + m + 1);
  for (i = m + 1; i <= r; i++)
    cou[s][0] += lower_bound(a + l, a + m + 1, a[i]) - (a + l);
  sort(a + l, a + r + 1);
}
int main() {
  int q, all, i, p, j;
  scanf("%d", &n);
  all = 1 << n;
  for (i = 1; i <= all; i++) scanf("%I64d", &a[i]);
  build(1, all, 0);
  scanf("%d", &q);
  for (i = 1; i <= q; i++) {
    scanf("%d", &p);
    for (j = n - p; j <= n; j++) swap(cou[j][0], cou[j][1]);
    long long ans = 0;
    for (j = 0; j <= n; j++) ans += cou[j][1];
    printf("%I64d\n", ans);
  }
}
