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