#include <bits/stdc++.h>
using namespace std;
const int MAX = (1 << 21) + 5;
int n, m;
int a[MAX];
long long tree[22][2];
void f(int l, int r, int lev) {
  if (l >= r) return;
  int mid = (l + r) >> 1;
  f(l, mid, lev + 1);
  f(mid + 1, r, lev + 1);
  for (int i = l; i <= mid; ++i) {
    tree[lev][0] += lower_bound(a + mid + 1, a + r + 1, a[i]) - (a + mid + 1);
  }
  for (int i = mid + 1; i <= r; ++i) {
    tree[lev][1] += lower_bound(a + l, a + mid + 1, a[i]) - (a + l);
  }
  sort(a + l, a + r + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (1 << n); ++i) {
    scanf("%d", a + i);
  }
  f(0, (1 << n) - 1, 1);
  long long ans = 0ll;
  for (int i = 1; i <= n + 1; ++i) ans += tree[i][0];
  scanf("%d", &m);
  while (m--) {
    int q;
    scanf("%d", &q);
    for (int i = n - q + 1; i <= n + 1; ++i) {
      ans += (tree[i][1] - tree[i][0]);
      swap(tree[i][1], tree[i][0]);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
