#include <bits/stdc++.h>
using namespace std;
const long long INF = 2000000000LL;
const long double EPS = 1e-9;
const int NP = 22;
const int N = (1 << 20) + 7;
long long c1[NP], c2[NP];
int a[N];
int n, np;
vector<int> inv(int lev, int l, int r) {
  if (l == r) {
    vector<int> ret(1, a[l]);
    return ret;
  }
  int mid = (l + r) / 2;
  auto x = inv(lev - 1, l, mid);
  auto y = inv(lev - 1, mid + 1, r);
  vector<int> z;
  int i = 0, j = 0;
  while (i < (int)x.size() and j < (int)y.size()) {
    int cur = min(x[i], y[j]);
    long long cx = 0, cy = 0;
    while (i < (int)x.size() and x[i] == cur) ++i, ++cx;
    while (j < (int)y.size() and y[j] == cur) ++j, ++cy;
    c1[lev - 1] += cy * (x.size() - i);
    c2[lev - 1] += cx * (y.size() - j);
    for (int k = 0; k < cx + cy; ++k) z.push_back(cur);
  }
  while (i < (int)x.size()) z.push_back(x[i++]);
  while (j < (int)y.size()) z.push_back(y[j++]);
  return z;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> np;
  n = (1 << np);
  for (int i = 1; i <= n; ++i) cin >> a[i];
  memset(c1, 0, sizeof c1);
  memset(c2, 0, sizeof c2);
  inv(np, 1, n);
  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) swap(c1[i], c2[i]);
    long long res = 0;
    for (int i = 0; i < np; ++i) res += c1[i];
    cout << res << "\n";
  }
  return 0;
}
