#include<bits/stdc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;usingPQ=priority_queue<int,vector<int>,greater<int>>;constintINF=2e18;map<vector<int>,int>mp;intidx=1;structTree{staticconstintM=1000696969;staticconstintX=131;vector<vector<int>>G;vector<int>C;vector<int>sz;intn;voidinit(int_n){n=_n;sz.resize(n+1);G.resize(n+1);for(inti=1;i<=n;i++){sz[i]=0;}}voidadd_edge(intu,intv){G[u].pb(v);G[v].pb(u);}voidfind(intu,intpar){sz[u]=1;intw=0;for(autov:G[u]){if(v==par)continue;find(v,u);sz[u]+=sz[v];w=max(w,sz[v]);}w=max(n-sz[u],w);if(w<=n/2){C.pb(u);}}inthash(intu,intpar){intsum=0;vector<int>res;for(autov:G[u]){if(v==par)continue;res.pb(hash(v,u));}sort(ALL(res));if(!mp[res])mp[res]=idx++;returnmp[res];}};voidsolve(){intn;cin>>n;TreeA;TreeB;intu,v;A.init(n);B.init(n);for(inti=0;i<n-1;i++){cin>>u>>v;A.add_edge(u,v);}for(inti=0;i<n-1;i++){cin>>u>>v;B.add_edge(u,v);}A.find(1,0);B.find(1,0);intres=0;for(autoa:A.C){for(autob:B.C){if(A.hash(a,0)==B.hash(b,0)){res=1;}}}if(res)cout<<"YES\n";elsecout<<"NO\n";}signedmain(){// ios::sync_with_stdio(0);// cin.tie(0);intt=1;cin>>t;while(t--){solve();}}
#include<bits/stdc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;usingPQ=priority_queue<int,vector<int>,greater<int>>;constintINF=2e18;structTree{staticconstintM=1000696969;staticconstintX=131;vector<vector<int>>G;vector<int>C;vector<int>sz;vector<int>H;intn;voidinit(int_n){n=_n;sz.resize(n+1);H.resize(n+1);G.resize(n+1);for(inti=1;i<=n;i++){sz[i]=0;H[i]=0;}H[0]=1;for(inti=1;i<=n;i++){H[i]=(H[i-1]*X)%M;}}voidadd_edge(intu,intv){G[u].pb(v);G[v].pb(u);}voidfind(intu,intpar){sz[u]=1;intw=0;for(autov:G[u]){if(v==par)continue;find(v,u);sz[u]+=sz[v];w=max(w,sz[v]);}w=max(n-sz[u],w);if(w<=n/2){C.pb(u);}}inthash(intu,intpar){intsum=0;vector<int>res;for(autov:G[u]){if(v==par)continue;res.pb(hash(v,u));}sort(ALL(res));intcnt=1;for(autoh:res){sum=(sum+(H[cnt]*h)%M)%M;cnt++;}sum=(sum+(H[cnt]*(sum))%M)%M;if(G[u].size()==1)sum=1;returnsum;}};voidsolve(){intn;cin>>n;TreeA;TreeB;intu,v;A.init(n);B.init(n);for(inti=0;i<n-1;i++){cin>>u>>v;A.add_edge(u,v);}for(inti=0;i<n-1;i++){cin>>u>>v;B.add_edge(u,v);}A.find(1,0);B.find(1,0);intres=0;for(autoa:A.C){for(autob:B.C){if(A.hash(a,0)==B.hash(b,0)){res=1;}}}if(res)cout<<"YES\n";elsecout<<"NO\n";}signedmain(){// ios::sync_with_stdio(0);// cin.tie(0);intt=1;cin>>t;while(t--){solve();}}