#include <bits/stdc++.h>
using namespace std;
int a[2000000];
int b[2000000];
long long q[30];
long long ans[30];
long long eql[30];
int n;
long long Eql(int l, int r, int len) {
  long long ans = 0;
  int ll = l + len - 1;
  int rr = r + len - 1;
  while (l <= ll && r <= rr) {
    if (a[l] == a[r]) {
      int l1 = l;
      int r1 = r;
      while (l1 + 1 <= ll && a[l1 + 1] == a[l]) l1++;
      while (r1 + 1 <= rr && a[r1 + 1] == a[r]) r1++;
      ans = ans + (l1 - l + 1) * 1LL * (r1 - r + 1);
      l = l1 + 1;
      r = r1 + 1;
    } else if (a[l] < a[r]) {
      l++;
    } else {
      r++;
    }
  }
  return ans;
}
long long Mer(int l, int r, int len) {
  long long ans = 0;
  int p = 0;
  int ll = l + len - 1;
  int rr = r + len - 1;
  while (l <= ll && r <= rr) {
    if (a[l] <= a[r]) {
      p++;
      b[p] = a[l];
      l++;
    } else {
      p++;
      b[p] = a[r];
      ans = ans + (ll - l) + 1;
      r++;
    }
  }
  while (l <= ll) {
    p++;
    b[p] = a[l];
    l++;
  }
  while (r <= rr) {
    p++;
    b[p] = a[r];
    r++;
  }
  while (p) {
    a[rr] = b[p];
    rr--;
    p--;
  }
  return ans;
}
int main() {
  q[0] = 1;
  for (int i = 1; i <= 30; i++) q[i] = 2 * q[i - 1];
  scanf("%d", &n);
  int p = q[n];
  for (int i = 1; i <= p; i++) {
    scanf("%d", &a[i]);
  }
  int k = 1;
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= n; i++) {
    k = k * 2;
    for (int j = 1; j <= p / k; j++) {
      eql[i] = eql[i] + Eql((j - 1) * k + 1, (j - 1) * k + 1 + k / 2, k / 2);
      ans[i] = ans[i] + Mer((j - 1) * k + 1, (j - 1) * k + 1 + k / 2, k / 2);
    }
  }
  int m, x;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &x);
    for (int j = 1; j <= x; j++) {
      ans[j] = q[n] * q[j] / 4 - eql[j] - ans[j];
    }
    long long Ans = 0;
    for (int j = 1; j <= n; j++) Ans += ans[j];
    printf("%lld\n", Ans);
  }
}
