#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define dbg(a,b,c,d) cerr<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
#define kill(a) {cout<<a<<endl;continue;}
#define KILL(a) {cout<<a<<endl;return 0;}
#define debug cerr<<"Error Found"<<endl;
#define mem(a,b) memset(a,b,sizeof(a))
#define lcm(a, b) (a/__gcd(a,b))*b
#define w(t) cin>>t;while(t--)
#define pi 2 * acos(0.0)
#define endl "\n"
int t, cs = 0;
const int mxn = 1e5 + 2,mod = 1e9 + 7;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
signed main()
{
//fast;
w(t)
{
int n;
cin >> n;
int ar[n + 1];
ordered_set pre1, pre2, suf1, suf2;
for(int i = 1; i <= n; i++)cin >> ar[i], suf1.insert(-ar[i]), suf2.insert(ar[i]);
int ans[n + 1] = {};
for(int i = 1; i <= n; i++)
{
int val = ar[i];
int left1 = pre1.order_of_key(val);
suf1.erase(--suf1.lower_bound(-val));
int right1 = suf1.order_of_key(-val);
ans[i] = min(left1, right1);
int left2 = pre2.order_of_key(-val);
suf2.erase(--suf2.lower_bound(val));
int right2 = suf2.order_of_key(val);
pre1.insert(ar[i]), pre2.insert(-ar[i]);
dbg(left1, right1, left2, right2);
ans[i] += min(left2, right2);
}
int q;
cin >> q;
vector<int>v;
while(q--)
{
int x;
cin >> x;
v.push_back(ans[x]);
}
for(auto i:v)cout << i << endl;
}
}