#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef pair<long long,long long> PLL;
typedef long long LL;
#define all(v) v.begin(),v.end()
#define faster {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
const LL mod = 1e9 + 7;
const int N = 2e5 + 10;
const LL inf = 1e9;
void solve(int test){
int n, q; cin>>n;
vector<int> a(n);
ordered_multiset l, r;
for(auto &i: a){
cin>>i;
r.insert(i);
}
cin>>q;
vector<int> qrs[n];
for(int i = 0; i < q; i++){
int u; cin>>u; qrs[--u].push_back(i);
}
vector<LL> ans(q);
for(int i = 0; i < n; i++){
int x = a[i];
int pos = r.order_of_key(x);
r.erase(r.find_by_order(pos));
int left1 = l.order_of_key(x), left2 = l.size() - l.order_of_key(x + 1);
int right1 = r.size() - r.order_of_key(x + 1), right2 = r.order_of_key(x);
LL cur = (1LL * left1 * right1) ;
cur += (1LL * left2 * right2) ;
for(auto id: qrs[i]) ans[id] = cur;
l.insert(x);
}
for(auto u: ans) cout<<u<<"\n";
}
signed main(){
faster
int t = 1;
cin>>t;
for(int i = 1; i <= t; i++){
solve(i);
}
return 0;
}