#include <bits/stdc++.h>
using namespace std;
int a[2000010];
long long f[22], g[22];
int b[2000010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= (1 << n); i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    int x = 1 << (i - 1);
    int N = 1 << n;
    for (int j = 1; j <= N / 2 / x; j++) {
      int st = 2 * x * (j - 1) + 1;
      int nn = 0;
      for (int k = st; k < st + x; k++) b[++nn] = a[k];
      sort(b + 1, b + nn + 1);
      for (int k = st + x; k < st + 2 * x; k++) {
        int id = upper_bound(b + 1, b + nn + 1, a[k]) - b;
        int jd = lower_bound(b + 1, b + nn + 1, a[k]) - b;
        f[i] += nn - id + 1;
        g[i] += jd - 1;
      }
    }
  }
  int m;
  scanf("%d", &m);
  while (m--) {
    int x;
    scanf("%d", &x);
    for (int i = 1; i <= x; i++) swap(f[i], g[i]);
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans += f[i];
    printf("%I64d\n", ans);
  }
  return 0;
}
