#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 21;
int a[N], n, m, q, b[N];
long long cnt[23][2];
bool rev[23];
int tree[21][N];
void merge_sort(int node, int s, int e, int l, int r) {
  int i = s, j = l, ptr = 0;
  long long cur = 0;
  while (i <= e) {
    if (j > r) {
      i++;
      cnt[node][0] += cur;
    } else if (tree[node - 1][i] <= tree[node - 1][j])
      i++, cnt[node][0] += cur;
    else
      j++, cur++;
  }
  i = s, j = l, ptr = 0;
  cur = 0;
  while (j <= r) {
    if (i > e) {
      j++;
      cnt[node][1] += cur;
    } else if (tree[node - 1][i] < tree[node - 1][j])
      i++, cur++;
    else
      j++, cnt[node][1] += cur;
  }
  i = s, j = l, ptr = 0;
  while (i <= e && j <= r) {
    if (tree[node - 1][i] <= tree[node - 1][j])
      tree[node][s + (ptr++)] = tree[node - 1][i++];
    else
      tree[node][s + (ptr++)] = tree[node - 1][j++];
  }
  while (i <= e) tree[node][s + (ptr++)] = tree[node - 1][i++];
  while (j <= r) tree[node][s + (ptr++)] = tree[node - 1][j++];
}
void solve(int node, int s, int e) {
  if (node == 0) {
    tree[node][s] = a[s];
    return;
  }
  int md = (s + e) >> 1;
  solve(node - 1, s, md);
  solve(node - 1, md + 1, e);
  merge_sort(node, s, md, md + 1, e);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (1 << n); i++) scanf("%d", a + i), b[i] = a[i];
  scanf("%d", &m);
  solve(n, 0, (1 << n) - 1);
  long long res = 0;
  while (m--) {
    scanf("%d", &q);
    for (int i = 1; i <= q; i++) rev[i] ^= 1;
    for (int i = 1; i <= n; i++) res += cnt[i][rev[i]];
    printf("%lld\n", res);
    res = 0;
  }
}
