#include <bits/stdc++.h>
using namespace std;
int const MAXN = (1 << 20) + 5;
int const MAXM = 1e6 + 6;
int a[MAXN];
long long sum[MAXN][2];
int n, m;
void DFS(int l, int r, int dep) {
  if (dep == 0) {
    sum[0][1] = sum[0][0] = 0;
    return;
  }
  int mid = (l + r) >> 1;
  DFS(l, mid, dep - 1);
  DFS(mid + 1, r, dep - 1);
  for (int i = l; i <= mid; i++) {
    sum[dep][0] += lower_bound(a + mid + 1, a + r + 1, a[i]) - (a + mid + 1);
  }
  for (int i = mid + 1; i <= r; i++)
    sum[dep][1] += lower_bound(a + l, a + mid + 1, a[i]) - (a + l);
  sort(a + l, a + r + 1);
  return;
}
int main() {
  scanf("%d", &n);
  int all = (1 << n);
  for (int i = 1; i <= all; i++) scanf("%d", &a[i]);
  DFS(1, all, n);
  scanf("%d", &m);
  int q;
  for (int i = 0; i < m; i++) {
    scanf("%d", &q);
    for (int j = q; j >= 0; j--) swap(sum[j][0], sum[j][1]);
    long long ans = 0;
    for (int j = n; j >= 0; j--) ans += (long long)sum[j][0];
    printf("%I64d\n", ans);
  }
}
