#include <bits/stdc++.h>
using namespace std;
const int MAX = 2001000;
int n, m, N;
int A[MAX];
long long Sl[MAX], Sr[MAX];
int v[MAX];
void srt(int l, int r, int s, int lvl) {
  if (l == r)
    return;
  else {
    srt(l, (l + r) / 2, s / 2, lvl - 1);
    srt((l + r) / 2 + 1, r, s / 2, lvl - 1);
    int cnt = 0, id = 0;
    int s2 = s / 2;
    int a = l, b = (l + r) / 2 + 1;
    while (true) {
      if (a == (l + r) / 2 + 1 && b == r + 1)
        break;
      else if (a == (l + r) / 2 + 1) {
        Sl[lvl] += a - l;
        v[id++] = A[b++];
      } else if (b == (r + 1))
        v[id++] = A[a++];
      else {
        if (A[a] >= A[b]) {
          Sl[lvl] += a - l;
          v[id++] = A[b++];
        } else
          v[id++] = A[a++];
      }
    }
    a = (l + r) / 2;
    b = r;
    while (true) {
      if (a == l - 1 && b == (l + r) / 2)
        break;
      else if (a == l - 1) {
        Sr[lvl] += (l + r) / 2 - a;
        b--;
      } else if (b == (l + r) / 2)
        a--;
      else {
        if (A[a] <= A[b]) {
          Sr[lvl] += (l + r) / 2 - a;
          b--;
        } else
          a--;
      }
    }
    for (int i = 0; i < s; i++) A[l + i] = v[i];
  }
}
int main() {
  scanf("%d", &n);
  N = (1 << n);
  for (int i = 0; i < int(N); i++) scanf("%d", A + i);
  srt(0, N - 1, N, n);
  scanf("%d", &m);
  for (int i = 0; i < int(m); i++) {
    int q;
    scanf("%d", &q);
    long long ansD = 0;
    for (int i = 0; i <= q; i++) swap(Sl[i], Sr[i]);
    for (int i = 0; i <= n; i++) ansD += Sr[i];
    printf("%lld\n", ansD);
  }
}
