#include<bits/stdc++.h>
#define ll long long
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// Template of Multi Ordered set
struct Treap{ /// hash = 96814
int len;
const int ADD = 1000010;
const int MAXVAL = 1000000010;
unordered_map <long long, int> mp; /// Change to int if only int in treap
tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> T;
Treap(){
len = 0;
T.clear(), mp.clear();
}
inline void clear(){
len = 0;
T.clear(), mp.clear();
}
inline void insert(long long x){
len++, x += MAXVAL;
int c = mp[x]++;
T.insert((x * ADD) + c);
}
inline void erase(long long x){
x += MAXVAL;
int c = mp[x];
if (c){
c--, mp[x]--, len--;
T.erase((x * ADD) + c);
}
}
/// 1-based index, returns the K'th element in the treap, -1 if none exists
inline long long kth(int k){
if (k < 1 || k > len) return -1;
auto it = T.find_by_order(--k);
return ((*it) / ADD) - MAXVAL;
}
/// Count of value < x in treap
inline int count(long long x){
x += MAXVAL;
int c = mp[--x];
return (T.order_of_key((x * ADD) + c));
}
/// Number of elements in treap
inline int size(){
return len;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--){
int n, q;
cin >> n;
Treap tt;
map<int, int> mp1, mp2;
vector<ll> a(n), mn1(n), mn2(n), mx1(n), mx2(n), ans(n);
for (int i = 0; i < n; i++){
cin >> a[i];
tt.insert(a[i]);
mp1[a[i]]++;
int x = tt.count(a[i]);
mn1[i] = x;
mx1[i] = (i + 1) - x - mp1[a[i]];
}
tt.clear();
int cnt = 0;
for (int i = n - 1; i >= 0; i--){
tt.insert(a[i]);
mp2[a[i]]++;
cnt++;
int x = tt.count(a[i]);
mn2[i] = x;
mx2[i] = cnt - x - mp2[a[i]];
}
for (int i = 0; i < n; i++){
ans[i] = mn1[i] * mx2[i] + mn2[i] * mx1[i];
}
cin >> q;
while (q--){
int j;
cin >> j;
j--;
cout << ans[j] << "\n";
}
}
return 0;
}