#include <bits/stdc++.h>
using namespace std;
long long n, m, sum[30][2], mi[30], a[1050000];
void read(long long &x) {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
}
void merge(int l, int r, int d) {
  int mid = (l + r) >> 1;
  if (l < r - 1) {
    merge(l, mid, d - 1);
    merge(mid, r, d - 1);
    long long cnt = 0;
    for (int i = l; i < mid; i++)
      cnt += lower_bound(a + mid, a + r, a[i]) - a - mid;
    sum[d][0] += cnt;
    cnt = 0;
    for (int i = mid; i < r; i++)
      cnt += lower_bound(a + l, a + mid, a[i]) - a - l;
    sum[d][1] += cnt;
    inplace_merge(a + l, a + mid, a + r);
  }
}
int main() {
  mi[0] = 1;
  for (int i = 1; i < 30; i++) mi[i] = mi[i - 1] << 1;
  read(n);
  for (int i = 0; i < mi[n]; i++) read(a[i]);
  merge(0, mi[n], n);
  read(m);
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    long long x = 0;
    read(x);
    for (int i = x - 1; i >= 0; i--) swap(sum[i + 1][0], sum[i + 1][1]);
    ans = 0;
    for (int i = 0; i <= n; i++) ans += sum[i][0];
    cout << ans << endl;
  }
}
