#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const char infile[] = "input.in"; const char outfile[] = "output.out"; ifstream fin(infile); ofstream fout(outfile); const int MAXN = 100015; const int oo = 0x3f3f3f3f; const int M = 1234567; const int S = 28; typedef vector Graph[MAXN]; typedef vector :: iterator It; const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; } const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; } const inline void Get_min(int &a, const int b) { if( a > b ) a = b; } const inline void Get_max(int &a, const int b) { if( a < b ) a = b; } int N, power[MAXN]; Graph G; char C[MAXN]; int Ans; inline void DFs(int Node, int father, int hash1, int hash2, int level) { if(hash1 == hash2) Ans = max(Ans, level); for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) if(*it != father) DFs(*it, Node, (hash1 + 1LL * power[level] * C[*it]) % M, (1LL * hash2 * S + C[*it]) % M, level + 1); } int main() { cin.sync_with_stdio(false); #ifndef ONLINE_JUDGE freopen(infile, "r", stdin); freopen(outfile, "w", stdout); #endif int T; cin >> T; power[0] = 1; for(int i = 1 ; i < MAXN ; ++ i) power[i] = (1LL * power[i - 1] * S) % M; while(T -- ) { cin >> N >> C + 1; for(int i = 1 ; i <= N ; ++ i) { C[i] -= ('a' - 1); vector ().swap(G[i]); } for(int i = 1 ; i < N ; ++ i) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } Ans = 0; DFs(1, 0, C[1], C[1], 1); cout << Ans << '\n'; } fin.close(); fout.close(); return 0; }