#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 20) + 10;
int a[N];
long long ans[N], rans[N];
void merge_sort(int s, int e, int t) {
  if (e - s < 2) return;
  int mid = (s + e) / 2;
  merge_sort(s, mid, t + 1);
  merge_sort(mid, e, t + 1);
  for (int i = s; i <= mid - 1; i++) {
    ans[t] += lower_bound(a + mid, a + e, a[i]) - (a + mid);
    long long x = e - mid;
    x -= upper_bound(a + mid, a + e, a[i]) - (a + mid);
    rans[t] += x;
  }
  sort(a + s, a + e);
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i <= (1 << n) - 1; i++) cin >> a[i];
  merge_sort(0, (1 << n), 0);
  int m;
  cin >> m;
  while (m--) {
    int q;
    cin >> q;
    for (int i = n - q; i <= n; i++) swap(ans[i], rans[i]);
    long long x = 0;
    for (int i = 0; i <= n; i++) x += ans[i];
    cout << x << endl;
  }
  return 0;
}
