#include <bits/stdc++.h>
using namespace std;
int kkaass = 1;
struct data {
  int level, u, v;
};
int n;
int ar[(1 << 20)];
pair<long long, long long> ans[25];
void ini(int left, int right, int lvl) {
  if (left == right) return;
  int mid = (left + right) / 2;
  ini(left, mid, lvl + 1);
  ini(mid + 1, right, lvl + 1);
  vector<int> L, R;
  for (int i = left; i <= mid; i++) L.push_back(ar[i]);
  for (int i = mid + 1; i <= right; i++) R.push_back(ar[i]);
  sort(L.begin(), L.end());
  sort(R.begin(), R.end());
  for (int i = 0; i < (int)L.size(); i++) {
    ans[lvl].first += (R.end() - upper_bound(R.begin(), R.end(), L[i]));
    ans[lvl].second += (lower_bound(R.begin(), R.end(), L[i]) - R.begin());
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  int i, j, k;
  while (cin >> n) {
    memset(ans, 0, sizeof(ans));
    for (i = 0; i < ((1 << n)); i++) cin >> ar[i];
    ini(0, (1 << n) - 1, 0);
    int q;
    cin >> q;
    while (q--) {
      int v;
      cin >> v;
      long long ret = 0;
      for (int i = n - v; i <= n; i++) swap(ans[i].first, ans[i].second);
      for (int i = 0; i <= n; i++) ret += ans[i].second;
      cout << ret << endl;
    }
  }
  return 0;
}
