#include <bits/stdc++.h>
using namespace std;
long long mpow(long long a, long long n) {
  long long ret = 1;
  long long b = a;
  while (n) {
    if (n & 1) ret = (ret * b) % 1000000007;
    b = (b * b) % 1000000007;
    n >>= 1;
  }
  return (long long)ret;
}
using namespace std;
int nArray[1 << 20];
int nBackup[1 << 20];
int nGoal[1 << 20];
long long nLevel[21][2];
long long g_nSum;
void MergeSort(int source[], int nLeft, int nRight, int goal[], int nLen,
               int part) {
  if (nLeft + 1 >= nRight) {
    return;
  }
  int p = nLeft;
  int mid = nLeft + (nRight - nLeft) / 2;
  int q = mid;
  int i = nLeft;
  MergeSort(source, nLeft, mid, goal, nLen - 1, part);
  MergeSort(source, mid, nRight, goal, nLen - 1, part);
  while (p < mid || q < nRight) {
    if ((q >= nRight) || ((p < mid) && (source[p] <= source[q]))) {
      goal[i++] = source[p++];
    } else {
      goal[i++] = source[q++];
      nLevel[nLen][part] += mid - p;
      if (!part) {
        g_nSum += mid - p;
      }
    }
  }
  for (i = nLeft; i < nRight; ++i) {
    source[i] = goal[i];
  }
}
int main() {
  int N;
  int M;
  while (~scanf("%d", &N)) {
    int nTotal = 1 << N;
    for (int i = 0; i < nTotal; ++i) {
      scanf("%d", &nArray[i]);
      nBackup[nTotal - 1 - i] = nArray[i];
    }
    g_nSum = 0;
    memset(nLevel, 0, sizeof(nLevel));
    MergeSort(nArray, 0, nTotal, nGoal, N, 0);
    MergeSort(nBackup, 0, nTotal, nGoal, N, 1);
    scanf("%d", &M);
    while (M--) {
      int x;
      scanf("%d", &x);
      for (int i = x; i >= 1; --i) {
        g_nSum += nLevel[i][1] - nLevel[i][0];
        swap(nLevel[i][1], nLevel[i][0]);
      }
      cout << g_nSum << endl;
    }
  }
  return 0;
}
