#include <bits/stdc++.h>
using namespace std;
long long pw2[100], a[1048580], b[1048580], f[100], g[100], res[1048580];
int m, n;
void merge(int l, int r, int p) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  merge(l, mid, p - 1);
  merge(mid + 1, r, p - 1);
  int x = l, y = mid + 1, z = 0;
  long long ans = 0, ans1 = 0, k = 0;
  while (x <= mid && y <= r) {
    while (y <= r && a[x] > a[y]) b[++z] = a[y++];
    if (y > r) break;
    ans += y - mid - 1;
    b[++z] = a[x++];
  }
  if (x <= mid) {
    for (int i = x; i <= mid; i++) {
      ans = ans + r - mid;
      z++, b[z] = a[i];
    }
  }
  if (y <= r) {
    for (int i = y; i <= r; i++) z++, b[z] = a[i];
  }
  for (int i = l; i <= r; i++) a[i] = b[i - l + 1];
  for (int i = l; i <= r; i++)
    if (i < r && a[i] == a[i + 1])
      k++;
    else
      ans1 += k * (k + 1) / 2, k = 0;
  f[p] += ans;
  g[p] += ans1;
}
int main() {
  memset(f, 0, sizeof(f));
  memset(g, 0, sizeof(g));
  pw2[0] = 1;
  for (int i = 1; i <= 60; i++) pw2[i] = pw2[i - 1] * 2;
  scanf("%d", &n);
  for (int i = 1; i <= (1 << n); i++) scanf("%d", a + i);
  merge(1, 1 << n, n);
  for (int i = 0; i < 1 << n; i++) {
    res[i] = 0;
    int pp = 0;
    for (int j = n; j > 0; j--) {
      if ((1 << (j - 1)) & i) pp = 1 - pp;
      if (pp)
        res[i] += pw2[n + j - 2] - f[j] - (g[j] - g[j - 1]);
      else
        res[i] += f[j];
    }
  }
  scanf("%d", &m);
  int q = 0;
  for (int i = 0; i < m; i++) {
    int x;
    scanf("%d", &x);
    if (x) q = q ^ (1 << (x - 1));
    printf("%I64d\n", res[q]);
  }
  return 0;
}
