#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long MAX1 = (1 << 20) + 5;
const long long MAX2 = 20 * 2 + 5;
long long tab[MAX1];
long long inv1[MAX2], inv2[MAX2];
vector<long long> msort(long long lvl, long long l, long long r) {
  if (lvl == 0) return {tab[l]};
  long long mid = (l + r) >> 1;
  vector<long long> v1 = msort(lvl - 1, l, mid),
                    v2 = msort(lvl - 1, mid + 1, r), v;
  long long w1 = 0, w2 = 0;
  while (w1 < (long long)v1.size() && w2 < (long long)v2.size()) {
    long long x = min(v1[w1], v2[w2]), roz1 = 0, roz2 = 0;
    while (w1 < (long long)v1.size() && v1[w1] == x)
      roz1++, v.push_back(v1[w1]), w1++;
    while (w2 < (long long)v2.size() && v2[w2] == x)
      roz2++, v.push_back(v2[w2]), w2++;
    inv1[lvl] += roz2 * ((long long)v1.size() - w1);
    inv2[lvl] += roz1 * ((long long)v2.size() - w2);
  }
  while (w1 < (long long)v1.size()) v.push_back(v1[w1++]);
  while (w2 < (long long)v2.size()) v.push_back(v2[w2++]);
  return v;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= (1 << n); i++) cin >> tab[i];
  msort(n, 1, (1 << n));
  long long q;
  cin >> q;
  for (long long z = 1; z <= q; z++) {
    long long ans = 0;
    long long lvl;
    cin >> lvl;
    for (long long i = 0; i <= lvl; i++) swap(inv1[i], inv2[i]);
    for (long long i = 0; i <= n; i++) ans += inv1[i];
    cout << ans << "\n";
  }
  return 0;
}
