#include<bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define error1(x) cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v) for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T, typename R> using ordered_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// *s.find_by_order(k): K-th element in a set (counting from zero).
// s.order_of_key(k): Number of items strictly smaller than k. (same as lower_bound of k)
// less_equal<T> => for ordered_multiset or, ordered_multimap
void solve() {
ll n, ans = 0;
cin >> n;
vector<ll> v(n), mn1(n), mx1(n);
ordered_set<ll> st;
for (int i = 0; i < n; i++) {
ll x; cin >> x;
v[i] = x;
mn1[i] = st.order_of_key(x);
mx1[i] = ll(st.size()) - st.order_of_key(x + 1);
st.insert(x);
}
// coutall(mn1);
// coutall(mx1);
vector<ll> mn2(n), mx2(n);
st.clear();
for (int i = n - 1; i >= 0; i--) {
ll x = v[i];
mn2[i] = st.order_of_key(x);
mx2[i] = ll(st.size()) - st.order_of_key(x + 1);
st.insert(x);
}
// coutall(mn2);
// coutall(mx2);
ll q;
cin >> q;
while(q--) {
ll i; cin >> i;
--i;
cout << (mn1[i] * mx2[i]) + (mn2[i] * mx1[i]) << endl;
}
return;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case " << t << ": ";
solve();
}
return 0;
}