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
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

bool find_pair(std::string& colors, std::unordered_map<int, std::vector<int>>& edges, int id, int prev_id, char prev_color) {
	if (colors[id-1] == prev_color) return true;
	for (int to : edges[id]) {
		if (to != prev_id) {
			bool found = find_pair(colors, edges, to, id, colors[id-1]);
			if (found) return true;
		}
	}
	return false;
}

bool solve() {
	int n;
	std::string init, target;
	std::cin >> n >> init >> target;

	if (n == 1) {
		return init == target;
	}

	std::unordered_map<int, std::vector<int>> edges;
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		std::cin >> a >> b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}

	if (init == target) {
		return true;
	}


	std::vector<int> ends;
	for (int i = 1; i <= n; ++i) {
		if (edges[i].size() == 1) {
			ends.push_back(i);
		}
	}

	if (ends.size() == 2) {
		int current = ends[0];
		int previous = -1;
		bool last_red = init[current - 1] != '0';
		std::vector<bool> init_colors;
		while (current != ends[1]) {
			if (init[current - 1] == '0' && !last_red) {
				init_colors.push_back(true);
				last_red = true;
			}
			else if (init[current - 1] == '1' && last_red) {
				init_colors.push_back(false);
				last_red = false;
			}
			for (auto vertex : edges[current]) {
				if (vertex != previous) {
					previous = current;
					current = vertex;
					break;
				}
			}
		}
		if (init[current - 1] == '0' && !last_red) {
			init_colors.push_back(true);
			last_red = true;
		}
		else if (init[current - 1] == '1' && last_red) {
			init_colors.push_back(false);
			last_red = false;
		}


		current = ends[0];
		previous = -1;
		last_red = target[current - 1] != '0';
		std::vector<bool> target_colors;
		while (current != ends[1]) {
			if (target[current - 1] == '0' && !last_red) {
				target_colors.push_back(true);
				last_red = true;
			}
			else if (target[current - 1] == '1' && last_red) {
				target_colors.push_back(false);
				last_red = false;
			}
			for (auto vertex : edges[current]) {
				if (vertex != previous) {
					previous = current;
					current = vertex;
					break;
				}
			}
		}
		if (target[current - 1] == '0' && !last_red) {
			target_colors.push_back(true);
			last_red = true;
		}
		else if (target[current - 1] == '1' && last_red) {
			target_colors.push_back(false);
			last_red = false;
		}

		int count = init_colors.size();
		if (init_colors[0] != target_colors[0]) {
			--count;
		}
		if (init_colors[init_colors.size() - 1] != target_colors[target_colors.size() - 1]) {
			--count;
		}

		return count >= static_cast<int>(target_colors.size());
	}
	else {
		bool red = true, black = true;
		for (int i = 0; i < n; ++i) {
			if (target[i] == '0') red = false;
			else black = false;
		}
		for (int i = 0; i < n; ++i) {
			if (init[i] == '0') red = true;
			else black = true;
		}

		bool pair = find_pair(target, edges, ends[0], -1, '2');

		return red && black && pair;
	}
}

int main() {
	int t;
	std::cin >> t;
	while (t--) {
		std::cout << (solve() ? "TAK" : "NIE") << std::endl;
	}

	return 0;
}