#include <bits/stdc++.h>
using namespace std;
int s[3000000], pre[3000000], b[3000000];
long long int cnt1[25] = {0}, cnt2[25] = {0};
void msort1(int l, int r, int d) {
  if (l + 1 == r) return;
  int m = (l + r) / 2;
  msort1(l, m, d - 1);
  msort1(m, r, d - 1);
  int L = 0, R = 0, i;
  while (L + l != m && R + m != r) {
    if (s[l + L] > s[m + R]) {
      cnt1[d] += m - L - l;
      b[l + L + R] = s[m + R];
      R++;
    } else if (s[l + L] < s[m + R]) {
      b[l + L + R] = s[l + L];
      L++;
    } else {
      b[l + L + R] = s[l + L];
      L++;
    }
  }
  while (L + l < m) b[l + L + R] = s[l + L], L++;
  while (R + m < r) b[l + L + R] = s[m + R], R++;
  for (i = l; i < r; i++) s[i] = b[i];
}
void msort2(int l, int r, int d) {
  if (l + 1 == r) return;
  int m = (l + r) / 2;
  msort2(l, m, d - 1);
  msort2(m, r, d - 1);
  int L = 0, R = 0, i;
  while (L + l != m && R + m != r) {
    if (s[l + L] > s[m + R]) {
      b[l + L + R] = s[m + R];
      R++;
    } else if (s[l + L] < s[m + R]) {
      cnt2[d] += r - R - m;
      b[l + L + R] = s[l + L];
      L++;
    } else {
      b[l + L + R] = s[m + R];
      R++;
    }
  }
  while (L + l < m) b[l + L + R] = s[l + L], L++;
  while (R + m < r) b[l + L + R] = s[m + R], R++;
  for (i = l; i < r; i++) s[i] = b[i];
}
int main() {
  int n, m, i, j, k;
  scanf("%d", &n);
  int all = (1 << n);
  for (i = 0; i < all; i++) scanf("%d", &s[i]), pre[i] = s[i];
  msort1(0, all, n);
  for (i = 0; i < all; i++) s[i] = pre[i];
  msort2(0, all, n);
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d", &k);
    for (j = 0; j <= k; j++) {
      swap(cnt1[j], cnt2[j]);
    }
    long long int ans = 0;
    for (j = 0; j <= n; j++) {
      ans += cnt1[j];
    }
    printf("%lld\n", ans);
  }
}
