#include <bits/stdc++.h>
using namespace std;
int tmp[1048576];
long long sum[30];
int pos(int n) {
  int c = 0;
  while (n) {
    c++;
    n /= 2;
  }
  return c - 1;
}
long long mergesort(int *p, int l, int r) {
  if (l + 1 >= r) return 0;
  int m = (l + r) / 2;
  long long ans = 0;
  ans += mergesort(p, l, m);
  ans += mergesort(p, m, r);
  int i = l, j = m, k = 0;
  long long x = 0;
  while (i != m || j != r) {
    if (i == m || (j != r && p[i] > p[j])) {
      tmp[k++] = p[j++];
      ans += m - i;
      x += m - i;
    } else
      tmp[k++] = p[i++];
  }
  memcpy(p + l, tmp, (r - l) * sizeof(int));
  sum[pos(r - l)] += x;
  return ans;
}
int a[1048576];
int b[1048576];
long long r[30];
long long s1[30], s2[30];
int main() {
  ios::sync_with_stdio(false);
  int k, n, m, q, stat[30] = {0};
  while (cin >> k) {
    memset(stat, 0, sizeof(stat));
    n = 1 << k;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[n - i - 1] = a[i];
    }
    memset(sum, 0, sizeof(sum));
    long long rev = mergesort(a, 0, n);
    memcpy(s1, sum, sizeof(sum));
    memset(sum, 0, sizeof(sum));
    mergesort(b, 0, n);
    memcpy(s2, sum, sizeof(sum));
    cin >> m;
    for (int i = 1; i <= k; i++) r[i] = s1[i] - s2[i];
    for (int i = 0; i < m; i++) {
      cin >> q;
      for (int i = 1; i <= q; i++) {
        if (stat[i]) {
          rev += r[i];
        } else {
          rev -= r[i];
        }
        stat[i] = !stat[i];
      }
      cout << rev << endl;
    }
  }
  return 0;
}
