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
/*
 *  Copyright (C) 2021  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

#ifdef DEBUG
	template<typename... ArgTypes>
	inline void debug_print(ArgTypes... args) {
		using expand = int[];
		(void)expand{0, ((cerr << args << " "), void(), 0)... };
		cerr << endl;
	}

	#define DBG(...) debug_print(__VA_ARGS__);
	#define DBGx(x) cerr << #x << " " << x << endl;
	#define DBGa(array) cerr << #array << " "; for (auto a: array) { cerr << a << " "; } cerr << endl;
#else
	#define DBG(...)
	#define DBGx(x)
	#define DBGa(array)
#endif


string solve(vector<bool>& source, vector<int>& different, vector<vector<int>>& graph) {
	if (different.size() == 0) {
		return "NIE";
	}
	return "TAK";
}


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

	int t;
	cin >> t;

	int n, a, b;
	char colour;

	vector<bool> source;  // true if red, false if black
	vector<int> different;

	while (t-- > 0) {
		cin >> n;

		source.clear();
		for (int i = 0; i < n; ++i) {
			cin >> colour;
			source.push_back(colour == '0');
		}

		// nodes to flip
		different.clear();
		for (int i = 0; i < n; ++i) {
			cin >> colour;
			if (source[i] && colour == '1') {
				different.push_back(i);
			}
		}

		// tree edges
		vector<vector<int>> graph(n);
		for (int i = 0; i < n - 1; ++i) {
			cin >> a >> b;
			graph[a - 1].push_back(b - 1);
			graph[b - 1].push_back(a - 1);
		}

		cout << solve(source, different, graph) << endl;
	}
}