#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000 * 1000 + 10;
long long a[MAXN], t[2][50];
int n;
vector<int> f(int s, int e, int p) {
  if (p == 0) {
    t[0][p] = t[1][p] = 0;
    return vector<int>(1, a[s]);
  }
  int m = (s + e) / 2;
  vector<int> x = f(s, m, p - 1), y = f(m, e, p - 1), z;
  int i = 0, j = 0;
  long long k1 = 0, k2 = 0;
  while (i < (int)x.size() && j < (int)y.size())
    if (x[i] < y[j])
      z.push_back(x[i]), i++, k2 += ((int)y.size() - j);
    else if (y[j] < x[i])
      z.push_back(y[j]), j++, k1 += ((int)x.size() - i);
    else {
      int k = x[i];
      int u = i, v = j;
      while (u < (int)x.size() && x[u] == k) z.push_back(x[u]), u++;
      while (v < (int)y.size() && y[v] == k) z.push_back(y[v]), v++;
      k1 += 1LL * (v - j) * ((int)x.size() - u);
      k2 += 1LL * (u - i) * ((int)y.size() - v);
      i = u, j = v;
    }
  while (i < (int)x.size()) z.push_back(x[i]), i++;
  while (j < (int)y.size()) z.push_back(y[j]), j++;
  t[0][p] += k1;
  t[1][p] += k2;
  return z;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < (1 << n); i++) cin >> a[i];
  f(0, (1 << n), n);
  int m;
  cin >> m;
  while (m--) {
    int k;
    cin >> k;
    long long sum = 0;
    for (int i = 0; i < k + 1; i++) swap(t[0][i], t[1][i]);
    for (int i = 0; i < n + 1; i++) sum += t[0][i];
    cout << sum << endl;
  }
  return 0;
}
