#include <bits/stdc++.h>
using namespace std;
const int MAX = (1 << 20);
int N;
int A[MAX + 10];
int A2[MAX + 10];
vector<int> temp;
inline long long merge(int l, int r) {
  temp.clear();
  long long invs = 0;
  int m = (l + r) / 2;
  int pl = l, pr = m;
  while (pl < m || pr < r) {
    if (pl == m) {
      temp.push_back(A[pr]);
      pr++;
    } else if (pr == r) {
      temp.push_back(A[pl]);
      pl++;
    } else {
      if (A[pl] <= A[pr]) {
        temp.push_back(A[pl]);
        pl++;
      } else {
        invs += (m - pl);
        temp.push_back(A[pr]);
        pr++;
      }
    }
  }
  for (int i = 0; i < temp.size(); i++) A[l + i] = temp[i];
  return invs;
}
long long invs[24];
long long total[24];
long long equals[24];
map<int, int> id;
long long freq[MAX + 10];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < (1 << N); i++) {
    scanf("%d", &(A[i]));
    id[A[i]]++;
  }
  int nextid = 0;
  for (map<int, int>::iterator it = id.begin(); it != id.end(); it++) {
    it->second = nextid;
    nextid++;
  }
  for (int i = 0; i < (1 << N); i++) {
    A[i] = id[A[i]];
  }
  memset(freq, 0, sizeof(freq));
  memset(equals, 0, sizeof(equals));
  memset(total, 0, sizeof(total));
  for (int s = 0; s <= N; s++) {
    for (int i = 0; i < (1 << N); i += (1 << s)) {
      for (int j = i; j < i + (1 << s); j++) {
        freq[A[j]]++;
      }
      for (int j = i; j < i + (1 << s); j++) {
        if (freq[A[j]]) {
          equals[s] += (freq[A[j]] * (freq[A[j]] - 1) / 2);
          freq[A[j]] = 0;
        }
      }
      total[s] += ((1LL << s) * ((1LL << s) - 1)) / 2;
    }
  }
  invs[0] = 0;
  for (int s = 1; s <= N; s++) {
    invs[s] = invs[s - 1];
    for (int i = 0; i < (1 << N); i += (1 << s)) {
      invs[s] += merge(i, i + (1 << s));
    }
  }
  int Q;
  scanf("%d", &Q);
  for (int q = 0; q < Q; q++) {
    int s;
    scanf("%d", &s);
    long long prev = invs[s];
    for (int i = 0; i <= s; i++) {
      invs[i] = total[i] - invs[i] - equals[i];
    }
    for (int i = s + 1; i <= N; i++) {
      invs[i] = invs[i] - prev + invs[s];
    }
    printf("%I64d\n", invs[N]);
  }
  return 0;
}
