#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline long long toll(string s) {
  long long v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
int n;
long long invLevel[22];
long long revInvLevel[22];
bool isRev[22];
vector<int> mergeSort(vector<int> arr, int lev, int len) {
  if (len == 1 && !lev) return arr;
  vector<int> l, r, res(len);
  for (int i = 0; i < len >> 1; i++) l.push_back(arr[i]);
  for (int i = len >> 1; i < len; i++) r.push_back(arr[i]);
  l = mergeSort(l, lev - 1, len >> 1);
  r = mergeSort(r, lev - 1, len >> 1);
  int idx = 0, idxL = 0, idxR = 0;
  long long inv = 0;
  long long revInv = 0;
  while (idx < len) {
    if (idxR == len >> 1) {
      res[idx++] = l[idxL++];
      continue;
    }
    if (idxL == len >> 1) {
      res[idx++] = r[idxR++];
      continue;
    }
    if (l[idxL] <= r[idxR])
      res[idx++] = l[idxL++];
    else {
      inv += (len >> 1) - idxL;
      res[idx++] = r[idxR++];
    }
  }
  idx = 0, idxL = 0, idxR = 0;
  while (idx < len) {
    if (idxR == len >> 1) break;
    if (idxL == len >> 1) break;
    if (r[idxR] <= l[idxL])
      idx++, idxR++;
    else {
      revInv += (len >> 1) - idxR;
      idx++, idxL++;
    }
  }
  invLevel[lev] += inv;
  revInvLevel[lev] += revInv;
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  vector<int> v(1 << n);
  for (int i = 0; i < (1 << n); i++) cin >> v[i];
  mergeSort(v, n, 1 << n);
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    long long res = 0;
    for (int i = 0; i < 22; i++) {
      if (i <= x) isRev[i] = !isRev[i];
      res += isRev[i] ? revInvLevel[i] : invLevel[i];
    }
    cout << res << endl;
  }
  return 0;
}
