#include <bits/stdc++.h>
using namespace std;
long long sum[1 << 21] = {0}, same[1 << 21] = {0};
int arr[1 << 21], temp[1 << 21], num;
long long ms(int l, int r) {
  int mid, i, j, k;
  long long cnt = 0, rep = 0, c = 1;
  if (r > l) {
    mid = (r + l) / 2;
    cnt = ms(l, mid);
    cnt += ms(mid + 1, r);
    i = l;
    j = mid + 1;
    k = l;
    while (i <= mid && j <= r) {
      if (arr[i] <= arr[j]) {
        temp[k++] = arr[i++];
      } else {
        temp[k++] = arr[j++];
        cnt += mid + 1 - i;
      }
    }
    while (i <= mid) temp[k++] = arr[i++];
    while (j <= r) temp[k++] = arr[j++];
    for (i = l; i <= r; ++i) {
      arr[i] = temp[i];
      if (i > l && arr[i] == arr[i - 1]) {
        c++;
      } else if (c > 1) {
        rep += (c * (c - 1)) / 2;
        c = 1;
      }
    }
    rep += (c * (c - 1)) / 2;
  }
  sum[num / (r - l + 1)] += cnt;
  same[num / (r - l + 1)] += rep;
  return cnt;
}
int main() {
  ios::sync_with_stdio(false);
  int t = 0, i = 0, j = 0, n = 0, m = 0, k = 0, ans = 0;
  long long a, b, change;
  cin >> n;
  num = 1 << n;
  string str;
  for (i = 1; i <= num; ++i) {
    cin >> arr[i];
  }
  ms(1, num);
  cin >> m;
  for (i = 0; i < m; ++i) {
    cin >> k;
    t = n - k;
    for (j = n; j > t; --j) {
      a = 1 << (n - j);
      b = 1 << j;
      sum[b] = b * ((a * (a - 1)) / 2) - sum[b] - same[b];
    }
    a = 1 << (n - t);
    b = 1 << t;
    change = b * ((a * (a - 1)) / 2) - 2 * sum[b] - same[b];
    for (j = t; j >= 0; --j) {
      b = 1 << j;
      sum[b] += change;
    }
    cout << sum[1] << endl;
  }
  return 0;
}
