/**
* written by Binoy Barman .
**/
#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Too_Many_Jobs int tts, tc = 1; cin >> tts; hell: while(tts--)
#define Dark_Lord_Binoy ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifdef LOCAL
#include "debug/whereisit.hpp"
#else
#define dbg(...) 42
#endif
#define int long long
int32_t main() {
Dark_Lord_Binoy
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Too_Many_Jobs {
int n;
cin >> n;
vector<int> a(n), l(n + 1, 0), h(n + 1, 0), L(n + 1, 0), H(n + 1, 0);
multiset<int> s;
for (int i = 0; i < n; i++) {
cin >> a[i];
auto k = s.insert(a[i]);
auto it = distance(s.begin(), k);
l[i + 1] = it;
h[i + 1] = (i - it);
}
s.clear();
for (int i = n - 1; i >= 0; i--) {
auto k = s.insert(a[i]);
auto it = distance(s.begin(), k);
L[i + 1] = it;
H[i + 1] = (s.size() - 1 - it);
}
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
int ans = l[x] * H[x] + h[x] * L[x];
cout << ans << nl;
}
}
return 0;
}