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
#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug(x) cerr << #x << " " << x << endl
#define int long long
#define st first
#define nd second

using namespace std;

const int N(1e5 + 13);
const int MAX(LLONG_MAX);

string src, dst;
int n, q, l, r, cnt_b, cnt_r;
bool flag, is_path, is_tree, zjebane_drzewo, has_r, has_b;
string s;

vector <int> G[N];

int32_t main() {
  boost;
  cin >> q;
  while (q--) {
    cin >> n;
    cin >> src;
    cin >> dst;

    cnt_b = 0;
    cnt_r = 0;
    has_r = false;
    has_b = false;

    for (int i = 0; i < src.size(); i++) {
      if (src[i] == '0') {
        cnt_r++;
      } else {
        cnt_b++;
      }
      if (dst[i] == '0') {
        has_r = true;
      } else {
        has_b = true;
      }
    }

    is_path = true;
    is_tree = false;
    zjebane_drzewo = true;

    for (int i = 1; i < n; i++) {
      cin >> l >> r;
      G[l].push_back(r);
      G[r].push_back(l);
    }


    for (int i = 1; i <= n; i++) {
      if (G[i].size() > 2) {
        is_path = false;
        is_tree = true;

        for (int j = 0; j < G[i].size(); j++) {
          if (dst[i] == dst[G[i][j]]) {
            zjebane_drzewo = false;
          }
        }
      }
    }

    if ((has_b && cnt_b == 0) || (has_r && cnt_r == 0)) {
      cout << "NIE\n";
    } else if (src == dst) {
      cout << "TAK\n";

    } else if (is_tree) {
      if (zjebane_drzewo) {
        cout << "NIE\n";
      } else {
        cout << "TAK\n";
      }
    } else {
      cout << "NIE\n";
    }

    for (int i = 1; i <= n; i++) {
      G[i].clear();
    }
  }

}