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
#include<bits/stdc++.h>
#define ST first
#define ND second
#define ll long long

using namespace std;

const int SIZE = 5e5 + 5;

vector<int> adj[SIZE];

int changes1 = 0, changes2 = 0;
string s1,s2;

void dfs1(int x, int prev){
	if(prev != -1){
		if(s1[x-1] != s1[prev-1]) changes1++;
		if(s2[x-1] != s2[prev-1]) changes2++;
	}
	for(auto it : adj[x])
		if(it != prev) dfs1(it,x);
}

bool dfs2(int x, int prev){
	bool czy = false;
	for(auto it : adj[x]){
		if(it == prev) continue;
		if(s2[it-1] == s2[x-1]) return true;
		else czy |= dfs2(it,x);
		if(czy) return true;
	}
	return czy;
}

int main() {
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
    
    int tt; cin >> tt;
    while(tt--){
    
    	int n; cin >> n;
    	cin >> s1 >> s2;
    	vector<pair<int,int> > edges;
    	vector<int> in(n+1);
    	changes1 = 0, changes2 = 0;
    	
    	for(int i = 1; i <= n; i++){
    		adj[i].clear();
    	}
    	
    	for(int i = 1; i < n; i++){
    		int a,b; cin >> a >> b;
    		edges.push_back({a,b});
    		adj[a].push_back(b);
    		adj[b].push_back(a);
    		in[a]++;
    		in[b]++;
    	}
    	
    	if(s1 == s2){
    		cout << "TAK\n";
    		continue;
    	}
    	
    	if(n == 1){
    		if(s1 == s2) cout << "TAK\n";
    		else cout << "NIE\n";
    		continue;
    	}
    	
    	bool zero11 = false, one11 = false, zero22 = false, one22 = false;
    	
    	for(int i = 0; i < n; i++){
    		if(s1[i] == '0') zero11 = true;
    		else one11 = true;
    	}
    	
    	for(int i = 0; i < n; i++){
    		if(s2[i] == '0') zero22 = true;
    		else one22 = true;
    	}
    	
    	if(!one11 && one22){
    		cout << "NIE\n";
    		continue;
    	}
    	
    	if(!zero11 && zero22){
    		cout << "NIE\n";
    		continue;
    	}
    	
    	int ile1 = 0, ile2 = 0;
    	for(int i = 1; i <= n; i++){
    		if(in[i] == 1) ile1++;
    		else if(in[i] == 2) ile2++;
    	}
    
    	// line
    	if(ile2 == n-2 && ile1 == 2){
    		/*for(int i = 1; i < n; i++){
    			if(s1[i] != s1[i-1]) changes1++;
    			if(s2[i] != s2[i-1]) changes2++;
    		}*/
    		int ktory = -1;
    		for(int i = 1; i <= n; i++){
    			if(in[i] == 1){
    				ktory = i;
    				break;
    			}
    		}
    		dfs1(ktory,-1);
    		if(changes1 == changes2 && s2[ktory-1] == s1[ktory-1]) cout << "TAK\n";
    		else if(changes1 > changes2) cout << "TAK\n";
    		else cout << "NIE\n";
    		continue;
    	}
    	
    	bool ans = false;
    	for(int i = 1; i <= n; i++){
    		if(in[i] < 3) continue;
    		bool zero = false, one = false;
    		for(auto it : adj[i]){
    			if(s2[it-1] == '1') one = true;
    			else zero = true;
    		}
    		if(zero && one) ans = true;
    		else if(zero && s2[i-1] == '0') ans = true;
    		else if(one && s2[i-1] == '1') ans = true;
    		
    	}
    	
    	if(ans){
    		cout << "TAK\n";
    		continue;
    	}
    
    	for(int i = 1; i <= n; i++){
    		if(in[i] < 3) continue;
    		bool zero = false, one = false;
    		for(auto it : adj[i]){
    			if(s2[it-1] == '1') one = true;
    			else zero = true;
    		}   		
    		bool ok = false;
    		if(zero && s2[i-1] == '1'){
    			for(auto it : adj[i]){
    				ok |= dfs2(it,i);
    			}
    		} else if(one && s2[i-1] == '0'){
    			for(auto it : adj[i]){
    				ok |= dfs2(it,i);
    			}
    		}
    		if(ok){
    			ans = true;
    			break;
    		} else {
    			break;
    		}
    	}
    	
    	if(ans) cout << "TAK\n";
    	else cout << "NIE\n";
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
	
    return 0;
}