#include <bits/stdc++.h>
using namespace std;
struct ListNode {
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(NULL) {}
};
ListNode *sortList(ListNode *A, vector<long long> &cnt, int dps) {
  if (A == NULL || A->next == NULL) return A;
  int sz = 0;
  ListNode *l = NULL, *lr = NULL;
  ListNode *r = NULL, *rr = NULL;
  ListNode *L = NULL, *R = NULL, *ret = NULL, *retr = NULL;
  ListNode *tmp = A;
  while (tmp) {
    tmp = tmp->next;
    sz++;
  }
  tmp = A;
  sz /= 2;
  sz--;
  int remL = 0;
  while (sz--) {
    tmp = tmp->next;
    remL++;
  }
  L = A;
  R = tmp->next;
  tmp->next = NULL;
  L = sortList(L, cnt, dps - 1);
  R = sortList(R, cnt, dps - 1);
  while (L || R) {
    if (!L) {
      if (ret == NULL) {
        ret = R;
        retr = ret;
      } else {
        ret->next = R;
        ret = ret->next;
      }
      R = R->next;
    } else if (!R) {
      if (ret == NULL) {
        ret = L;
        retr = ret;
      } else {
        ret->next = L;
        ret = ret->next;
      }
      L = L->next;
    } else {
      if (R->val < L->val) {
        cnt[dps] += remL + 1;
        if (ret == NULL) {
          ret = R;
          retr = ret;
        } else {
          ret->next = R;
          ret = ret->next;
        }
        R = R->next;
      } else {
        remL--;
        if (ret == NULL) {
          ret = L;
          retr = ret;
        } else {
          ret->next = L;
          ret = ret->next;
        }
        L = L->next;
      }
    }
  }
  return retr;
}
vector<long long> cnt(25, 0), com(25, 0);
vector<int> input;
int main() {
  int n;
  scanf("%d", &n);
  input.resize(1 << n);
  for (int i = 0; i < (1 << n); i++) scanf("%d", &input[i]);
  ListNode *root = new ListNode(input[0]);
  ListNode *tmp = root;
  for (int i = 1; i < input.size(); i++) {
    tmp->next = new ListNode(input[i]);
    tmp = tmp->next;
  }
  root = sortList(root, cnt, n);
  reverse(input.begin(), input.end());
  root = new ListNode(input[0]);
  tmp = root;
  for (int i = 1; i < input.size(); i++) {
    tmp->next = new ListNode(input[i]);
    tmp = tmp->next;
  }
  root = sortList(root, com, n);
  int q, h;
  scanf("%d", &q);
  for (int it = 0; it < q; it++) {
    scanf("%d", &h);
    for (int i = 0; i <= h; i++) swap(cnt[i], com[i]);
    long long ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += cnt[i];
    }
    cout << ans << "\n";
  }
}
