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
#include <bits/stdc++.h>

using namespace std;

map<string, bool> checked;
unordered_map<int, set<int>> nbh;

bool dfs(const string &g, const string &t, const int &m, const vector<pair<int, int>> &es) {
    if(g == t) {
        return true;
    }

    if(!checked[g]) {
        checked[g] = true;
        for(int i = 0; i < m; i++) {
            string new_g;

            new_g = g;
            new_g[es[i].first] = g[es[i].second];
            if(!checked[new_g] && g != new_g && dfs(new_g, t, m, es)) {
                return true;
            }

            new_g = g;
            new_g[es[i].second] = g[es[i].first];
            if(!checked[new_g] && g != new_g && dfs(new_g, t, m, es)) {
                return true;
            }
        }
    }
    return false;
}

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

    int t;
    cin >> t;
    while(t--) {
        // checked.clear();
        nbh.clear();

        int n;
        cin >> n;
        string a;
        cin >> a;
        string b;
        cin >> b;

        int max_deg = -1;

        bool black = false;
        bool red = false;

        for(int i = 0; i < n; i++) {
            black = black || (a[i] == '0');
            red = red || (a[i] == '1');
        }

        vector<pair<int, int>> es(n - 1);
        for(int i = 0; i < n - 1; i++) {
            int v, u;
            cin >> v >> u;
            nbh[v - 1].insert(u - 1);
            nbh[u - 1].insert(v - 1);
            es[i] = {v - 1, u - 1};

            max_deg = max(max_deg, (int)nbh[v - 1].size());
            max_deg = max(max_deg, (int)nbh[u - 1].size());
        }

        if(n == 1) {
            cout << (a == b ? "TAK" : "NIE") << "\n";
            continue;
        }

        if(a == b) {
            cout << "TAK\n";
            continue;
        }

        if(black == false || red == false) {
            cout << "NIE\n";
            continue;
        }

        if(max_deg < 3) {
            int leaf = -1;
            for(int i = 0; i < n; i++) {
                if(nbh[i].size() == 1) {
                    leaf = i;
                    break;
                }
            }

            string a2 = "";
            string b2 = "";
            while(nbh[leaf].size()) {
                a2 += a[leaf];
                b2 += b[leaf];

                int new_leaf = *nbh[leaf].begin();
                nbh[leaf].erase(new_leaf);
                nbh[new_leaf].erase(leaf);
                leaf = new_leaf;
            }
            a2 += a[leaf];
            b2 += b[leaf];

            string reduced_a = "";
            string reduced_b = "";
            for(int i = 0; i < n; i++) {
                if(reduced_a.back() != a2[i]) {
                    reduced_a += a2[i];
                }
                if(reduced_b.back() != b2[i]) {
                    reduced_b += b2[i];
                }
            }

            // TODO hashes?
            cout << (reduced_a.find(reduced_b) != string::npos ? "TAK" : "NIE") << "\n";

            continue;
        }

        bool trigger = false;
        for(int i = 0; i < n - 1; i++) {
            if(b[es[i].first] == b[es[i].second]) {
                trigger = true;
                break;
            }
        }
        cout << (trigger ? "TAK" : "NIE") << "\n";

        // cout << (dfs(a, b, n - 1, es) ? "TAK" : "NIE") << "\n";
    }

    return 0;
}