#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define dbg(a,b,c,d) cerr<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
#define kill(a) {cout<<a<<endl;continue;}
#define KILL(a) {cout<<a<<endl;return 0;}
#define debug cerr<<"Error Found"<<endl;
#define mem(a,b) memset(a,b,sizeof(a))
#define lcm(a, b) (a/__gcd(a,b))*b
#define w(t) cin>>t;while(t--)
#define pi 2 * acos(0.0)
#define endl "\n"
int t, cs = 0;
const int mxn = 1e5 + 2,mod = 1e9 + 7;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
signed main()
{
fast;
w(t)
{
int n;
cin >> n;
int ar[n + 1];
ordered_set pre1, pre2, suf1, suf2;
for(int i = 1; i <= n; i++)cin >> ar[i], suf1.insert(-ar[i]), suf2.insert(ar[i]);
int64_t ans[n + 1] = {};
for(int i = 1; i <= n; i++)
{
int val = ar[i];
int64_t left1 = pre1.order_of_key(val);
suf1.erase(--suf1.lower_bound(-val));
int64_t right1 = suf1.order_of_key(-val);
ans[i] = 1LL * left1 * right1;
int64_t left2 = pre2.order_of_key(-val);
suf2.erase(--suf2.lower_bound(val));
int64_t right2 = suf2.order_of_key(val);
pre1.insert(ar[i]), pre2.insert(-ar[i]);
//dbg(left1, right1, left2, right2);
ans[i] += 1LL * left2 * right2;
}
int q;
cin >> q;
while(q--)
{
int x;
cin >> x;
cout << ans[x] << endl;
}
}
}