#include <bits/stdc++.h>
using namespace std;
inline int rei() {
  int x;
  cin >> x;
  return x;
}
inline long long rel() {
  long long x;
  cin >> x;
  return x;
}
inline string res() {
  string x;
  cin >> x;
  return x;
}
long long Olr[20];
long long Orl[20];
long long state[1 << 20];
void Calc() {
  int N = rei();
  vector<int> A(1 << N);
  vector<int> oA(1 << N);
  for (int i = 0; i < 1 << N; i++) {
    A[i] = rei();
  }
  for (int i = 0; i < N; i++) {
    int Block = 1 << (i + 1);
    for (int j = 0; j < 1 << N; j += Block) {
      {
        int p = j;
        int pl = p;
        int pr = p + Block / 2;
        int lf = p;
        int rf = pr;
        int lt = pr;
        int rt = p + Block;
        while (pl < lt && pr < rt) {
          if (A[pl] <= A[pr]) {
            oA[p] = A[pl];
            p++;
            pl++;
            Orl[i] += pr - rf;
          } else {
            oA[p] = A[pr];
            p++;
            pr++;
          }
        }
        while (pl < lt) {
          oA[p] = A[pl];
          p++;
          pl++;
          Orl[i] += pr - rf;
        }
        while (pr < rt) {
          oA[p] = A[pr];
          p++;
          pr++;
        }
      }
      {
        int p = j;
        int pl = p;
        int pr = p + Block / 2;
        int lf = p;
        int rf = pr;
        int lt = pr;
        int rt = p + Block;
        while (pl < lt && pr < rt) {
          if (A[pr] <= A[pl]) {
            p++;
            pr++;
            Olr[i] += pl - lf;
          } else {
            p++;
            pl++;
          }
        }
        while (pr < rt) {
          p++;
          pr++;
          Olr[i] += pl - lf;
        }
      }
    }
    swap(A, oA);
  }
  for (int i = 0; i < 1 << N; i++) {
    long long sum = 0;
    for (int j = 0; j < N; j++) {
      if (i & (1 << j)) {
        sum += Olr[j];
      } else {
        sum += Orl[j];
      }
    }
    state[i] = sum;
  }
  int Q = rei();
  int curstate = 0;
  for (int i = 0; i < Q; i++) {
    int x = rei();
    curstate = curstate ^ ((1 << x) - 1);
    cout << state[curstate] << "\n";
  }
}
int main(int argc, char** argv) {
  ios::sync_with_stdio(false), cin.tie(0);
  cout.tie(0);
  Calc();
  return 0;
}
