Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.

You have an array a of length 2n and m queries on it. The i-th query is described by an integer qi. In order to perform the i-th query you must:

  * split the array into 2n - qi parts, where each part is a subarray consisting of 2qi numbers; the j-th subarray (1 ≤ j ≤ 2n - qi) should contain the elements a[(j - 1)·2qi + 1], a[(j - 1)·2qi + 2], ..., a[(j - 1)·2qi + 2qi]; 
  * reverse each of the subarrays; 
  * join them into a single array in the same order (this array becomes new array a); 
  * output the number of inversions in the new a. 



Given initial array a and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries.

Input

The first line of input contains a single integer n (0 ≤ n ≤ 20). 

The second line of input contains 2n space-separated integers a[1], a[2], ..., a[2n] (1 ≤ a[i] ≤ 109), the initial array.

The third line of input contains a single integer m (1 ≤ m ≤ 106). 

The fourth line of input contains m space-separated integers q1, q2, ..., qm (0 ≤ qi ≤ n), the queries.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

Output m lines. In the i-th line print the answer (the number of inversions) for the i-th query.

Examples

Input

2
2 1 4 3
4
1 2 0 2


Output

0
6
6
0


Input

1
1 2
3
0 1 1


Output

0
1
0

Note

If we reverse an array x[1], x[2], ..., x[n] it becomes new array y[1], y[2], ..., y[n], where y[i] = x[n - i + 1] for each i.

The number of inversions of an array x[1], x[2], ..., x[n] is the number of pairs of indices i, j such that: i < j and x[i] > x[j].
2
1 1 3 1
3
0 1 2

                                                               1
                                                               0
                                                               3

#include <bits/stdc++.h>
  
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
 
using namespace std;
 
#if !ONLINE_JUDGE
#define debug
#endif
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<pair<int,int> > vii;
typedef pair<ll,ll> pll;
typedef vector<vector<int> > vvi;
typedef vector<vector<ii> > vvii;
 
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define ff first
#define ss second
#define trav(a, x) for(auto& a : x) 
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
const ll INF = 1e18;
const ll mod = 1e9 + 7;
 
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}
 
//*X.find_by_order(2) element at index=2
//X.order_of_key(1) how many elements strictly less than 1
 
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> 
 
inline int pow_(ll x, ll y,ll p){
  int r = 1; while(y){if(y & 1) r = r * x % p; y >>= 1; x = x * x % p;} return r;
}
inline int inv_(int x){return pow_(x, mod - 2, mod);}
inline int add(ll a,ll b){a += b; if(a >= mod) a -= mod; return a;}
inline int mul(int a, int b){return a * 1LL * b % mod;}
inline int sub(int a, int b){a -= b; if(a < 0) a += mod; return a;}
 
#ifdef debug
#define dbg(...) { cerr<<"[ "; dump(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.ff<<","<<x.ss<<")";
}
 
template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const unordered_map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
} 
 
template <typename Arg1>
void dump(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}
 
template <typename Arg1, typename... Args>
void dump(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    dump(names.substr(1+(int)name.size()), args...);
}
#else
    #define dbg(args...)
#endif
 
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1<<20;
int a[N+5] = {0};
ll arr[25][2] = {0};


pair<ll,ll> merge(int l,int r, int d){
    int mid = (l+r)/2;

    vector<int> v;

    unordered_map<int,int> m1;
    unordered_map<int,int> m2;

    for(int i=l;i<=mid;i++){
        m1[a[i]]++;
    }

    for(int i=mid+1;i<=r;i++){
        m2[a[i]]++;
    }


    int i = l;
    int j = mid + 1;
    ll val1 = 0;
    ll val2 = 0;
    while(i <= mid && j <= r){
        if(a[i] <= a[j]){
            v.pb(a[i]);
            i++;
        } else {
            v.pb(a[j]);
            val1 += mid - i + 1;
            j++;
        }
    }

    while(i <= mid){
        v.pb(a[i]);
        i++;
    }

    while(j <= r){
        v.pb(a[i]);
        j++;
    }

    for(int i=l;i<=r;i++){
        a[i] = v[i-l];
    }

    for(auto it:m1){
        ll p = m2[it.first];
        val2 += p * it.second;
    }

    ll tot = (mid-l+1) * (mid-l+1);
    dbg(val1,val2,d,tot);
    val2 = tot - val1 - val2;

    return mp(val1,val2);
}

void merge_sort(int l, int r, int d){

    if(l == r){
        return;
    }

    int mid = (l+r)/2;

    merge_sort(l,mid,d+1);
    merge_sort(mid+1,r,d+1);

    pair<ll,ll> val = merge(l,r,d);

    arr[d][0] += val.ff;
    arr[d][1] += val.ss;
}

int main(int argc, char const *argv[])
{
    #ifdef LOCAL_TEST
    freopen("in.txt","r",stdin);
    //freopen("out.txt", "w", stdout);
    #else
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #endif
    int n;
    cin>>n;

    for(int i=1;i<=(1<<n);i++){
        cin>>a[i];
    }

    merge_sort(1,1<<n,0);

    for(int i=0;i<n;i++){
        dbg(i,arr[i][0],arr[i][1]);
    }

    int m;
    cin>>m;

    for(int i=1;i<=m;i++){
        int q;
        cin>>q;
        q = n - q;
        ll ans = 0;
        for(int i = 0; i < n;i++){
            if(i < q){
                ans += arr[i][0];
            } else {
                ans += arr[i][1];
                swap(arr[i][1],arr[i][0]);
            }
        }

        for(int i=0;i<n;i++){
            dbg(i,arr[i][0],arr[i][1]);
        }

        cout << ans << "\n";
    }

    return 0;    
}