#include <bits/stdc++.h>
using namespace std;
int n, q, v[1058576];
long long sum[30], inv[30], ans, m, I[4 * 1058576];
void build(int nod = 1, int a = 1, int b = (1 << n), int deep = n) {
  if (a == b) return;
  build((2 * nod), a, ((a + b) / 2), deep - 1),
      build((2 * nod + 1), ((a + b) / 2) + 1, b, deep - 1);
  long long aux = 0, eq = 0;
  for (int i = a; i <= ((a + b) / 2); i++) {
    int x = v[i];
    long long p = (lower_bound(v + ((a + b) / 2) + 1, v + b + 1, x) -
                   (v + ((a + b) / 2) + 1));
    aux += (long long)p;
  }
  sort(v + a, v + b + 1);
  for (int i = a; i <= b; i++) {
    int x = v[i];
    if (v[i] == v[i - 1] and i != a) continue;
    long long p = (lower_bound(v + a, v + b + 1, x) - (v + a + 1));
    long long pp = (upper_bound(v + a, v + b + 1, x) - (v + a + 1));
    eq += (pp - p) * (pp - p - 1) / 2;
  }
  I[nod] = I[(2 * nod)] + I[(2 * nod + 1)] + aux;
  inv[deep] += I[nod];
  sum[deep] += (((long long)b - (long long)a + 1LL) *
                ((long long)b - (long long)a) / 2) -
               eq;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= (1 << n); i++) cin >> v[i];
  build();
  cin >> m;
  ans = I[1];
  for (int i = 1; i <= m; i++) {
    cin >> q;
    ans = ans + sum[q] - 2LL * inv[q];
    for (int j = n; j >= q; j--) inv[j] += sum[q] - 2LL * inv[q];
    for (int j = 0; j < q; j++) inv[j] += sum[j] - 2LL * inv[j];
    cout << inv[n] << "\n";
  }
}
