#include <bits/stdc++.h> #define ll long long int #define ld long double #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) using namespace std; ll timer = 0; vector<int> tin, tout; vector<vector<int>> ancest, adj, cnt; string s; void dfs(int node, int p) { tin[node] = ++timer; ancest[node][0] = p; for (int i = 1; i <= 20; i++) { ancest[node][i] = ancest[ancest[node][i - 1]][i - 1]; if (ancest[node][i] == 0) break; } cnt[node] = cnt[p]; ++cnt[node][(int)(s[node - 1] - 'a')]; for (auto it : adj[node]) { if (it == p) continue; dfs(it, node); } tout[node] = ++timer; } bool is_ancestor(ll u, ll v) // check if u ancestor of v { return (tin[u] <= tin[v] && tout[u] >= tout[v]); } int lca(ll u, ll v) { if (is_ancestor(u, v)) return u; if (is_ancestor(v, u)) return v; for (int i = 20; i >= 0; --i) { if (!is_ancestor(ancest[u][i], v)) u = ancest[u][i]; } return ancest[u][0]; } void mainSolve() { int n; cin >> n >> s; timer = 0; adj.assign(n + 1, vector<int>()); cnt.assign(n + 1, vector<int>(26, 0)); ancest.assign(n + 1, vector<int>(21, 0)); tin.assign(n + 1, 0); tout.assign(n + 1, 0); for (int i = 1; i < n; i++) { ll a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1, 0); tout[0] = ++timer; int q; cin >> q; while (q--) { int u, v; cin >> u >> v; int ances = lca(u, v); bool poss = false; for (int i = 0; i < 26; i++) { if (cnt[u][i] - cnt[ances][i] > 0 && cnt[v][i] - cnt[ances][i] > 0) poss = true; } if (poss) cout << "YES\n"; else cout << "NO\n"; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fio; int t; cin >> t; while (t--) { mainSolve(); } return 0; }