#include <bits/stdc++.h>
using namespace std;
const long long MAX_N = 21;
long long arr[1 << MAX_N], tmp[1 << MAX_N], cpy[1 << MAX_N];
long long inv[MAX_N], inv1[MAX_N];
void mergesort(int l, int r, int h) {
  if (l == r) {
    tmp[l] = arr[l];
    return;
  }
  int m = (l + r) / 2;
  mergesort(l, m, h - 1);
  mergesort(m + 1, r, h - 1);
  int li = 0, ri = 0;
  while (l + li <= m || m + ri + 1 <= r) {
    if (l + li <= m && m + ri + 1 <= r) {
      if (arr[l + li] <= arr[m + ri + 1]) {
        tmp[l + li + ri] = arr[l + li];
        li++;
        inv[h] += ri;
      } else {
        tmp[l + li + ri] = arr[m + ri + 1];
        ri++;
      }
    } else if (l + li <= m) {
      tmp[l + li + ri] = arr[l + li];
      li++;
      inv[h] += ri;
    } else {
      tmp[l + li + ri] = arr[m + ri + 1];
      ri++;
    }
  }
  for (int i = l; i <= r; i++) {
    arr[i] = tmp[i];
  }
}
void mergesort1(int l, int r, int h) {
  if (l == r) {
    tmp[l] = arr[l];
    return;
  }
  int m = (l + r) / 2;
  mergesort1(l, m, h - 1);
  mergesort1(m + 1, r, h - 1);
  int li = 0, ri = 0;
  while (l + li <= m || m + ri + 1 <= r) {
    if (l + li <= m && m + ri + 1 <= r) {
      if (arr[l + li] < arr[m + ri + 1]) {
        tmp[l + li + ri] = arr[l + li];
        li++;
      } else {
        tmp[l + li + ri] = arr[m + ri + 1];
        ri++;
        inv1[h] += li;
      }
    } else if (l + li <= m) {
      tmp[l + li + ri] = arr[l + li];
      li++;
    } else {
      tmp[l + li + ri] = arr[m + ri + 1];
      ri++;
      inv1[h] += li;
    }
  }
  for (int i = l; i <= r; i++) {
    arr[i] = tmp[i];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < (1 << n); i++) {
    cin >> cpy[i];
  }
  for (int i = 0; i < (1 << n); i++) {
    arr[i] = cpy[i];
  }
  mergesort(0, (1 << n) - 1, n);
  for (int i = 0; i < (1 << n); i++) {
    arr[i] = cpy[i];
  }
  mergesort1(0, (1 << n) - 1, n);
  long long q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long k;
    cin >> k;
    for (int j = k; j >= 0; j--) {
      swap(inv[j], inv1[j]);
    }
    long long ans = 0;
    for (int j = 0; j <= n; j++) {
      ans += inv[j];
    }
    cout << ans << "\n";
  }
  return 0;
}
