#include <bits/stdc++.h>
using namespace std;
long long arr[(1 << 21) + 20];
vector<long long> tree[4 * (1 << 21)];
long long normal[25];
long long rev[25];
bool check[25];
void merge_sort(long long node, long long a, long long b, long long lev) {
  if (a == b) {
    tree[node].push_back(arr[a]);
    return;
  }
  long long mid = (a + b) / 2, left, right;
  left = node * 2;
  right = left + 1;
  merge_sort(left, a, mid, lev + 1);
  merge_sort(right, mid + 1, b, lev + 1);
  long long i = 0, j = 0;
  while (1) {
    if (i == tree[left].size()) {
      for (long long ind = j; ind < tree[right].size(); ind++) {
        tree[node].push_back(tree[right][ind]);
      }
      break;
    }
    if (j == tree[right].size()) {
      for (long long ind = i; ind < tree[left].size(); ind++) {
        tree[node].push_back(tree[left][ind]);
      }
      break;
    }
    if (tree[left][i] > tree[right][j]) {
      normal[lev] += tree[left].size() - i;
      tree[node].push_back(tree[right][j]);
      j++;
    } else {
      tree[node].push_back(tree[left][i]);
      i++;
    }
  }
  i = j = 0;
  while (1) {
    if (i == tree[left].size()) break;
    if (j == tree[right].size()) break;
    if (tree[left][i] < tree[right][j]) {
      rev[lev] += tree[right].size() - j;
      i++;
    } else {
      j++;
    }
  }
}
int main() {
  long long i, j, k, l, m, n, o;
  long long testcase;
  long long input, flag, tag, ans;
  scanf("%lld", &n);
  long long sz = (1 << n);
  for (i = 1; i <= sz; i++) scanf("%lld", &arr[i]);
  merge_sort(1, 1, sz, 0);
  scanf("%lld", &m);
  for (i = 0; i < 24; i++) check[i] = 1;
  long long x;
  for (long long y = 1; y <= m; y++) {
    scanf("%lld", &x);
    long long lev = n - x;
    for (i = lev; i <= n; i++) {
      if (check[i])
        check[i] = 0;
      else
        check[i] = 1;
    }
    ans = 0;
    for (i = 0; i <= n; i++) {
      if (check[i])
        ans += normal[i];
      else
        ans += rev[i];
    }
    cout << ans << endl;
  }
}
