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
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
using namespace std;

const int N = 100000 + 5;

char buf1[N];
char buf2[N];

int nextInt() { int n; scanf("%d", &n); return n; }

int n;
vector<vector<int> > g;
vector<int> deg;
int maxDeg;

bool solve() {
	string s1(buf1);
	string s2(buf2);
	if (s1 == s2) return true;
	if (s1 == string(n, '0')) return false;
	if (s1 == string(n, '1')) return false;

	if (maxDeg > 2) {
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < g[i].size(); ++j)
				if (buf2[i] == buf2[g[i][j]])
					return true;
		return false;
	}

	vector<int> leaf;
	int cnt1 = 0;
	int cnt2 = 0;

	for (int i = 0; i < n; ++i) {
		if (deg[i] == 1) leaf.push_back(i);
		for (int j = 0; j < g[i].size(); ++j) {
			int ii = g[i][j];
			if (buf1[i] != buf1[ii]) ++cnt1;
			if (buf2[i] != buf2[ii]) ++cnt2;
		}
	}
	if (cnt1 < cnt2) return false;
	if (cnt1 > cnt2) return true;
	if (buf1[leaf[0]] == buf2[leaf[0]]) return true;
	return false;
}

int main() {
	int TC = nextInt();
	for (int tc = 0; tc < TC; ++tc) {
		n = nextInt();
		scanf("%s%s", buf1, buf2);
		g.clear();
		g.resize(n, vector<int>());
		deg.clear();
		deg.resize(n, 0);
		maxDeg = 0;
		for (int i = 1; i < n; ++i) {
			int a = nextInt() - 1;
			int b = nextInt() - 1;
			g[a].push_back(b);
			g[b].push_back(a);
			++deg[a];
			++deg[b];
			if (deg[a] > maxDeg) maxDeg = deg[a];
			if (deg[b] > maxDeg) maxDeg = deg[b];
		}
		bool res = solve();
		printf("%s\n", res ? "TAK" : "NIE");
	}

	return 0;
}