#include <bits/stdc++.h>
using namespace std;
int N, M, Len;
long long dp[25][2], A[2000005];
inline long long read() {
  char ch;
  long long a = 0;
  while ((ch = getchar()) < '0' || ch > '9')
    ;
  while ((ch >= '0') && (ch <= '9')) a = a * 10 + ch - '0', ch = getchar();
  return a;
}
inline void read(int &a) {
  char ch;
  a = 0;
  while ((ch = getchar()) < '0' || ch > '9')
    ;
  while ((ch >= '0') && (ch <= '9')) a = a * 10 + ch - '0', ch = getchar();
}
void write(long long a) {
  if (a >= 10) write(a / 10);
  putchar(a % 10 + '0');
}
inline void Merge(int l, int r, int dep) {
  if (!dep) return;
  int m = (l + r) / 2;
  Merge(l, m, dep - 1);
  Merge(m + 1, r, dep - 1);
  for (int i = l; i <= m; i++) {
    dp[dep][0] += lower_bound(A + m + 1, A + r + 1, A[i]) - (A + m + 1);
    dp[dep][1] +=
        r - m - (upper_bound(A + m + 1, A + r + 1, A[i]) - (A + m + 1));
  }
  sort(A + l, A + r + 1);
}
int main() {
  read(N);
  Len = 1 << N;
  for (int i = 1; i <= Len; i++) A[i] = read();
  memset(dp, 0, sizeof(dp));
  Merge(1, Len, N);
  read(M);
  for (int i = 1, x; i <= M; i++) {
    read(x);
    long long Ans = 0;
    for (int i = 1; i <= x; i++) swap(dp[i][0], dp[i][1]);
    for (int i = 1; i <= N; i++) Ans += dp[i][0];
    write(Ans);
    putchar('\n');
  }
  return 0;
}
