#include <bits/stdc++.h>
using namespace std;
long long a[2000005], c[2000005], inver[25][2], hass[25];
void calc(int h, int low, int high) {
  if (h == 0) return;
  int l = low, r = (low + high) / 2;
  calc(h - 1, l, r);
  calc(h - 1, r, high);
  int choose;
  int i = 0;
  while (i < high - low) {
    if (l >= (low + high) / 2)
      choose = 1;
    else if (r >= high)
      choose = 0;
    else if (a[l] < a[r]) {
      inver[h][1] += high - r;
      choose = 0;
    } else if (a[r] < a[l]) {
      inver[h][0] += (low + high) / 2 - l;
      choose = 1;
    } else {
      int same = a[l];
      int Left = 0, Right = 0;
      while (l < (low + high) / 2 && a[l] == same) {
        c[i] = same;
        i++;
        Left++;
        l++;
      }
      while (r < high && a[r] == same) {
        c[i] = same;
        i++;
        Right++;
        r++;
      }
      inver[h][0] += Right * ((low + high) / 2 - l);
      inver[h][1] += Left * (high - r);
      continue;
    }
    if (choose == 0) {
      c[i] = a[l];
      l++;
    } else {
      c[i] = a[r];
      r++;
    }
    i++;
  }
  for (int i = low; i < high; i++) a[i] = c[i - low];
}
long long query(int h, int n) {
  long long ans = 0;
  for (int i = 0; i <= h; i++) hass[i] = 1 - hass[i];
  for (int i = 0; i <= n; i++) ans += inver[i][hass[i]];
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, m, q;
  cin >> n;
  for (int i = 0; i < (1 << n); i++) cin >> a[i];
  calc(n, 0, (1 << n));
  cin >> m;
  while (m--) {
    cin >> q;
    cout << query(q, n) << endl;
  }
  return 0;
}
