#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 20) + 10;
const int M = 22;
int a[N], n, tmp[N];
long long p[M], q[M];
void build(int l, int r, int dep) {
  if (l == r) {
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, dep - 1);
  build(mid + 1, r, dep - 1);
  int po = mid + 1, x = 0;
  long long pcnt = 0, qcnt = 0;
  for (int i = l; i <= mid; i++) {
    while (po <= r && a[i] > a[po]) {
      pcnt += mid - i + 1;
      po++;
    }
  }
  p[dep] += pcnt;
  po = mid + 1;
  for (int i = l; i <= mid; i++) {
    while (po <= r && a[i] >= a[po]) {
      tmp[x++] = a[po];
      qcnt += i - l;
      po++;
    }
    tmp[x++] = a[i];
  }
  while (po <= r) {
    tmp[x++] = a[po++];
    qcnt += mid - l + 1;
  }
  q[dep] += qcnt;
  for (int i = l; i <= r; i++) {
    a[i] = tmp[i - l];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < 1 << n; i++) {
    scanf("%d", a + i);
  }
  build(0, (1 << n) - 1, n);
  int nq, x;
  scanf("%d", &nq);
  while (nq--) {
    scanf("%d", &x);
    for (int i = x; i >= 0; i--) swap(p[i], q[i]);
    long long ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += p[i];
    }
    printf("%lld\n", ans);
  }
  return 0;
}
