#include <bits/stdc++.h>
using namespace std;
int n, m, que;
int el[2000000], du[2000000];
long long cpy[2][2000000], ans;
void merge1(int ls, int le, int rs, int re, int de) {
  int size = re - ls + 1;
  int temp[size];
  int ind = 0;
  int lind = ls;
  int rind = rs;
  long long c = 0;
  int temp2[size];
  int ind2 = 0;
  int lind2 = ls;
  int rind2 = rs;
  long long c2 = 0;
  while (lind <= le && rind <= re) {
    if (el[lind] <= el[rind]) {
      temp[ind] = el[lind];
      lind++;
    } else {
      c += le - lind + 1;
      temp[ind] = el[rind];
      rind++;
    }
    ind++;
  }
  while (lind <= le) {
    temp[ind] = el[lind];
    lind++;
    ind++;
  }
  while (rind <= re) {
    temp[ind] = el[rind];
    rind++;
    ind++;
  }
  cpy[0][de] += c;
  while (lind2 <= le && rind2 <= re) {
    if (du[lind2] <= du[rind2]) {
      temp2[ind2] = du[lind2];
      lind2++;
    } else {
      c2 += le - lind2 + 1;
      temp2[ind2] = du[rind2];
      rind2++;
    }
    ind2++;
  }
  while (lind2 <= le) {
    temp2[ind2] = du[lind2];
    lind2++;
    ind2++;
  }
  while (rind2 <= re) {
    temp2[ind2] = du[rind2];
    rind2++;
    ind2++;
  }
  cpy[1][de] += c2;
  for (int i = 0; i < size; i++) {
    el[i + ls] = temp[i];
    du[i + ls] = temp2[i];
  }
}
void ms(int l, int r, int d) {
  if (l != r) {
    int mid = (l + r) / 2;
    ms(l, mid, d - 1);
    ms(mid + 1, r, d - 1);
    merge1(l, mid, mid + 1, r, d);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int end = 1 << n;
  for (int i = 1; i <= end; i++) {
    cin >> el[i];
    du[end - i + 1] = el[i];
  }
  ms(1, end, n);
  cin >> m;
  while (m--) {
    cin >> que;
    ans = 0;
    for (int i = 1; i <= que; i++) swap(cpy[0][i], cpy[1][i]);
    for (int i = 1; i <= n; i++) ans += cpy[0][i];
    cout << ans << endl;
  }
  return 0;
}
