#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--){
int n, q;
cin >> n;
multiset<int> st1, st2;
vector<ll> a(n), mn1(n), mn2(n), mx1(n), mx2(n), ans(n);
for (int i = 0; i < n; i++){
cin >> a[i];
st1.insert(a[i]);
mn1[i] = distance(st1.begin(), st1.lower_bound(a[i]));
mx1[i] = (i + 1) - distance(st1.begin(), st1.lower_bound(a[i] + 1));
}
for (int i = n - 1; i >= 0; i--){
st2.insert(a[i]);
mn2[i] = distance(st2.begin(), st2.lower_bound(a[i]));
mx2[i] = (n - i) - distance(st2.begin(), st2.lower_bound(a[i] + 1));
}
for (int i = 0; i < n; i++){
ans[i] = mn1[i] * mx2[i] + mn2[i] * mx1[i];
}
cin >> q;
while (q--){
int j;
cin >> j;
j--;
cout << ans[j] << "\n";
}
}
return 0;
}