#include <bits/stdc++.h>
using namespace std;
int n;
int a[1500005];
long long f[55], g[55];
inline void merge(int l, int r, int x) {
  if (l >= r) return;
  int mid = (l + r) >> 1;
  merge(l, mid, x - 1), merge(mid + 1, r, x - 1);
  for (int i = l; i <= mid; i++) {
    f[x] += lower_bound(a + mid + 1, a + r + 1, a[i]) - (a + mid + 1);
    g[x] +=
        r - mid - (upper_bound(a + mid + 1, a + r + 1, a[i]) - (a + mid + 1));
  }
  sort(a + l, a + r + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= (1 << n); i++) scanf("%d", &a[i]);
  merge(1, (1 << n), n);
  int query;
  scanf("%d", &query);
  while (query--) {
    int x;
    long long ans = 0;
    scanf("%d", &x);
    for (int i = 1; i <= x; i++) swap(f[i], g[i]);
    for (int i = 1; i <= n; i++) ans += f[i];
    printf("%I64d\n", ans);
  }
}
