#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define f(i,x,y) for(int i=x;i<y;i++)
#define f2(i,x,y) for(int i=x;i>=y;i--)
#define pii pair<int,int>
#define Fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int MOD =1000000007;
const int INF = 1e18;
const int N = 2e5+10;
int v[N],tree[4*N];
int merge(int a,int b){
return (a+b);
}
void build(int node,int b,int e)// 1 based
{
if(b==e)
{
tree[node]=v[b];
return;
}
int left=node*2;
int right=node*2+1;
int mid=(b+e)/2;
build(left,b,mid);
build(right,mid+1,e);
tree[node]=merge(tree[left],tree[right]);
}
int query(int node,int b,int e,int i,int j)// 1 based
{
if(i>e || j<b)
return 0;
if(b>=i && e<=j)
return tree[node];
int left=node*2;
int right=node*2+1;
int mid=(b+e)/2;
int p1=query(left,b,mid,i,j);
int p2=query(right,mid+1,e,i,j);
return merge(p1,p2);
}
void update(int node,int b,int e,int i,int value) // 1 based
{
if(i>e || i<b)
return;
if(b>=i && e<=i)
{
tree[node]+=value;
return;
}
int left=node*2;
int right=node*2+1;
int mid=(b+e)/2;
update(left,b,mid,i,value);
update(right,mid+1,e,i,value);
tree[node]=merge(tree[left],tree[right]);
}
vector<int>indices;
int getCompressedIndex(int a) {
return lower_bound(indices.begin(), indices.end(), a) - indices.begin() + 1;
}
//========= COORDINATE COMPRESSION =======
void solve(int tc){
int n; cin >> n;
vector<int>a(n),pre(n);
vector<pii>after(n),before(n);
for(int i=0;i<n;i++){
cin >> a[i];
indices.pb(a[i]);
}
sort(indices.begin(), indices.end());
indices.erase(unique(indices.begin(), indices.end()), indices.end());
build(1,1,n);
//cout << before[i].ff << " " << before[i].ss << endl;
for(int i=0;i<n;i++){
a[i] = getCompressedIndex(a[i]);
before[i].ff = query(1,1,n,1,a[i]-1);
before[i].ss = query(1,1,n,a[i]+1,n);
update(1,1,n,a[i],1);
}
build(1,1,n);
for(int i=n-1;i>=0;i--){
after[i].ff = query(1,1,n,1,a[i]-1);
after[i].ss = query(1,1,n,a[i]+1,n);
update(1,1,n,a[i],1);
}
for(int i=0;i<n;i++){
pre[i] = before[i].ff * after[i].ss;
pre[i] += before[i].ss * after[i].ff;
}
int q; cin >> q;
while(q--){
int x; cin >> x;
cout << pre[x-1] << endl;
}
}
int32_t main(){
Fast
int t=1;
cin >> t;
for(int tc=1;tc<=t;tc++){
solve(tc);
}
return 0;
}