#include <bits/stdc++.h>
using namespace std;
int arr[1 << 20], temp[1 << 20];
pair<long long, long long> p[21];
int c;
long long tt;
void mergesort(int a[], int first, int last, int ind) {
  if (first >= last) return;
  int middle = (first + last) / 2;
  mergesort(a, first, middle, ind - 1);
  mergesort(a, middle + 1, last, ind - 1);
  c = 0;
  int i, j, k;
  tt = 1 << (ind - 1);
  tt = tt * tt;
  p[ind].first = p[ind].first + tt;
  for (i = first, j = first, k = middle + 1; i <= last; i++) {
    if (j == middle + 1) {
      temp[i] = a[k++];
      p[ind].first -= c;
      if (k <= last && a[k] != a[k - 1]) {
        c = 0;
      }
    } else if (k == last + 1) {
      temp[i] = a[j++];
    } else if (a[j] > a[k]) {
      temp[i] = a[k++];
      p[ind].first = p[ind].first - c;
      p[ind].second = p[ind].second + (middle - j + 1);
      if (k <= last && a[k] != a[k - 1]) {
        c = 0;
      }
    } else {
      if (a[j] == a[k]) c++;
      temp[i] = a[j++];
    }
  }
  for (i = first; i <= last; i++) {
    a[i] = temp[i];
  }
}
long long inversions(int current, int n) {
  long long ans = 0;
  int i;
  for (i = 0; i <= n; i++) {
    if (i <= current) p[i].second = p[i].first - p[i].second;
    ans = ans + p[i].second;
  }
  return ans;
}
int main() {
  int n, N, i, q, m;
  long long ans;
  scanf("%d", &n);
  N = 1 << n;
  for (i = 0; i < N; i++) {
    scanf("%d", &arr[i]);
  }
  mergesort(arr, 0, N - 1, n);
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d", &q);
    ans = inversions(q, n);
    printf("%I64d\n", ans);
  }
}
