#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<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void sufi() {
ordered_set 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], 0ll});
pre1[i] = les;
int gr = s.size() - s.order_of_key({ara[i] + 1, 0ll});
pre2[i] = gr;
s.insert({ara[i], 0ll});
}
ordered_set 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], 0ll});
pre3[i] = les;
int gr = s2.size() - s2.order_of_key({ara[i] + 1, 0ll});
pre4[i] = gr;
s2.insert({ara[i], 0ll});
}
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;
}