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
// clang-format off
#include <bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(), (x).end()

template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); }
template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); }

#ifdef LOCAL
const bool debug = true;
#else
const bool debug = false;
#define cerr if (true) {} else cout
#endif
// clang-format on

#define int long long
#define double long double

const int INF = 1e18;
const int mod = 1e9 + 7;

struct Algo {
	int n;
	bool is_s[2];
	bool is_t[2];
	vector<vector<int>> drogi;
	string s, t;
	string ss, tt;
	
	Algo() {
		cin >> n;
		is_s[0] = is_s[1] = false;
		is_t[0] = is_t[1] = false;
		drogi.resize(n + 10);
		
		cin >> s >> t;
		if (odp(s == t, n > 1)) {
			for (int i = 1; i < n; i++) {
				int a;
				cin >> a >> a;
			}
			return;
		}
		
		for (char c : s) {
			is_s[c - '0'] = true;
		}
		for (char c : t) {
			is_t[c - '0'] = true;
		}
		s = "#" + s;
		t = "#" + t;
		
		bool czy3 = false;
		bool czy2 = false;
		
		for (int i = 1; i < n; i++) {
			int a, b;
			cin >> a >> b;
			drogi[a].push_back(b);
			drogi[b].push_back(a);

			if (drogi[a].size() > 2) {
				czy3 = true;
			}
			if (drogi[b].size() > 2) {
				czy3 = true;
			}
			if (t[a] == t[b]) {
				czy2 = true;
			}
		}
		
		if (czy3) {
			bool colors = (!is_t[0] || is_s[0]) && (!is_t[1] || is_s[1]);
			odp(czy2 && colors);
			return;
		}
		
		for (int i = 1; i <= n; i++) {
			if (drogi[i].size() == 1) {
				dfs(i);
				break;
			}
		}
		
		odp(tt.size() + (ss[0] != tt[0]) <= ss.size());
	}
	
	bool odp(bool res, bool fallthroug=false) {
		if (res) {
			cout << "TAK\n";
			return true;
		}
		if (!fallthroug) {
			cout << "NIE\n";
			return true;
		}
		return false;
	}
	
	void dfs(int gdzie, int skad=-1) {
		if (ss.empty() || ss.back() != s[gdzie])
			ss.push_back(s[gdzie]);
		if (tt.empty() || tt.back() != t[gdzie])
			tt.push_back(t[gdzie]);
		
		for (int i : drogi[gdzie]) {
			if (i == skad) continue;
			dfs(i, gdzie);
		}
	}
};

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int t;
    cin >> t;
    while (t--) {
		Algo p;
	}
    
    return 0;
}/*
3
4
1011
1100
1 2
2 3
2 4
2
10
10
1 2
2
10
01
1 2
* TTN

2
1
1
1
1
1
0
* TN

4
5
00110
01100
1 2
2 3
3 4
4 5
5
00110
01101
1 2
2 3
3 4
4 5
5
10110
01100
1 2
2 3
3 4
4 5
5
10110
01101
1 2
2 3
3 4
4 5
* TNTN
*/