#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
const double eps = 1e-10;
const int maxn = 1111111;
long long s[2][30];
int now[30];
long long pow2[30];
int A[maxn], AA[maxn], T[maxn];
int n, m;
void merge_sort(int *A, int x, int y, int *T, int level) {
  if (y <= x) return;
  int m = x + (y - x) / 2;
  int p = x, q = m + 1, i = x;
  merge_sort(A, x, m, T, level + 1);
  merge_sort(A, m + 1, y, T, level + 1);
  while (p <= m || q <= y) {
    if (q > y || (p <= m && A[p] <= A[q]))
      T[i++] = A[p++];
    else {
      s[0][level] += m - p + 1;
      T[i++] = A[q++];
    }
  }
  for (int i = x; i <= y; i++) A[i] = T[i];
}
void merge_sort2(int *A, int x, int y, int *T, int level) {
  if (y <= x) return;
  int m = x + (y - x) / 2;
  int p = x, q = m + 1, i = x;
  merge_sort2(A, x, m, T, level + 1);
  merge_sort2(A, m + 1, y, T, level + 1);
  while (p <= m || q <= y) {
    if (q > y || (p <= m && A[p] >= A[q]))
      T[i++] = A[p++];
    else {
      s[1][level] += m - p + 1;
      T[i++] = A[q++];
    }
  }
  for (int i = x; i <= y; i++) A[i] = T[i];
}
int main() {
  pow2[0] = 1;
  for (int i = 1; i <= 22; i++) pow2[i] = pow2[i - 1] * 2;
  scanf("%d", &n);
  for (int i = 0; i < (pow2[n]); ++i) scanf("%d", &A[i]), AA[i] = A[i];
  merge_sort(A, 0, pow2[n] - 1, T, 0);
  merge_sort2(AA, 0, pow2[n] - 1, T, 0);
  scanf("%d", &m);
  while (m--) {
    int x;
    scanf("%d", &x);
    for (int i = x; i >= 1; i--) {
      int y = n - i;
      now[y] ^= 1;
    }
    long long ans = 0;
    for (int i = 0; i <= n; i++) ans += s[now[i]][i];
    cout << ans << endl;
  }
  return 0;
}
