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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, t, a, b;
int w[N][2];
bool d[N][2];
char c;
vector<int> v[N];
const int inf = N + 3;

int bfs(int p, int j) {
  int res = 0;

  queue<int>q;

  for (int i = 0; i < n; i++) {
    w[i][j] = inf;
  }

  w[p][j] = 0;

  while (!q.empty()) {
    a = q.front();
    for (int i = 0; i < v[a].size(); i++) {
      b = v[a][i];
      if (w[b][j] == inf) {
        w[b][j] = w[a][j] + (d[a][j] != d[b][j]);
        q.push(b);
        res = max(res, w[b][j]);
      }
    }
  }

  return res;
}

bool wsz(int a) {
  if (v[a].size() < 3) {
    return false;
  }
  int rozne = 0;
  for (int i = 0; i < v[a].size(); i++) {
    int b = v[a][i];
    if (d[b][0] != d[a][0]) {
      rozne++;
    }
  }
  return (rozne > 0 && rozne < v[a].size());
}

bool test() {
  for (int i = 0; i <= n; i++) {
    if (wsz(i)) {
      return true;
    }
  }

  for (int i = 0; i <= n; i++) {
    if (d[i][0] == d[i][1]) {
      return bfs(i, 0) >= bfs(i, 1);
    }
  }
  return false;
}

int main()
{
  ios_base::sync_with_stdio(0);

  cin >> t;

  while (t--) {
    cin >> n;

    for (int j = 0; j < 2; j++) {
      for (int i = 0; i < n; i++) {
        cin >> c;
        d[i][j] = c - '0';
        v[i].clear();
      }
    }

    for (int i = 1; i < n; i++) {
      cin >> a >> b;
      a--;
      b--;
      v[a].push_back(b);
      v[b].push_back(a);
    }

    if(test()) {
      cout << "TAK" << endl;
    } else {
      cout << "NIE" << endl;
    }
  }

  return 0;
}