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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <map> 
#include <string>
#include <vector>  
#include <iostream> 
#include <sstream> 
#include <queue>
#include <algorithm>
#include <assert.h>


using namespace std;

#define ll long long
#define PB 		push_back
#define FOR(a,start,end) 	for(int a=int(start); a<int(end); a++)
#define INF 		INT_MAX
#define SORT(a) 	sort(a.begin(),a.end()) 
#define CL(a,x) 		memset(a,x,sizeof(a))
#define REP(a,x)	for(int a=0;a<x;a++)
#define REP1(a,x)	for(int a=1;a<=x;a++)
#define MP 		make_pair



typedef vector<ll>     vi;
typedef pair<ll, ll>     pii;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vector<ll> > vvi;
typedef vector<vector<string> > vvs;
typedef vector<pair<string, string> > vss;
typedef pair<string, string> pss;
typedef pair<ll, pii> ppii;
typedef vector<ppii> vppii;
typedef vector<vector<pii> > vvii;
typedef vector<vvi> vvvi;


ll b1, n, k, a, b, c, z, m, d, t, m2, s;

vi vk, va, vb, vc;
vvi vva, vvb;
vii vkx;
string w1, w2;

bool szu(string w, char zn) {
	for (int i = 0; i < (int)w.size(); i++) {
		if (w[i] == zn)
			return true;
	}
	return false;
}
vi szu2(string w) {
	vi vk;
	vk.assign(2, 0);
//	char zn = w[0];
	int a = w[0] - '0';
	for (int i = 1; i < (int)w.size(); i++) {
		if (w[i] != w[i - 1]) {
			vk[a]++;
			a = w[i] - '0';
		}
	}

	if (w.back() == '0')
		vk[0]++;
	else
		vk[1]++;

	if (w[0] == '0')
		vk[0] *= -1;
	return vk;
}
string konw(string w) {
	string w3;
	w3.assign(n, '0');
	int a = 0;
	for (int i = 0; i < n; i++) {
		if (vva[i].size() == 1) {
			a = i;
			break;
		}
	}
	w3[0] = w[a];
	int b = vva[a][0];
	for (int i = 1; i < n - 1; i++) {
		w3[i] = w[b];
		if (vva[b][0] == a) {
			a = b;
			b = vva[b][1];
		}
		else {
			a = b;
			b = vva[b][0];
		}
	}
	w3[n - 1] = w[b];
	return w3;
}

bool czynie() {
	vi va;
	va.assign(n, 0);
	vi vk1, vk2;
	vk1.clear(); vk2.clear();
	vk1.push_back(0);
	va[0] = 1;
	while (vk1.size() > 0) {
		for (int i = 0; i < (int)vk1.size(); i++) {
			for (int j = 0; j < (int)vva[vk1[i]].size(); j++) {
				if (va[vva[vk1[i]][j]] == 0) {
					if (w2[vva[vk1[i]][j]] != w2[vk1[i]]) {
						va[vva[vk1[i]][j]] = 1;
						vk2.push_back(vva[vk1[i]][j]);
					}
					else
						return false;
				}
			}
		}
		vk1.swap(vk2);
		vk2.clear();
	}
	return true;
}
int main() {
	//freopen("c:\\wojtek\\uva\\pa\\debug\\t1.in", "rt", stdin);


//	while (1) {
	cin >> t;
	for (int cas = 0; cas < t; cas++) {
		vk.clear();
		cin >> n;
		cin >> w1;
		cin >> w2;
		vva.assign(n, vk);
		for (int i = 0; i < n - 1; i++) {
			cin >> a >> b;
			vva[a - 1].push_back(b - 1);
			vva[b - 1].push_back(a - 1);
		}
		if (n == 1) {
			if (w1[0] == w2[0])
				z = 1;
			else
				z = 0;
		}
		else {
			if (w1.compare(w2) == 0)
				z = 1;
			else {
				k = 0;
				for (int i = 0; i < n; i++) {
					if (vva[i].size() > 2) {
						k = 1;
						break;
					}
				}
				if (k == 1) {
					bool jest0 = szu(w1, '0');
					bool jest1 = szu(w1, '1');
					if (jest0 && jest1) {
						if (czynie())
							z = 0;
						else
							z = 1;
					}
					else
						z = 0;
				}
				else {
					w1 = konw(w1);
					w2 = konw(w2);
					va = szu2(w1);
					vb = szu2(w2);
					if ((abs(va[0]) >= abs(vb[0])) && (va[1] >= vb[1])) {
						if ((va[0] * vb[0] < 0) && (abs(va[0]) == abs(vb[0])) && (va[1] == vb[1])) {
							z = 0;
						}
						else
							z = 1;
					}
					else
						z = 0;
				}
			}
		}
		if (z == 0)
			cout << "NIE" << endl;
		else
			cout << "TAK" << endl;

	}
	//	}

	return 0;

}