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
#include<bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

using namespace std;

constexpr int MAX_N = 100000;

int err;
int n;
int kol[2][2];
int kol_pocz[MAX_N+9], kol_kon[MAX_N+9];
vector<int> v[MAX_N+9];
char s[MAX_N+9];

void solve() {
	err = scanf("%d", &n);
	err = scanf("%s", s);
	for(int i = 1; i <= n; ++i) {
		kol_pocz[i] = s[i-1]-'0';
	}
	err = scanf("%s", s);
	bool bol1 = true;
	kol[0][0] = kol[0][1] = kol[1][0] = kol[1][1] = 0;
	for(int i = 1; i <= n; ++i) {
		v[i].clear();
		kol_kon[i] = s[i-1]-'0';
		kol[0][kol_pocz[i]]++;
		kol[1][kol_kon[i]]++;
		bol1 &= kol_pocz[i] == kol_kon[i];
	}
	
	bool bol2 = false;

	for(int i = 1; i < n; ++i) {
		int a,b;
		scanf("%d%d", &a, &b);
		v[a].pb(b);
		v[b].pb(a);
		bol2 |= kol_kon[b] == kol_kon[a];
	}
	//printf("WCZYTANO\n");
	if(bol1) {
		printf("TAK\n");
		return;
	}
	
	for(int k = 0; k < 2; ++k) {
		if(kol[0][k] <= 0 && kol[1][k] > 0) {
			printf("NIE\n");
			return;
		}
	}
	
	if(!bol2) {
		printf("NIE\n");
		return;
	}
	
	for(int i = 1; i <= n; ++i) {
		if(v[i].size() >= 3) {
			printf("TAK\n");
			return;
		}
	}
	
	int leaf1, leaf2;
	for(int i = 1; i <= n; ++i) {
		if(v[i].size() == 1) {
			leaf1 = i;
			break;
		}
	}
	
	for(int i = leaf1+1; i <= n; ++i) {
		if(v[i].size() == 1) {
			leaf2 = i;
			break;
		}
	}
	
	v[leaf1].pb(0);
	v[leaf2].pb(0);
	int par = 0;
	int i = leaf1;
	int pocz_kol = -1;
	int kon_kol = 0;
	int q = 0;
	while(i > 0) {
		//printf("%d -> %d -> %d|%d\n", par, i, v[i][0], v[i][1]);
		if(q < 0) {
			if(kol_pocz[i] != kon_kol) {
				--q;
				kon_kol ^= 1;
			}
			
			if(kol_kon[i] != pocz_kol) {
				pocz_kol ^= 1;
				++q;
			}
		}
		else if(q > 0) {
			if(kol_pocz[i] == kon_kol) {
				--q;
				kon_kol ^= 1;
			}
			
			if(kol_kon[i] != pocz_kol) {
				pocz_kol ^= 1;
				++q;
			}
		}
		if(q == 0) {
			if(kol_pocz[i] != kol_kon[i]) {
				++q;
				pocz_kol = kol_kon[i];
				kon_kol = kol_kon[i];
			}
			else {
				--q;
				pocz_kol = kol_kon[i];
				kon_kol = kol_kon[i];
			}
		}
		
		if(v[i][0] == par) {
			par = i;
			i = v[i][1];
		}
		else {
			par = i;
			i = v[i][0];
		}
	}
	
	if(q > 0) {
		printf("NIE\n");
	}
	else {
		printf("TAK\n");
	}
	return;
}

int main() {
	int T;
	err = scanf("%d", &T);
	while(T--) {
		solve();
	}
	return 0;
}
/*
1
10
1001011111
0110110011
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10



//*/