#include <bits/stdc++.h>
using namespace std;
const int N = 2020202;
int n, m;
int a[N], b[N], tmp[N];
long long ia[22], ib[22];
void Sort(int a[], int len, long long inv[], int _) {
  if (len == 1) {
    inv[_] = 0;
    return;
  }
  Sort(a, len / 2, inv, _ - 1);
  Sort(a + len / 2, len / 2, inv, _ - 1);
  int p = 0, q = len / 2;
  for (int i = (0); i < (len); i++) {
    if (p >= len / 2) {
      tmp[i] = a[q++];
    } else if (q >= len) {
      tmp[i] = a[p++];
    } else {
      if (a[p] <= a[q]) {
        tmp[i] = a[p++];
      } else {
        tmp[i] = a[q++];
        inv[_] += len / 2 - p;
      }
    }
  }
  for (int i = (0); i < (len); i++) a[i] = tmp[i];
}
int main() {
  while (~scanf("%d", &n)) {
    memset(ia, 0, sizeof(ia));
    memset(ib, 0, sizeof(ia));
    for (int i = (0); i < (1 << n); i++)
      scanf("%d", a + i), b[(1 << n) - i - 1] = a[i];
    Sort(a, 1 << n, ia, n);
    Sort(b, 1 << n, ib, n);
    scanf("%d", &m);
    while (m--) {
      int x;
      scanf("%d", &x);
      for (int i = (0); i < (x + 1); i++) swap(ia[i], ib[i]);
      long long ans = 0;
      for (int i = (0); i < (n + 1); i++) ans += ia[i];
      printf("%lld\n", ans);
    }
  }
  return 0;
}
