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
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   Vi         = vector<int>;
using   Pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

bool run() {
	int n; cin >> n;
	string s, t;
	cin >> s >> t;

	vector<Vi> G(n);

	rep(i, 1, n) {
		int u, v; cin >> u >> v;
		G[u-1].pb(v-1);
		G[v-1].pb(u-1);
	}

	if (s == t) {
		return true;
	}

	int k = int(count(all(s), '0'));
	if (k == 0 || k == n) {
		return false;
	}

	rep(i, 0, n) {
		if (sz(G[i]) >= 3) {
			rep(v, 0, n) each(e, G[v]) if (t[v] == t[e]) return true;
			return false;
		}
	}

	Vi path;
	rep(i, 0, n) if (sz(G[i]) == 1) {
		path.pb(i);
		path.pb(G[i][0]);
		break;
	}
	while (sz(G[path.back()]) != 1) {
		auto& vec = G[path.back()];
		path.pb(vec[vec[0] == path[sz(path)-2]]);
	}

	int segs1 = 1, segs2 = 1;

	rep(i, 1, sz(path)) {
		int u = path[i-1], v = path[i];
		segs1 += s[u] != s[v];
		segs2 += t[u] != t[v];
	}

	segs1 -= (s[path[0]] != t[path[0]]);
	segs1 -= (s[path.back()] != t[path.back()]);
	return segs1 >= segs2;
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(12);
	int t; cin >> t;
	while (t--) cout << (run() ? "TAK\n" : "NIE\n");
	return 0;
}