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
#include <cstdio>
#include <vector>
using namespace std;

const int maxN = 1e5 + 10;
char t0[maxN];
char t1[maxN];

struct Prog {
    vector<vector<int>> nbh;
    int N;

    void checkline() { puts(":-DDD"); }

    Prog() {
        scanf("%d%s%s", &N, t0 + 1, t1 + 1);
        int mxd = 0;
        nbh.resize(N + 1);
        bool same_edge = false;
        for (int a, b, i = 1; i < N; ++i) {
            scanf("%d%d", &a, &b);
            nbh[a].push_back(b);
            nbh[b].push_back(a);
            mxd = max<int>(mxd, max(nbh[a].size(), nbh[b].size()));
            same_edge |= t1[a] == t1[b];
        }

        bool equal = true;
        int cnt0 = 0, cnt1 = 0, d1 = 0;
        for (int i = 1; i <= N; ++i) {
            equal &= t0[i] == t1[i];
            cnt0 += t0[i] == '1';
            cnt1 += t1[i] == '1';
            if (nbh[i].size() == 1)
                d1 = i;
        }

        if (equal) {
            puts("TAK");
            return;
        }
        if (cnt0 == 0 || cnt0 == N) {
            puts("NIE");
            return;
        }
        if (mxd > 2) {
            puts(same_edge ? "TAK" : "NIE");
            return;
        }

        int x = nbh[d1][0], px = d1;
        int ch0 = t0[x] != t0[px];
        int ch1 = t1[x] != t1[px];
        while (nbh[x].size() == 2) {
            int c = px;
            px = x;
            x = nbh[x][0] ^ nbh[x][1] ^ c;
            ch0 += t0[x] != t0[px];
            ch1 += t1[x] != t1[px];
        }
        puts((ch0 + (t0[d1] == t1[d1]) > ch1) ? "TAK" : "NIE");
    }
};

int main() {
    int _;
    scanf("%d", &_);
    while (_--)
        Prog();
}