#include <bits/stdc++.h>
using namespace std;
const int N = 2000010, D = 25;
int n, q, a[N], te[N], b[D];
long long x[D], y[D];
void calc(int l, int r, int d) {
  if (!d) return;
  int m = l + r >> 1, p = r - l + 1 >> 1;
  calc(l, m, d - 1);
  calc(m + 1, r, d - 1);
  for (int i = 1, j = 1; i <= p; i++) {
    while (j <= p && a[l + i - 1] > a[m + j]) j++;
    x[d] += j - 1;
  }
  for (int i = 1, j = 1; i <= p; i++) {
    while (j <= p && a[l + i - 1] >= a[m + j]) j++;
    y[d] += p + 1 - j;
  }
  merge(a + l, a + m + 1, a + m + 1, a + r + 1, te);
  for (int i = l; i <= r; i++) a[i] = te[i - l];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 1 << n; i++) scanf("%d", &a[i]);
  calc(1, 1 << n, n);
  scanf("%d", &q);
  while (q--) {
    int t;
    long long s = 0;
    scanf("%d", &t);
    for (int i = 0; i <= t; i++) b[i] ^= 1;
    for (int i = 0; i <= n; i++)
      if (!b[i])
        s += x[i];
      else
        s += y[i];
    printf("%lld\n", s);
  }
  return 0;
}
