#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
int n;
vector<int> bit;
Fenwick(int n=0){ init(n); }
void init(int n_){ n = n_; bit.assign(n+1, 0); }
void add(int i, int delta){
for(; i <= n; i += i & -i) bit[i] += delta;
}
int sumPrefix(int i){
int s = 0;
for(; i > 0; i -= i & -i) s += bit[i];
return s;
}
int rangeSum(int l, int r){
if(l>r) return 0;
return sumPrefix(r) - sumPrefix(l-1);
}
};
int N;
vector<vector<int>> g;
vector<int> A;
vector<int> sz, heavy, tin, tout, euler;
int timerdfs;
vector<int> cnt; // frequency of values in current data structure
Fenwick bit;
vector<int> ans;
void dfs_sz(int u, int p){
sz[u] = 1;
tin[u] = ++timerdfs;
euler[timerdfs] = u;
heavy[u] = -1;
int maxsz = 0;
for(int v : g[u]) if(v != p){
dfs_sz(v, u);
sz[u] += sz[v];
if(sz[v] > maxsz){ maxsz = sz[v]; heavy[u] = v; }
}
tout[u] = timerdfs;
}
inline void add_node(int node){
int val = A[node];
if(cnt[val] == 0) bit.add(val, 1);
cnt[val]++;
}
inline void remove_node(int node){
int val = A[node];
cnt[val]--;
if(cnt[val] == 0) bit.add(val, -1);
}
void dfs(int u, int p, bool keep){
// process all light children
for(int v : g[u]) if(v != p && v != heavy[u])
dfs(v, u, false);
// process heavy child (kept)
if(heavy[u] != -1)
dfs(heavy[u], u, true);
// add light children contributions into current data
for(int v : g[u]) if(v != p && v != heavy[u]){
for(int t = tin[v]; t <= tout[v]; ++t){
add_node(euler[t]);
}
}
// add current node
add_node(u);
// compute answer for u
int S = sz[u];
int distinct_le_S = bit.sumPrefix(min(S, N)); // values are in [1..N]
ans[u] = S - distinct_le_S;
// if not keep, remove all nodes in this subtree
if(!keep){
for(int t = tin[u]; t <= tout[u]; ++t){
remove_node(euler[t]);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
if(!(cin >> T)) return 0;
while(T--){
cin >> N;
A.assign(N+1, 0);
for(int i=1;i<=N;++i) cin >> A[i];
g.assign(N+1, {});
for(int i=0;i<N-1;++i){
int u,v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
sz.assign(N+1, 0);
heavy.assign(N+1, -1);
tin.assign(N+1, 0);
tout.assign(N+1, 0);
euler.assign(N+1, 0);
timerdfs = 0;
dfs_sz(1, 0);
cnt.assign(N+1, 0);
bit.init(N);
ans.assign(N+1, 0);
dfs(1, 0, true);
int Q; cin >> Q;
vector<int> queries(Q);
for(int i=0;i<Q;++i) cin >> queries[i];
for(int i=0;i<Q;++i){
cout << ans[queries[i]] << (i+1==Q?'\n':'\n');
}
}
return 0;
}