#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
using namespace std;
using namespace __gnu_pbds;
const ll md=1e9+7;
const ll smpl=4*1e5;
const ll p=31;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>pbds;
void sufi() {
pbds s;
int n;
cin >> n;
ll ara[n+1];
ll pre1[n+1] = {0};
ll pre2[n+1] = {0};
for (int i = 1; i <= n; i++) {
cin >> ara[i];
int les = s.order_of_key(ara[i]);
pre1[i] = les;
int gr = s.size() - s.order_of_key(ara[i] + 1);
pre2[i] = gr;
s.insert(ara[i]);
}
pbds s2;
ll pre3[n+1] = {0};
ll pre4[n+1] = {0};
for (int i = n; i >= 1; i--) {
int les = s2.order_of_key(ara[i]);
pre3[i] = les;
int gr = s2.size() - s2.order_of_key(ara[i] + 1);
pre4[i] = gr;
s2.insert(ara[i]);
}
int q;
cin >> q;
while (q--) {
ll i;
cin >> i;
ll ans = pre1[i] * pre4[i];
ans += pre2[i] * pre3[i];
cout << ans << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
while(t--)
sufi();
return 0;
}