#include #include #include #include #include using namespace std; struct tr { long long a,b,c; tr(int _a=0,int _b=0,int _c=0) { a=_a; b=_b; c=_c; } }; inline bool operator==(tr x,tr y) { return x.a==y.a&&x.b==y.b&&x.c==y.c; } const int MAX_N=100100; const int MOD1=666013; const int MOD2=1000000007; const int MOD3=1000000009; vector A[MAX_N]; char s[MAX_N]; bool viz[MAX_N]; long long pow1[MAX_N],pow2[MAX_N],pow3[MAX_N]; int ans; void dfs(int nod,tr h1,tr h2,int l) { viz[nod]=true; h1.a=(h1.a*26+s[nod]-'a')%MOD1; h1.b=(h1.b*26+s[nod]-'a')%MOD2; h1.c=(h1.c*26+s[nod]-'a')%MOD3; h2.a=(h2.a+(s[nod]-'a')*pow1[l])%MOD1; h2.b=(h2.b+(s[nod]-'a')*pow2[l])%MOD2; h2.c=(h2.c+(s[nod]-'a')*pow3[l])%MOD3; if(h1==h2) { ans=max(ans,l+1); } for(auto it=A[nod].begin(); it!=A[nod].end(); it++) { if(!viz[*it]) { dfs(*it,h1,h2,l+1); } } } void solve() { int n; ans=0; cin>>n; cin>>(s+1); memset(viz,0,sizeof(viz)); for(int i=1;i<=n;i++) { A[i].clear(); } for(int i=1;i>a>>b; A[a].push_back(b); A[b].push_back(a); } dfs(1,tr(),tr(),0); cout<>t;t;t--) { solve(); } return 0; }