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
#include <bits/stdc++.h>
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
template<typename A,typename B>
ostream& operator<<(ostream& out,const pair<A,B>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out,const vector<T>& c) {
    out << "{";
    for(auto it = c.begin(); it != c.end(); it++) {
        if(it != c.begin()) out << ", ";
        out << *it;
    }
    return out << "}";
}
vector<string> c(2);
vector<vector<int>> g;
vector<int> o1,o2;
vector<int> u;
void dfs2(int v) {
    u[v] = 1;
    o1.push_back(c[0][v] - '0');
    o2.push_back(c[1][v] - '0');
    for(int to : g[v]) {
        if(!u[to]) {
            dfs2(to);
        }
    }
}
void solve() {
    int n;
    cin >> n >> c[0] >> c[1];
    int w0 = 0,w1 = 0;
    g.clear();
    g.resize(n);
    o1.clear();
    o2.clear();
    u.clear();
    u.resize(n);
    for(int i = 0; i < n - 1; i++) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    if(c[0] == c[1]) {
        cout << "TAK" << '\n';
        return;
    }
    int d = 0;
    for(int i = 0; i < n; i++) {
        w0 += c[0][i] == '0';
        w1 += c[1][i] == '0';
        d += g[i].size() >= 3;
    }
    if(w0 == 0 || w0 == n) {
        if(w1 == w0) {
            cout << "TAK" << '\n';
        } else {
            cout << "NIE" << '\n';
        }
    } else {
        if(!d) {
            for(int i = 0; i < n; i++) {
                if(g[i].size() == 1) {
                    dfs2(i);
                    break;
                }
            }
            vector<int> v1,v2;
            v1.push_back(o1[0]);
            v2.push_back(o2[0]);
            for(int i = 1; i < n; i++) {
                if(o1[i] != o1[i - 1]) v1.push_back(o1[i]);
                if(o2[i] != o2[i - 1]) v2.push_back(o2[i]);
            }
            if(v1.size() > v2.size()) {
                cout << "TAK" << '\n';
            } else if(v1.size() < v2.size()) {
                cout << "NIE" << '\n';
            } else {
                if(v1[0] == v2[0]) {
                    cout << "TAK" << '\n';
                } else {
                    cout << "NIE" << '\n';
                }
            }
        } else {
            for(int i = 0; i < n; i++) {
                if(g[i].size() >= 3) {
                    bool f = false;
                    for(int to : g[i]) {
                        if(c[1][i] == c[1][to]) {
                            f = true;
                            break;
                        }
                    }
                    if(f) {
                        cout << "TAK" << '\n';
                        return;
                    }
                }
            }
            bool f = true;
            for(int i = 0; i < n; i++) {
                for(int to : g[i]) {
                    if(c[1][to] == c[1][i]) {
                        f = false;
                        break;
                    }
                }
            }
            if(f) {
                cout << "NIE" << '\n';
            } else {
                cout << "TAK" << '\n';
            }
        }
    }
    
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int testNum = 1;
    cin >> testNum;
    for(int i = 1; i <= testNum; i++) {
        solve();
    }
    return 0;
}