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
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int,int>;
using vpii = vector<pii>;
using graph = vector<vi>;

#define FOR(name__, upper__) for (int name__ = 0; name__ < (upper__); ++name__)
#define all(x) begin(x), end(x)
#define mp make_pair
#define mt make_tuple

template<class T>
void initialize_matrix(vector<vector<T>>& matrix, int rows, int cols, T value) {
	assert(matrix.empty());
	FOR (row, rows) 
		matrix.emplace_back(cols, value);
}

// beacon node -> deg(u) > 2 & not (adjacent nodes have different color than u)
// -> one beacon node allows for painting whole graph
bool check_beacon_node(const graph& adj, vi& target_color) {
    FOR (u, (int) adj.size()) {
        if (adj[u].size() <= 2) continue;

        int adj_colors = 0;

        for (int v : adj[u]) {
            adj_colors |= (1 << target_color[v]);
        }

        if ((adj_colors & (1 << target_color[u])) != 0) {
            return true;
        }
    }
    return false;
}

bool check_colors_subset(vi& current, vi& target) {
    int n = current.size();
    int target_colors = 0;
    int current_colors = 0;

    FOR (i, n) {
        current_colors |= (1 << current[i]);
        target_colors |= (1 << target[i]);
    }
    return ((target_colors & current_colors) == target_colors);
}

void dfs(graph& adj, const vi& junction_or_leaf, const vi& color, vi& parent, vi& junction_parent, vi& color_dist, int u, int p = -1) {
    if (p == -1) {
        color_dist[u] = 1;
        parent[u] = junction_parent[u] =  -1;
    }
    else {
        parent[u] = p;
        junction_parent[u] = (junction_or_leaf[p] == 1 ? p : junction_parent[p]);
        color_dist[u] = (junction_or_leaf[p] == 1 ? 1 : color_dist[p]);
        color_dist[u] += (color[p] != color[u] ? 1 : 0);
    }

    for (int v : adj[u]) {
        if (v != p) {
            dfs(adj, junction_or_leaf, color, parent, junction_parent, color_dist, v, u);
        }
    }
}

bool check_moving_edges(graph& adj, vi& current, vi& target) {
    int n = current.size();
    vi junction_or_leaf(n, 0);
    vi color_dist(n, 0);

    int root = -1;
    FOR(u, n) {
        if (adj[u].size() != 2) junction_or_leaf[u] = 1;
        if (root < 0 && adj[u].size() == 1) root = u;
    }

    vi target_dist(n), current_dist(n);
    vi parent(n), junction_parent(n);

    dfs(adj, junction_or_leaf, current, parent, junction_parent, current_dist, root);
    dfs(adj, junction_or_leaf, target, parent, junction_parent, target_dist, root);

    // build a graph
    graph diffs(n);

    bool junction_exists = false;
    FOR (u, n) {
        if (!junction_or_leaf[u]) continue;

        if (adj[u].size() > 2) junction_exists = true;

        int dist = current_dist[u] - target_dist[u];

        if (u != root) {
            diffs[junction_parent[u]].push_back(dist);
            diffs[u].push_back(dist);
        }
    }

    // moving edges
    // FOR (u, n) { cout << u << ": { "; for (int x : diffs[u]) cout << x << " "; cout << "}\n"; }
    // exist vertex with deg(v) > 
    FOR (u, n) {
        if (std::any_of(diffs[u].begin(), diffs[u].end(), [](int x){ return x < 0; })) return false;
        // junction
        if (adj[u].size() > 2 && all_of(diffs[u].begin(), diffs[u].end(), [](int x){ return x == 0; })) {
            if (current[u] != target[u]) return false;
        }
    }

    // edge case -> tree withouth junctions
    if (!junction_exists) {
        FOR (u, n) {
            for (int x : diffs[u]) {
                if (x == 0 && current[u] != target[u]) return false;
            }
        }
    }

    return true;
}

bool go() {
    int n; cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    vi current(n, 0), target(n, 0);

    FOR (i, n) {
        current[i] = (s1[i] == '1' ? 1 : 0);
        target[i] = (s2[i] == '1' ? 1 : 0);
    }

    graph adj(n);
    FOR (i, n - 1) {
        int u, v; cin >> u >> v;
        u--, v--;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    if (s1 == s2) { 
        // cout << "EQUAL - ";
        return true; 
    }
    else if (!check_colors_subset(current, target)) {
        // cout << "COLORS_SUBSET - ";
        return false;
    }
    else if (check_beacon_node(adj, target)) {
        // assert -> last condition is true: colors(target) c colors(current)
        // cout << "BEACON_NODE - ";
        return true;
    }
    else {
        // cout << "MOVING_EDGES - ";
        return check_moving_edges(adj, current, target);
    }
      
}

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

    int t; cin >> t;
    while (t--) cout << (go() ? "TAK" : "NIE") << '\n';

	return 0;
}