#include <bits/stdc++.h>
using namespace std;
int n, q, x, a[(1 << 20)], b[(1 << 20)], h[(1 << 20)];
long long f[21], g[21];
long long MergeSort(int* a, long long* f, int l, int r, int x) {
  if (l == r)
    return f[x] = 0;
  else {
    long long cur = 0;
    int mid = (l + r) >> 1;
    cur += MergeSort(a, f, l, mid, x - 1);
    cur += MergeSort(a, f, mid + 1, r, x - 1);
    for (int i = l, j = mid + 1, k = 0; i <= mid || j <= r;) {
      if (j > r || (i <= mid && a[i] <= a[j]))
        h[k++] = a[i++], cur += j - mid - 1;
      else
        h[k++] = a[j++];
    }
    f[x] += cur;
    for (int i = l; i <= r; i++) a[i] = h[i - l];
    return cur;
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < (1 << n); i++) scanf("%d", a + i);
  for (int i = 0; i < (1 << n); i++) b[i] = a[(1 << n) - i - 1];
  MergeSort(a, f, 0, (1 << n) - 1, n);
  MergeSort(b, g, 0, (1 << n) - 1, n);
  for (int i = 0; i <= n; i++) g[i] += f[i];
  for (cin >> q; q--;) {
    scanf("%d", &x);
    long long ofs = g[x] - f[x] - f[x];
    for (int i = 0; i < x; i++) f[i] = g[i] - f[i];
    for (int i = x; i <= n; i++) f[i] += ofs;
    printf("%I64d\n", f[n]);
  }
  return 0;
}
