// Solution start from Line 113
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using namespace __gnu_pbds;
// Data Types
typedef int64_t lli;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef string str;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<lli, lli> pll;
typedef pair<lli, int> pli;
typedef pair<int, lli> pil;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pli> vpli;
typedef vector<pil> vpil;
typedef map<int, int> mi;
typedef map<lli, lli> ml;
typedef set<int> si;
typedef set<lli> sl;
typedef multiset<int> msi;
typedef multiset<lli> msl;
// Shortcuts
#define ff first
#define ss second
#define pf push_front
#define pb push_back
#define pof pop_front
#define pob pop_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define sz(a) a.size()
#define bt(a) a.begin(), a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define rall(a) a.rbegin(), a.rend()
#define Unique(a) (a).erase(unique(bt(a)), (a).end())
#define getSum(a) accumulate(bt(a), 0)
#define maxE(a) *max_element(bt(a))
#define minE(a) *min_element(bt(a))
#define fixed(x) fixed << setprecision(x)
#define makeUnique(a) sort(bt(a)); a.erase(unique(bt(a)) - a.begin())
// Inputs
#define cin(a) cin >> a;
#define cind(a, b) cin >> a >> b;
#define cint(a, b, c) cin >> a >> b >> c;
#define cintd(a, b, c, d) cin >> a >> b >> c >> d;
#define input(a, ini, till) for(int i = ini; i < till; i++) cin >> a[i];
// New Line
#define line cout << "\n";
#define endl "\n";
// Print
#define print(a) cout << a << endl
#define dprint(a, b) cout << a << " " << b << endl
#define aPrint(a) cout << a << " "
#define yes(a) cout << (a ? "Yes" : "YES") << endl
#define no(a) cout << (a ? "No" : "NO") << endl
// Loops
#define loop(i, a, n) for(int (i) = (a); i < (n); i++)
#define dloop(i, a, n) for(int (i) = (a); i >= (n); i--)
#define trav(a) for(auto &it: a)
#define rloop(a) for(auto it = a.rbegin(); it != a.rend(); it++)
#define vprint(a) trav(a) cout << it << " "; line;
#define test int t; int T = 1; cin(t); loop(i, 1, t + 1)
// Sort
#define asort(a) sort(bt(a))
#define dsort(a) sort(bt(a), greater<int>())
#define pairSortSec(f) sort(bt(a), f)
// Values
#define MOD 1000000007
#define MOD1 998244353
#define inf 1e18
#define PI acos(-1)
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define fastKajKorBhai ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL)
typedef pair<lli, int> pii_lli;
typedef tree<
pii_lli,
null_type,
less<pii_lli>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
void clear_ordered_multiset(ordered_multiset &s){
ordered_multiset empty;
s = empty;
}
// Function to handle each test case
void take_a_breath(int test_case){
lli n, x;
cin(n);
vl a(n);
ml m;
input(a, 0, n);
ordered_multiset s;
int unique_id = 0;
vpl ml(n, {0, 0}), mr(n, {0, 0});
loop(i, 0, n){
s.ins({a[i], unique_id++});
m[a[i]]++;
lli dis = s.order_of_key({a[i], -1});
lli mini = dis;
if(i == 0 && dis == 0) mini = 0;
lli maxi = i - dis - m[a[i]] + 1;
ml[i] = {mini, maxi};
}
clear_ordered_multiset(s);
m.clear();
lli k = 0;
dloop(i, n-1, 0){
s.ins({a[i], unique_id++});
m[a[i]]++;
lli dis = s.order_of_key({a[i], -1});
lli mini = dis;
if(i == n-1 && dis == 0) mini = 0;
lli maxi = k - dis - m[a[i]] + 1;
k++;
mr[i] = {mini, maxi};
}
cin(x);
while(x--){
lli pos;
cin(pos);
pos -= 1;
lli ans = ml[pos].ff * mr[pos].ss + ml[pos].ss * mr[pos].ff;
print(ans);
}
}
int32_t main(){
fastKajKorBhai;
test{
// cout << "Case " << i << ": ";
take_a_breath(i);
}
return 0;
}