#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 20;
int a[N], b[N];
long long rp[25], p[25];
void s(int l, int r, int d) {
  if (l >= r) return;
  int m = l + r >> 1;
  s(l, m, d - 1);
  s(m + 1, r, d - 1);
  int i = l, j = m + 1, cnt = 0;
  while (i <= m && j <= r) {
    if (a[i] > a[j]) {
      b[cnt++] = a[j];
      j++;
      rp[d] += m - i + 1;
      p[d] += (i - l);
    } else if (a[i] < a[j]) {
      b[cnt++] = a[i];
      i++;
    } else {
      int tmp = a[i], c = 0;
      while (j <= r && a[j] == tmp) {
        b[cnt++] = a[j++];
        c++;
      }
      p[d] += (i - l) * c;
      while (i <= m && a[i] == tmp) {
        b[cnt++] = a[i++];
      }
      rp[d] += (m - i + 1) * c;
    }
  }
  while (i <= m) {
    b[cnt++] = a[i];
    i++;
  }
  while (j <= r) {
    b[cnt++] = a[j];
    j++;
    rp[d] += m - i + 1;
    p[d] += (i - l);
  }
  cnt = 0;
  for (i = l; i <= r; i++) {
    a[i] = b[cnt++];
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < (1 << n); i++) {
    scanf("%d", &a[i]);
  }
  int m;
  s(0, (1 << n) - 1, n);
  scanf("%d", &m);
  long long res = 0;
  for (int i = 0; i <= n; i++) {
    res += rp[i];
  }
  for (int i = 0; i < m; i++) {
    int q;
    scanf("%d", &q);
    while (q) {
      res -= rp[q];
      res += p[q];
      swap(rp[q], p[q]);
      q--;
    }
    printf("%lld\n", res);
  }
}
