#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long nums[1 << (21 - 1)];
long long nums2[1 << (21 - 1)];
long long invrange[21];
long long invrange2[21];
long long inversion(long long l, long long r, long long x) {
  if (l == r) return 0;
  long long mid = (l + r) / 2;
  long long i1 = inversion(l, mid, x - 1);
  long long i2 = inversion(mid + 1, r, x - 1);
  long long nums1[r - l + 1];
  long long ct1 = l;
  long long ct2 = mid + 1;
  long long ct = 0;
  long long sum = 0;
  while (ct <= r - l) {
    if ((ct1 <= mid) && (ct2 > r || nums[ct1] <= nums[ct2])) {
      nums1[ct] = nums[ct1];
      ct1++;
      ++ct;
    } else {
      nums1[ct] = nums[ct2];
      ct2++;
      ++ct;
      sum += (mid - ct1 + 1);
    }
  }
  for (long long i = l; i <= r; ++i) nums[i] = nums1[i - l];
  invrange[x] += i1 + i2;
  invrange[x] += sum;
  return sum + i1 + i2;
}
long long inversion2(long long l, long long r, long long x) {
  if (l == r) return 0;
  long long mid = (l + r) / 2;
  long long i1 = inversion2(l, mid, x - 1);
  long long i2 = inversion2(mid + 1, r, x - 1);
  long long nums1[r - l + 1];
  long long ct1 = l;
  long long ct2 = mid + 1;
  long long ct = 0;
  long long sum = 0;
  while (ct <= r - l) {
    if ((ct2 <= r) && (ct1 > mid || nums2[ct2] <= nums2[ct1])) {
      nums1[ct] = nums2[ct2];
      ct2++;
      ++ct;
    } else {
      nums1[ct] = nums2[ct1];
      ct1++;
      ++ct;
      sum += (r - ct2 + 1);
    }
  }
  for (long long i = l; i <= r; ++i) nums2[i] = nums1[i - l];
  invrange2[x] += i1 + i2;
  invrange2[x] += sum;
  return sum + i1 + i2;
}
void update(long long x) {
  long long diff;
  for (long long i = 0; i <= x; ++i) {
    long long sum = invrange[i] + invrange2[i];
    diff = invrange2[i] - invrange[i];
    invrange[i] = sum - invrange[i];
    invrange2[i] = sum - invrange2[i];
  }
  for (long long i = x + 1; i <= n; ++i) {
    invrange[i] += diff;
    invrange2[i] -= diff;
  }
}
int main() {
  cin >> n;
  for (long long i = 0; i < 1 << n; ++i) {
    scanf("%d", &nums[i]);
    nums2[i] = nums[i];
  }
  for (long long i = 0; i < 21; ++i) {
    invrange[i] = 0;
    invrange2[i] = 0;
  }
  inversion(0, (1 << n) - 1, n);
  inversion2(0, (1 << n) - 1, n);
  cin >> m;
  for (long long i = 0; i < m; ++i) {
    int t;
    scanf("%d", &t);
    update(t);
    printf("%I64d\n", invrange[n]);
  }
  return 0;
}
