#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';
tree<int, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update> o;
tree<int, null_type, greater_equal<>, rb_tree_tag, tree_order_statistics_node_update> o2;
void shelby() {
int n;
cin >> n;
vector<int> v(n + 1);
for (int i = 1; i <= n; ++i) cin >> v[i];
vector cnt(n + 1, vector(2, array<int, 2>()));
for (int i = 1; i <= n; ++i) {
cnt[i][0][0] = o.order_of_key(v[i]);
cnt[i][1][0] = o2.order_of_key(v[i]);
o.insert(v[i]), o2.insert(v[i]);
}
o.clear(), o2.clear();
for (int i = n; i; --i) {
cnt[i][1][1] = o.order_of_key(v[i]);
cnt[i][0][1] = o2.order_of_key(v[i]);
o.insert(v[i]), o2.insert(v[i]);
}
vector<int> ans(n + 1);
for (int i = 1; i <= n; ++i) ans[i] = cnt[i][0][0] * cnt[i][0][1] + cnt[i][1][0] * cnt[i][1][1];
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
cout << ans[x] << '\n';
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
cin >> t;
for (int _ = 1; _ <= t; ++_) {
// cout << "Case " << _ << ": ";
shelby();
}
}