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
//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7;
int n, a, b;
int rep[2][N];
int roz[2][N];
int spj[2][2];
int stp[N];
char s[2][N];

int fin(int y, int x) {
	if (x != rep[y][x]) {
		rep[y][x] = fin(y, rep[y][x]);
	}
	return rep[y][x];
}

void uni(int y, int a, int b) {
	a = fin(y, a);
	b = fin(y, b);
	if (s[y][a] != s[y][b] || a == b) {
		return;
	}
	if (roz[y][a] < roz[y][b]) {
		swap(a, b);
	}
	roz[y][a] += roz[y][b];
	rep[y][b] = a;
}

void solve() {
	for (int i = 0; i < 2; ++i) {
		for (int j = 1; j <= n; ++j) {
			rep[i][j] = j;
			roz[i][j] = 1;
			stp[j] = 0;
		}
	}
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &a, &b);
		uni(0, a, b);
		uni(1, a, b);
		++stp[a];
		++stp[b];
	}
	spj[0][0] = spj[0][1] = spj[1][0] = spj[1][1] = 0;
	for (int i = 0; i < 2; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (fin(i, j) == j) {
				++spj[i][s[i][j] - '0'];
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		if (stp[i] > 2) {
			if ((spj[1][0] && spj[0][0] == 0) || (spj[1][1] && spj[0][1] == 0)) {
				printf("NIE\n");
			}
			else if (spj[1][0] + spj[1][1] == n) {
				if (spj[0][0] + spj[0][1] < n) {
					printf("NIE\n");
				}
				else if (s[0][1] != s[1][1]) {
					printf("NIE\n");
				}
				else {
					printf("TAK\n");
				}
			}
			else {
				printf("TAK\n");
			}
			return;
		}
		if (stp[i] < 2) {
			a = i;
		}
	}
	if (spj[0][0] + spj[0][1] > spj[1][0] + spj[1][1]) {
		printf("TAK\n");
	}
	else if(spj[0][0] + spj[0][1] == spj[1][0] + spj[1][1] && s[0][a] == s[1][a]) {
		printf("TAK\n");
	}
	else {
		printf("NIE\n");
	}
}

int kr[N][2];
int poc[2];

int go(int x, int a, int b) {
	if (((x & (1 << a)) > 0) == ((x & (1 << b)) > 0)) {
		return x;
	}
	return x ^ (1 << b);
}

void solve2() {
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &kr[i][0], &kr[i][1]);
		--kr[i][0];
		--kr[i][1];
	}
	for (int j = 0; j < 2; ++j) {
		reverse(s[j] + 1, s[j] + 1 + n);
		poc[j] = 0;
		for (int i = 1; i <= n; ++i) {
			poc[j] = poc[j] * 2 + s[j][i] - '0';
		}
	}
	queue <int> que;
	unordered_set <int> mp;
	que.push(poc[0]);
	mp.insert(poc[0]);
	while (!que.empty()) {
		int x = que.front();
		que.pop();
		if (x == poc[1]) {
			printf("TAK\n");
			return;
		}
		for (int i = 1; i < n; ++i) {
			int y = go(x, kr[i][0], kr[i][1]);
			if (!mp.count(y)) {
				que.push(y);
				mp.insert(y);
			}
			y = go(x, kr[i][1], kr[i][0]);
			if (!mp.count(y)) {
				que.push(y);
				mp.insert(y);
			}
		}
	}
	printf("NIE\n");
}

int main() {
	int q=1;
	scanf("%d", &q);
	while(q--) {
		scanf("%d%s%s", &n, s[0] + 1, s[1] + 1);
		if (n < 0) {
			solve2();
		}
		else {
			solve();
		}
	}
	return 0;
}