#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 20) + 10;
int a[maxn], b[maxn], c[maxn];
long long inv[2][25];
int n, d, m;
long long ans;
void f(int *a, int l, int r, int dep, int mark) {
  if (r - l == 0) return;
  int mid = (r + l) / 2;
  f(a, l, mid, dep + 1, mark);
  f(a, mid + 1, r, dep + 1, mark);
  int x = l, y = mid + 1, i = l;
  while (i <= r) {
    if (y > r || (x <= mid && a[x] <= a[y]))
      c[i++] = a[x++];
    else {
      c[i++] = a[y++];
      inv[mark][dep] += (mid - x + 1);
    }
  }
  for (int j = l; j <= r; j++) a[j] = c[j];
  return;
}
int main() {
  scanf("%d", &d);
  n = (1 << d);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  long long k = 1;
  for (int j = n; j > 0; j--) b[j] = a[k++];
  f(a, 1, n, 0, 0);
  f(b, 1, n, 0, 1);
  scanf("%d", &m);
  while (m--) {
    ans = 0;
    int x;
    scanf("%d", &x);
    for (int i = d - x; i <= d; i++) swap(inv[0][i], inv[1][i]);
    for (int i = 0; i <= d; i++) ans += inv[0][i];
    printf("%I64d\n", ans);
  }
}
