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
158
159
160
161
162
163
164
165
166
167
168
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define st first
#define nd second

using pii = pair<int, int>;
template<class T>
using vec = vector<T>;

const int N = 1e5 + 7;
vec<int> g[N];
char s[N], d[N];
int n;

int col(char c) {
    return c - '0';
}

pii calc(vec<int>& vs, char* s) {
    int c[2] = {};
    int p = 0;
    for (int v : vs) {
        if (s[v] != s[p])
            ++c[col(s[v])];
        p = v;
    }
    return {c[0], c[1]};
}

bool good(int v, int p, int f, bool r = 0, int de = 0) {
    for (int s : g[v])
        if (s != p)
            r += (col(d[s]) != f);
    for (int s : g[v]) {
        if (s == p)
            continue;
        int c = col(d[s]);
        if (c == f) {
            // if (r)
            // printf("%d %d %d", v, s, p);
            // printf("A: %d: %d %d %d\n", v, s, r, r || de == 0);
            return r || de == 0;
        }
        else if (good(s, v, f, r - 1, de + 1)) {
            // printf("B: %d: 1\n", v);
            return true;
        }
    }
    // printf("C: %d false\n", v);
    return false;
}

bool good(int v) {
    int c0 = 0, c1 = 0;
    for (int s : g[v])
        if (d[s] == '0')
            ++c0;
        else
            ++c1;
    if (c1 != 0 && c0 != 0)
        return true;
    if (c1 != 0 && d[v] == '1')
        return true;
    if (c0 != 0 && d[v] == '0')
        return true;

    int c = (col(d[v]) ^ 1);
    // printf("%d\n", v);
    for (int s : g[v])
        if (good(s, v, c)) {
            // printf("%d", v);
            return true;
        }
    return false;
}

bool solve3() {
    int c[2] = {};
    for (int i = 1; i <= n; ++i)
        ++c[col(s[i])];
    if (c[0] == 0 || c[1] == 0)
        return string(s+1) == string(d+1);

    for (int i = 1; i <= n; ++i)
        if (g[i].size() > 2)
            if (good(i))
                return true;

    return false;
}

bool solve2() {
    if (n == 1)
        return s[1] == d[1];

    int v;
    for (int i = 1; i <= n; ++i)
        if (g[i].size() == 1) {
            v = i;
            break;
        }

    bool change = true;
    vector<int> vs;
    int p = -1;
    do {
        vs.pb(v);
        change = false;
        for (int s : g[v])
            if (s != p) {
                p = v;
                v = s;
                change = true;
                break;
            }
    } while (change);

    auto s1 = calc(vs, s);
    auto s2 = calc(vs, d);

    if (s[vs[0]] != d[vs[0]]) {
        if (s[vs[0]] == '0')
            --s1.st;
        else
            --s1.nd;
    }
    if (s[vs.back()] != d[vs.back()]) {
        if (s[vs.back()] == '0')
            --s1.st;
        else
            --s1.nd;
    }

    return s1.st >= s2.st && s1.nd >= s2.nd;
}

bool solve() {
    scanf("%d", &n);
    scanf("%s%s", s+1, d+1);
    for (int i = 1; i <= n; ++i)
        g[i].clear();
    for (int i = 1; i < n; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].pb(v);
        g[v].pb(u);
    }

    if (string(s+1) == string(d+1))
        return true;

    for (int i = 1; i <= n; ++i)
        if (g[i].size() > 2)
            return solve3();

    return solve2();
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--)
        puts(solve() ? "TAK" : "NIE");

    return 0;
}