#include <bits/stdc++.h>
using namespace std;
vector<int> A, B;
long long c1[25], c2[25];
int n;
long long merge1(vector<int>& a, int lev) {
  int n = a.size();
  if (lev == 0) return 0;
  long long cnt = 0;
  vector<int> b(a.begin(), a.begin() + n / 2);
  vector<int> c(a.begin() + n / 2, a.end());
  cnt += merge1(b, lev - 1);
  cnt += merge1(c, lev - 1);
  int ai = 0, bi = 0, ci = 0;
  long long sum = 0;
  while (ai < n) {
    if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci]))
      a[ai++] = b[bi++];
    else {
      cnt += n / 2 - bi;
      sum += n / 2 - bi;
      a[ai++] = c[ci++];
    }
  }
  c1[lev] += sum;
  return cnt;
}
long long merge2(vector<int>& a, int lev) {
  int n = a.size();
  if (lev == 0) return 0;
  long long cnt = 0;
  vector<int> b(a.begin(), a.begin() + n / 2);
  vector<int> c(a.begin() + n / 2, a.end());
  cnt += merge2(b, lev - 1);
  cnt += merge2(c, lev - 1);
  int ai = 0, bi = 0, ci = 0;
  long long sum = 0;
  while (ai < n) {
    if (bi < b.size() && (ci == c.size() || b[bi] >= c[ci]))
      a[ai++] = b[bi++];
    else {
      cnt += n / 2 - bi;
      sum += n / 2 - bi;
      a[ai++] = c[ci++];
    }
  }
  c2[lev] += sum;
  return cnt;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (1 << n); i++) {
    int temp;
    scanf("%d", &temp);
    A.push_back(temp);
  }
  B = A;
  merge1(A, n);
  merge2(B, n);
  int q;
  scanf("%d", &q);
  while (q--) {
    int pos;
    scanf("%d", &pos);
    for (int i = 0; i <= pos; i++) swap(c1[i], c2[i]);
    long long ans = 0;
    for (int i = 0; i <= n; i++) ans += c1[i];
    cout << ans << endl;
  }
  return 0;
}
