#include <bits/stdc++.h>
using namespace std;
stringstream ss;
long long mod = 1000000007LL;
long long x[32], y[32];
int a[1 << 20], b[1 << 20], N;
void solve(int l, int r, int dep) {
  if (!dep) return;
  int m = (l + r) / 2;
  solve(l, m, dep - 1);
  solve(m, r, dep - 1);
  int i = l, j = m, k = 0;
  while (j < r) {
    while (i < m && a[i] < a[j]) b[k++] = a[i++];
    y[dep] += i - l;
    b[k++] = a[j++];
  }
  while (i < m) b[k++] = a[i++];
  i = l, j = m;
  while (i < m) {
    while (j < r && a[j] < a[i]) j++;
    i++;
    x[dep] += j - m;
  }
  for (int i = l, k = 0; i < r; i++) a[i] = b[k++];
}
int main() {
  int Q;
  cin >> N;
  for (int i = 0; i < (1LL << N); i++) cin >> a[i];
  solve(0, (1LL << N), N);
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long xx, ans = 0;
    cin >> xx;
    for (int j = xx; j >= 0; j--) swap(x[j], y[j]);
    for (int j = 0; j <= N; j++) ans += x[j];
    cout << ans << endl;
  }
  return 0;
}
