#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
const int N = 1111111;
long long v[N];
long long nor[N], inv[N];
int n;
vector<int> merge(int l, int r, int lvl) {
  if (lvl == 0) {
    vector<int> foo;
    foo.push_back(v[l]);
    return foo;
  }
  int mid = (l + r) >> 1;
  vector<int> p1 = merge(l, mid, lvl - 1);
  vector<int> p2 = merge(mid + 1, r, lvl - 1);
  int i, j;
  i = j = 0;
  vector<int> foo;
  while (i < p1.size() && j < p2.size()) {
    long long tx = 0, ty = 0;
    int curr = min(p1[i], p2[j]);
    while (i < p1.size() && p1[i] == curr) i++, tx++;
    while (j < p2.size() && p2[j] == curr) j++, ty++;
    nor[lvl] += ty * (p1.size() - i);
    inv[lvl] += tx * (p2.size() - j);
    for (int k = 0; k < tx + ty; ++k) foo.push_back(curr);
  }
  while (i < p1.size()) foo.push_back(p1[i++]);
  while (j < p2.size()) foo.push_back(p2[j++]);
  return foo;
}
int main() {
  memset(nor, 0, sizeof nor);
  memset(inv, 0, sizeof inv);
  scanf("%d", &n);
  for (int i = 0; i < (1 << n); ++i) scanf("%lld", v + i);
  merge(0, (1 << n) - 1, n);
  int q;
  scanf("%d", &q);
  while (q--) {
    int k;
    scanf("%d", &k);
    for (int i = 0; i <= k; ++i) swap(nor[i], inv[i]);
    long long ans = 0;
    for (int i = 0; i <= n; ++i) ans += nor[i];
    printf("%lld\n", ans);
  }
  return 0;
}
