#include <bits/stdc++.h>
using namespace std;
long n, input[1200000];
vector<long> v;
map<long, long> mapp;
long calc;
long long span, tinv[200], linv[200];
long long p;
vector<pair<long, long> > ad;
long tests;
long lvl;
long long answ;
long vars[1200000];
class Fenwick {
  long *m, *mt, N;

 public:
  Fenwick(long n);
  Fenwick(long a[], long n);
  void add_range(long l, long d);
  void add_range(long l, long r, long d);
  long sum(long r);
  long sum(long l, long r);
};
Fenwick::Fenwick(long n) {
  N = n;
  m = new long[N];
  mt = new long[N];
  for (int i = 0; i < n; i++) m[i] = mt[i] = 0;
}
void Fenwick::add_range(long r, long d) {
  if (r < 0) return;
  for (int i = r; i >= 0; i = (i & (i + 1)) - 1) mt[i] += d;
  for (int i = r | (r + 1); i < N; i |= i + 1)
    m[i] += d * (r - (i & (i + 1)) + 1);
}
void Fenwick::add_range(long l, long r, long d) {
  add_range(r, d);
  add_range(l - 1, -d);
}
long Fenwick::sum(long r) {
  if (r < 0) return 0;
  long long res = 0;
  for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
    res += m[i] + mt[i] * (i - (i & (i + 1)) + 1);
  for (int i = r | (r + 1); i < N; i |= i + 1)
    res += mt[i] * (r - (i & (i + 1)) + 1);
  return res;
}
long Fenwick::sum(long l, long r) { return sum(r) - sum(l - 1); }
int main() {
  cin >> n;
  for (int i = 0; i < (1 << n); i++) {
    scanf("%d", &input[i]);
    vars[i] = input[i];
    v.push_back(input[i]);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (i == 0 || v[i] != v[i - 1]) {
      mapp[v[i]] = calc + 1;
      calc++;
    }
  }
  for (int i = 0; i < (1 << n); i++) {
    input[i] = mapp[input[i]];
    ad.push_back(make_pair(input[i], -i));
  }
  sort(ad.begin(), ad.end());
  Fenwick inv(1100100);
  for (int pp = 0; pp < (1 << n); pp++) {
    long i = -ad[pp].second;
    for (int lev = 1; lev <= n; lev++) {
      span = i % (1 << lev);
      p = i - span;
      if (span < (1 << (lev - 1))) continue;
      linv[lev] += inv.sum(p, p + (1 << (lev - 1)) - 1);
    }
    inv.add_range(-ad[pp].second, -ad[pp].second, 1);
  }
  for (int lev = 1; lev <= n; lev++) tinv[lev] = linv[lev];
  Fenwick inv1(1100100);
  ad.clear();
  for (int i = 0; i < (1 << n); i++) {
    input[i] = calc - input[i] + 1;
    ad.push_back(make_pair(-input[i], i));
  }
  sort(ad.begin(), ad.end());
  for (int pp = 0; pp < (1 << n); pp++) {
    long i = ad[pp].second;
    for (int lev = 1; lev <= n; lev++) {
      span = i % (1 << lev);
      p = i - span + (1 << (lev - 1));
      if (span >= (1 << (lev - 1))) continue;
      tinv[lev] += inv1.sum(p, p + (1 << (lev - 1)) - 1);
    }
    inv1.add_range(ad[pp].second, ad[pp].second, 1);
  }
  cin >> tests;
  for (; tests; --tests) {
    scanf("%d", &lvl);
    for (int i = lvl; i > 0; --i) linv[i] = tinv[i] - linv[i];
    answ = 0;
    for (int i = 1; i <= n; i++) answ += linv[i];
    printf("%I64d\n", answ);
  }
  cin.get();
  cin.get();
  return 0;
}
