#include <bits/stdc++.h>
int n, s, m, a[2000000], at[2000000], q[1000000];
long long sum[21], same[21];
long long V(int x) { return (long long)x * (x - 1) / 2; }
long long Merge(int l, int r, int t) {
  if (l == r) return 0;
  int mid = (l + r) / 2;
  long long ret = Merge(l, mid, t - 1) + Merge(mid + 1, r, t - 1);
  for (int i = l, j = mid + 1, k = l; i <= mid || j <= r; k++) {
    if (i > mid)
      at[k] = a[j++];
    else if (j > r)
      at[k] = a[i++];
    else if (a[i] <= a[j])
      at[k] = a[i++];
    else
      at[k] = a[j++], ret += mid - i + 1;
  }
  for (int i = l; i <= r; i++) a[i] = at[i];
  for (int i = l, j = l + 1; i <= r; i = j++) {
    while (j <= r && a[j] == a[i]) j++;
    same[t] += V(j - i);
  }
  sum[t] += ret;
  return ret;
}
int main() {
  scanf("%d", &n), s = 1 << n;
  for (int i = 0; i < s; i++) scanf("%d", &a[i]);
  scanf("%d", &m);
  for (int i = 0; i < m; i++) scanf("%d", &q[i]);
  Merge(0, s - 1, n);
  for (int i = 0; i <= n; i++) same[i] = V(1 << i) * (1 << (n - i)) - same[i];
  for (int i = 0; i < m; i++) {
    long long tmp = sum[q[i]];
    for (int j = q[i]; j <= n; j++) sum[j] += same[q[i]] - tmp * 2;
    for (int j = 0; j < q[i]; j++) sum[j] = same[j] - sum[j];
    printf("%I64d\n", sum[n]);
  }
  getchar();
  getchar();
  return 0;
}
