#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, q;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<int> lg(n + 1, 0), ls(n + 1, 0);
vector<int> rg(n + 1, 0), rs(n + 1, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (a[j] > a[i]) lg[i]++;
if (a[j] < a[i]) ls[i]++;
}
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (a[j] > a[i]) rg[i]++;
if (a[j] < a[i]) rs[i]++;
}
}
cin >> q;
while (q--) {
int x;
cin >> x;
int ans = lg[x] * rs[x] + ls[x] * rg[x];
cout << ans << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}