// Solution start from Line 113
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
// 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)
void file(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
}
struct BIT{
vi bit;
int n;
BIT(int n){
this->n = n;
bit.assign(n+1, 0);
}
void update(int idx, int val){
for(; idx <= n; idx += idx & -idx)
bit[idx] += val;
}
int query(int idx){
int sum = 0;
for(; idx > 0; idx -= idx & -idx)
sum += bit[idx];
return sum;
}
int rangeQuery(int l, int r){
return query(r) - query(l-1);
}
void clear(){
fill(bit.begin(), bit.end(), 0);
}
};
// Solution Starts Here
void take_a_breath(int test_case){
lli n, x, y, o, p, q, r, l, temp, res, sum = 0, mx = MIN, mn = MAX;
cin(n);
vl a(n);
ml m;
loop(i, 0, n){
cin(a[i]);
}
multiset<lli> s;
vpl ml(n), mr(n);
loop(i, 0, n){
s.ins(a[i]);
m[a[i]]++;
auto it = s.lower_bound(a[i]);
lli dis = distance(s.begin(), it);
lli mini = dis;
if(i==0 and dis==0)mini = 0;
lli maxi = i-dis-m[a[i]]+1;
ml[i] = {mini, maxi};
}
s.clear();
m.clear();
lli k = 0;
dloop(i, n-1, 0){
s.ins(a[i]);
m[a[i]]++;
auto it = s.lower_bound(a[i]);
lli dis = distance(s.begin(), it);
lli mini = dis;
if(i==n-1 and dis==0)mini = 0;
lli maxi = k-dis-m[a[i]]+1;
k++;
mr[i] = {mini, maxi};
}
cin(q);
while(q--){
cin(x);
lli ans = ml[x-1].ff*mr[x-1].ss + ml[x-1].ss*mr[x-1].ff;
print(ans);
}
}
int32_t main(){
fastKajKorBhai;
// file();
test{
// cout << "Case " << i << ": ";
take_a_breath(i);}
// take_a_breath(1);
return 0;
}