#include <bits/stdc++.h>
using namespace std;
const int maxn = 21;
const int maxlen = 1 << maxn;
int arr[maxlen];
long long correct[maxn], inverse[maxn];
long long cor, inv;
void solve(int l, int r) {
  cor = 0;
  inv = 0;
  int mid = (l + r) / 2;
  vector<int> seq(mid - l + 1);
  for (int i = l; i <= mid; i++) seq[i - l] = arr[i];
  sort(seq.begin(), seq.end());
  for (int i = mid + 1; i <= r; i++) {
    cor = cor + (lower_bound(seq.begin(), seq.end(), arr[i]) - seq.begin());
    inv = inv + ((r - mid) -
                 (upper_bound(seq.begin(), seq.end(), arr[i]) - seq.begin()));
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, valn, m, q;
  cin >> n;
  valn = 1 << n;
  for (int i = 1; i <= valn; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i <= n; i++) {
    int len = 1 << i;
    int lim = valn / len;
    for (int j = 0; j < lim; j++) {
      solve(j * len + 1, (j + 1) * len);
      correct[i] += cor;
      inverse[i] += inv;
    }
  }
  cin >> m;
  while (m--) {
    cin >> q;
    for (int i = q; i >= 0; i--) swap(correct[i], inverse[i]);
    long long ans = 0;
    for (int i = 0; i <= n; i++) ans += inverse[i];
    cout << ans << '\n';
  }
  return 0;
}
