#include <bits/stdc++.h>
using namespace std;
long long int h, n, m, inv[25][2] = {0}, q;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> tp;
void srt1(int l, int r, int ht) {
  int i = l, j = ((l + r) / 2) + 1, mid = (l + r) / 2, ct = 0,
      sz = (r - l + 1) / 2;
  while (i <= mid && j <= r) {
    if (b[i] <= b[j]) {
      tp[ct] = b[i];
      ct++;
      i++;
    } else {
      tp[ct] = b[j];
      ct++;
      j++;
      inv[ht][0] += mid - i + 1;
    }
  }
  while (i <= mid) {
    tp[ct] = b[i];
    ct++;
    i++;
  }
  while (j <= r) {
    tp[ct] = b[j];
    ct++;
    j++;
    inv[ht][0] += mid - i + 1;
  }
  for (i = l; i <= r; i++) {
    b[i] = tp[i - l];
  }
}
void srt2(int l, int r, int ht) {
  int i = l, j = ((l + r) / 2) + 1, mid = (l + r) / 2, ct = 0,
      sz = (r - l + 1) / 2;
  while (i <= mid && j <= r) {
    if (c[i] >= c[j]) {
      tp[ct] = c[i];
      ct++;
      i++;
    } else {
      tp[ct] = c[j];
      ct++;
      j++;
      inv[ht][1] += mid - i + 1;
    }
  }
  while (i <= mid) {
    tp[ct] = c[i];
    ct++;
    i++;
  }
  while (j <= r) {
    tp[ct] = c[j];
    ct++;
    j++;
    inv[ht][1] += mid - i + 1;
  }
  for (i = l; i <= r; i++) {
    c[i] = tp[i - l];
  }
}
void ms(int l, int r, int ht) {
  if (l == r) {
    return;
  }
  ms(l, (l + r) / 2, ht - 1);
  ms(((l + r) / 2) + 1, r, ht - 1);
  srt1(l, r, ht);
  srt2(l, r, ht);
}
int main(int argc, const char* argv[]) {
  cin >> h;
  n = 1 << h;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  tp.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
    c[i] = a[i];
  }
  ms(0, n - 1, h);
  cin >> m;
  for (int i = 0; i < m; i++) {
    long long int ans = 0;
    scanf(" %I64d", &q);
    for (int j = q; j >= 0; j--) {
      swap(inv[j][0], inv[j][1]);
    }
    for (int j = 0; j <= h; j++) {
      ans += inv[j][0];
    }
    cout << ans << endl;
  }
}
