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
#include <bits/stdc++.h>
#define ll long long
#define pii pair <int,int>
#define ppi pair <pii, pii>
#define psi pair <State, int>
#define fi first
#define se second
using namespace std;


template <typename T>
ostream & operator << (ostream &out, const vector <T> &V) {
	if (!V.empty()) {
		out << "{";
		for (auto v : V)
			out << v << ", ";
		out << "\b\b}";			// \b is backspace
	}
	else {
		out << "{}";
	}
	return out;
}

template <class T1, class T2>
ostream & operator << (ostream &out, const pair <T1, T2> &P) {
	out << "{" << P.first << ", " << P.second << "}";
	return out;
}


const int MAX = 100005;
int n;
string c1, c2;
vector <pii> Edges;
vector <int> Neighs[MAX];


bool test_obvious() {
	int z1 = count(c1.begin(), c1.end(), '0'), o1 = n - z1;
	int z2 = count(c2.begin(), c2.end(), '0'), o2 = n - z2;
	if (z1 == n || o1 == n) {
		cout << (z1 == z2 ? "TAK" : "NIE") <<endl;
		return true;
	}
	if (z2 == n) {
		cout << (z1 > 0 ? "TAK" : "NIE") <<endl;
		return true;
	}
	if (o2 == n) {
		cout << (o1 > 0 ? "TAK" : "NIE") <<endl;
		return true;
	}
	return false;
}



void testcase_brutal() {
	unordered_set <string> S;
	queue <string> Q;
	Q.push(c1);
	S.insert(c1);
	while (!Q.empty()) {
		string s = Q.front();
		// cout << s <<endl;
		Q.pop();
		for (pii& e : Edges) {
			if (s[e.fi] != s[e.se]) {
				s[e.fi] = s[e.se];
				if (S.find(s) == S.end()) {
					Q.push(s);
					S.insert(s);
				}
				s[e.fi] = s[e.se] = '0' + ('1' - s[e.fi]);
				if (S.find(s) == S.end()) {
					Q.push(s);
					S.insert(s);
				}
				s[e.se] = '0' + ('1' - s[e.fi]);
			}
		}
	}
	cout << (S.find(c2) != S.end() ? "TAK" : "NIE") <<endl;
}



inline int count_sim(string &a, string &b) {
	int ans = 0;
	for (int i = 0; i < a.size(); i++) {
		ans += (a[i] == b[i]);
	}
	return ans;
}


void testcase_all() {
	unordered_set <string> S;
	priority_queue < pair <int, string> > Q;
	Q.push({count_sim(c1, c2), c1});
	S.insert(c1);
	while (S.find(c2) == S.end() && !Q.empty() && S.size() * n < 5e7) {
		auto &r = Q.top();
		Q.pop();
		string s = r.se;
		for (pii& e : Edges) {
			if (s[e.fi] != s[e.se]) {
				s[e.fi] = s[e.se];
				if (S.find(s) == S.end()) {
					Q.push({count_sim(s, c2), s});
					S.insert(s);
				}
				s[e.fi] = s[e.se] = '0' + ('1' - s[e.fi]);
				if (S.find(s) == S.end()) {
					Q.push({count_sim(s, c2), s});
					S.insert(s);
				}
				s[e.se] = '0' + ('1' - s[e.fi]);
			}
		}
	}
	cout << (S.find(c2) != S.end() ? "TAK" : "NIE") <<endl;
}


int main() 
{
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
		
	int T;
	cin >> T;
	for (int t = 0; t < T; t++) {
		cin >> n;
		cin >> c1 >> c2;
		Edges.clear();
		Edges.resize(n-1);
		for (int i = 0; i < n; i++) {
			Neighs[i].clear();
		}
		for (int i = 0; i < n-1; i++) {
			cin >> Edges[i].fi >> Edges[i].se;
			Edges[i].fi--, Edges[i].se--;
			Neighs[Edges[i].fi].push_back(Edges[i].se);
			Neighs[Edges[i].se].push_back(Edges[i].fi);
		}
		// cout << "Testcase " << t <<endl;
		bool is_obvious = test_obvious();

		if (is_obvious) {
			continue;
		}
		if (n <= 1000000) {
			testcase_brutal();
		}
		else {
			testcase_all();
		}
	}
	return 0;
}