1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
const int MX=100100;
int t,it,n,i,x,y,c[2],u[2][MX];
vector<int> g[MX],leaves;
char s[2][MX];
void dfs(int x, int i) {
  u[x][i]=it;
  for (int j: g[i]) if (u[x][j]!=it && s[x][j]==s[x][i]) dfs(x,j);
}
int main() {
  scanf("%d",&t);
  for (it=1; it<=t; it++) {
    scanf("%d",&n);
    scanf("%s",s[0]+1);
    scanf("%s",s[1]+1);
    for (i=1; i<=n; i++) g[i].clear();
    for (i=1; i<n; i++) {
      scanf("%d%d",&x,&y);
      g[x].push_back(y);
      g[y].push_back(x);
    }
    for (i=1; i<=n; i++) if (s[0][i]!=s[1][i]) break;
    if (i>n) { puts("TAK"); continue; }
    leaves.clear();
    c[0]=c[1]=0;
    for (i=1; i<=n; i++) {
      if (g[i].size()==1) leaves.push_back(i);
      for (x=0; x<2; x++) if (u[x][i]!=it) {
        ++c[x];
        dfs(x,i);
      }
    }
    if (c[0]==1) { puts("NIE"); continue; }
    if (leaves.size()==2) {
      if (c[1]<c[0]) { puts("TAK"); continue; }
      if (c[1]>c[0]) { puts("NIE"); continue; }
      if (s[0][leaves[0]]!=s[1][leaves[0]]) { puts("NIE"); continue; }
      if (s[0][leaves[1]]!=s[1][leaves[1]]) { puts("NIE"); continue; }
      puts("TAK");
    } else puts((c[1]==n)?"NIE":"TAK");
  }
  return 0;
}