#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6;
int tmp[maxn];
int a[maxn], b[maxn];
long long sum[maxn], sum2[maxn];
long long merge_(int a[maxn], int s, int e) {
  long long num = 0;
  int t = (s + e) / 2;
  int fIndex = s, sIndex = t, j = s;
  while (j < e) {
    if ((sIndex < e && a[fIndex] > a[sIndex]) || fIndex == t) {
      num += (t - fIndex);
      tmp[j++] = a[sIndex++];
    } else {
      tmp[j++] = a[fIndex++];
    }
  }
  for (int i = s; i < e; i++) a[i] = tmp[i];
  return num;
}
void merge_sort(int a[maxn], long long sum[maxn], int s, int e, int lv = 0) {
  if (s == e - 1) return;
  int t = (s + e) / 2;
  merge_sort(a, sum, s, t, lv + 1);
  merge_sort(a, sum, t, e, lv + 1);
  sum[lv] += merge_(a, s, e);
}
int n, m, q, tp;
int main() {
  ios::sync_with_stdio(false);
  memset(sum, 0, sizeof(sum));
  scanf("%d", &n);
  m = n;
  n = (1 << n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  reverse(b, b + n);
  merge_sort(a, sum, 0, n);
  merge_sort(b, sum2, 0, n);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &tp);
    tp = m - tp;
    for (int i = tp; i < m + 1; i++) {
      swap(sum[i], sum2[i]);
    }
    long long k = 0;
    for (int i = 0; i < m + 1; i++) k += (long long)sum[i];
    printf("%I64d\n", k);
  }
  return 0;
}
