#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int A[1 << 21], B[1 << 21];
long long level_index1[21];
long long level_index2[21];
long long mergesort(int begin, int end, int level) {
  if (begin == end) return 0;
  long long inv_index = 0;
  int middle = (begin + end) / 2;
  inv_index += mergesort(begin, middle, level + 1);
  inv_index += mergesort(middle + 1, end, level + 1);
  int left = begin;
  int right = middle + 1;
  long long ct = 0;
  long long ct2 = 0;
  for (int k = begin; k <= end; ++k) {
    if (right > end || (left <= middle && (A[left] <= A[right]))) {
      B[k] = A[left++];
    } else {
      B[k] = A[right++];
      ct += (middle - left + 1);
    }
  }
  left = begin, right = middle + 1;
  for (int k = begin; k <= end; ++k) {
    if (right > end || (left <= middle && (A[left] < A[right]))) {
      left++;
      ct2 += (end - right + 1);
    } else {
      right++;
    }
  }
  for (int k = begin; k <= end; ++k) A[k] = B[k];
  level_index1[level] += ct;
  level_index2[level] += ct2;
  return inv_index + ct;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int fim = 1 << n;
  for (int i = 0; i < fim; ++i) cin >> A[i];
  for (int lvl = 0; lvl <= n; ++lvl) {
    level_index1[lvl] = level_index2[lvl] = 0;
  }
  mergesort(0, fim - 1, 0);
  int m;
  cin >> m;
  while (m--) {
    int q;
    cin >> q;
    long long ans = 0;
    q = n - q;
    for (int lvl = 0; lvl < q; ++lvl) {
      ans += level_index1[lvl];
    }
    for (int lvl = q; lvl < n; ++lvl) {
      swap(level_index1[lvl], level_index2[lvl]);
      ans += level_index1[lvl];
    }
    cout << ans << '\n';
  }
  return 0;
}
