#include <bits/stdc++.h>
using namespace std;
#define SC scanf
#define PF printf
#define ull unsigned long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define sort_a(a) sort(a.begin(),a.end());
#define sort_d(a) sort(a.rbegin(),a.rend());
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define rev(s) reverse(s.begin(),s.end())
#define P(ok) cout << (ok ? "YES\n": "NO\n")
#define __Heart__ ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll, ll> PII;
const int mx = 1e5 + 5;
int K , Ans[mx], arr[mx],freq[mx] , F[mx] ;
int cnt=0;
vector<int>edge[mx];
int out[mx],in[mx];
vector<int>convert_A;
int bit[mx];
int n,q;
int qquery(int id){
int sum=0;
while(id>0){
sum+=bit[id];
id-=id&(-id);
}
return sum;
}
void update(int id,int val){
while(id<=n){
bit[id]+=val;
id+=id&(-id);
}
}
struct Query
{
int index, l, r ;
bool operator < (const Query &other)const
{
int block_own = l/ K ;
int block_other = other.l / K ;
return(block_other == block_own) ? r < other.r : block_own < block_other ;
}
} query[mx];
void add(int index)
{
freq[arr[convert_A[index]-1]]++ ;
if(freq[arr[convert_A[index]-1]]==1)update(arr[convert_A[index]-1],1);
}
void remove (int index)
{
freq[arr[convert_A[index]-1]]-- ;
if(freq[arr[convert_A[index]-1]]==0)update(arr[convert_A[index]-1],-1);
}
void dfs(int x,int p){
in[x]=out[x]=cnt++;
convert_A.push_back(x);
for(auto u:edge[x]){
if(u!=p){
dfs(u,x);
}
}
out[x]=cnt;
}
void solve()
{
cin >> n ;
for(int i = 0 ; i <= n ; i++) freq[i] = 0 ;
for(int i = 0 ; i < n ; i++) cin >> arr[i] ;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
edge[x].push_back(y);
edge[y].push_back(x);
}
cnt=0;
dfs(1,-1);
K = sqrt(n) ;
cin >> q ;
for(int i = 0 ; i < q ; i++){
int x;
cin >> x;
query[i].l=in[x], query[i].r=out[x]-1 , query[i].index = i;
}
sort(query , query + q) ;
int l = 0, r = -1 ;
for(int i = 0 ; i < q ; i++)
{
while(r < query[i].r) add(++r ) ;
while(l < query[i].l) remove(l++) ;
while(r > query[i].r) remove(r-- );
while(l > query[i].l) add(--l) ;
// int cnt = 0 ;
int dis = query[i].r - query[i].l + 1 ;
Ans[query[i].index] = dis-qquery(dis);
}
convert_A.clear();
for(int i=1;i<=n;i++)edge[i].clear(),bit[i]=0;
for(int i = 0 ; i < q ; i++) cout<< Ans[i] << "\n" ;
}
int main()
{
__Heart__
// READ("input7.txt") ;
// WRITE("output7.txt") ;
int t ; cin >> t ; while(t--) solve() ;
}