/**
* author: Binoy Barman
* created: 2025-01-02 22:34:25
**/
#include<bits/stdc++.h>
#ifdef LOCAL
#include "debug/trace.hpp"
#else
#define dbg(...) 42
#define print(...) 42
#endif
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = 1e9;
// #define int long long
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Testcase_Handler int tts, tc = 1; cin >> tts; hell: while(tts--)
#define uniq(v) sort(all(v)), v.resize(distance(v.begin(), unique(v.begin(), v.end())))
template<class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {bool first = true;for(auto &x : a) {if(!first) out << ' ';first = false;out << x;}return out;};
namespace Dark_Lord_Binoy {
void preprocess() {}
void setup() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
preprocess();
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
}
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k . O(logn)
// find_by_order(k) : K-th element in a set (counting from zero). O(logn)
template <typename T, typename R>
using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 9;
int a[N], ans[N];
struct Dfs {
vector<vector<int>> g;
vector<int> sz;
vector<bool> vis;
vector<o_set<int>> c;
Dfs(int n) : sz(n + 1), vis(n + 1), g(n + 1), c(n + 1) {}
void add_edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
}
void dfs(int v, int p = -1) {
vis[v] = true;
sz[v] = 1;
int big = 0, mx = 0;
for(auto u : g[v]) if(!vis[u]) {
dfs(u, v);
sz[v] += sz[u];
if(sz[u] > mx) {
mx = sz[u];
big = u;
}
}
if(big == 0) {
c[v].insert(a[v]);
} else {
c[v] = move(c[big]);
c[big].clear();
for(auto u : g[v]) if(u != big) {
for(auto x : c[u]) {
c[v].insert(x);
}
c[u].clear();
}
c[v].insert(a[v]);
}
int nn = sz[v];
int ase = c[v].order_of_key(nn + 1);
ans[v] = nn - ase;
}
};
int32_t main() {
Dark_Lord_Binoy::setup();
Testcase_Handler {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
Dfs g(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g.add_edge(u, v);
}
g.dfs(1);
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
cout << ans[x] << nl;
}
}
print(_Time_);
return 0;
}