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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll INF = 1000000000000000;

int num_of_classes(bool c[], vector<int> tree[], int n, int left, int right) {
    int res = 1;
    int pr = left;
    bool prev = c[left];

    int copy;

    int ind = tree[left][0];
    while (pr != right) {
        if (c[ind] != prev) {
            res++;
        }

        prev = c[ind];
        if (tree[ind][0] != pr) {
            copy = ind;
            ind = tree[copy][0];
            pr = copy;
        }
        else {
            if (ind != right) {
                copy = ind;
                ind = tree[copy][1];
                pr = copy;
            }
            else {
                pr = right;
            }
        }
    }

    return res;
}

bool poss(bool c1[], bool c2[], vector<int> tree[], int n, int left, int right) {
    if (n > 1) {
        if (c1[left] == c2[left] && c1[right] == c2[right]) {
            return num_of_classes(c1, tree, n, left, right) >= num_of_classes(c2, tree, n, left, right);
        }
        else {
            return num_of_classes(c1, tree, n, left, right) > num_of_classes(c2, tree, n, left, right);
        }
    }
    else {
        return c1[1] == c2[1];
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;

    bool res[t + 1];

    for (int i = 1; i <= t; i++) {
        int n;
        cin >> n;

        bool c1[n + 1], c2[n + 1];
        bool same = 1;
        char x;
        for (int j = 1; j <= n; j++) {
            cin >> x;
            c1[j] = (x == '1');
        }
        for (int j = 1; j <= n; j++) {
            cin >> x;
            c2[j] = (x == '1');
        }

        for (int j = 2; j <= n; j++) {
            if (c1[j] != c1[j - 1]) {
                same = 0;
            }
        }

        bool bipartite = 1;

        vector<int> tree[n + 1];
        int a, b;
        for (int j = 1; j < n; j++) {
            cin >> a >> b;
            if (c2[a] == c2[b]) {
                bipartite = 0;
            }
            tree[a].push_back(b);
            tree[b].push_back(a);
        }

        bool list = 1;
        int left = -1;
        int right = -1;
        for (int j = 1; j <= n; j++) {
            if (tree[j].size() > 2) {
                list = 0;
            }
            if (tree[j].size() == 1) {
                if (left == -1) {
                    left = j;
                }
                else {
                    right = j;
                }
            }
        }

        if (list) {
            res[i] = poss(c1, c2, tree, n, left, right);
        }
        else {
            if (same) {
                res[i] = 1;
                for (int j = 1; j <= n; j++) {
                    if (c2[j] != c1[1]) {
                        res[i] = 0;
                    }
                }
            }
            else {
                if (bipartite) {
                    res[i] = 1;
                    for (int j = 1; j <= n; j++) {
                        if (c1[j] != c2[j]) {
                            res[i] = 0;
                        }
                    }
                }
                else {
                    res[i] = 1;
                }
            }
        }
    }

    for (int i = 1; i <= t; i++) {
        if (res[i]) {
            cout << "TAK\n";
        }
        else {
            cout << "NIE\n";
        }
    }

    return 0;
}