#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 1e5 + 5;
ordered_set t[4 * N];
vector<int> adj[N];
int n, q, arr[N], tin[N], tout[N], id[N], timer;
void euler_tour(int now, int par) {
tin[now] = ++timer;
id[timer] = now;
for (auto &it: adj[now]) {
if (it == par) continue;
euler_tour(it, now);
}
tout[now] = timer;
}
ordered_set merge(const ordered_set &a, const ordered_set &b) {
ordered_set ret = a;
for (auto &it: b) ret.insert(it);
return ret;
}
ordered_set build(int n, int s, int e) {
if (s == e) {
t[n].insert(arr[id[s]]);
return t[n];
}
int m = (s + e) / 2;
return t[n] = merge(build(n * 2, s, m), build(n * 2 + 1, m + 1, e));
}
ordered_set query(int n, int s, int e, int l, int r) {
if (s > r || e < l) return {};
if (s >= l && e <= r) return t[n];
int m = (s + e) / 2;
return merge(query(n * 2, s, m, l, r), query(n * 2 + 1, m + 1, e, l, r));
}
void shelby() {
cin >> n;
timer = 0;
for (int i = 1; i <= 4 * n; ++i) t[i].clear();
for (int i = 1; i <= n; ++i) cin >> arr[i], adj[i].clear();
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
euler_tour(1, -1);
build(1, 1, n);
debug(t);
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
ordered_set o = query(1, 1, n, tin[x], tout[x]);
int sz = tout[x] - tin[x] + 1, ans = sz;
ans -= (o.order_of_key(sz + 1));
cout << ans << '\n';
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
cin >> t;
for (int _ = 1; _ <= t; ++_) {
// cout << "Case " << _ << ": ";
shelby();
}
}