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
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

constexpr int M = 1e5+7;

string s1, s2;
vector<int>g[M];
string xd, xd2;

void nie(){
	cout << "NIE\n";
}

void tak(){
	cout << "TAK\n";
}

void dfs(int v, int p){
	if(s1[v-1] != s1[p-1]) xd += s1[v-1];
	if(s2[v-1] != s2[p-1]) xd2 += s2[v-1];
	
	for(auto w:g[v]){
		if(w == p) continue;
		dfs(w, v);
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	bool sc;
	int t, n, a, b, v, i0, i1, j0, j1;
	cin >> t;
	
	while(t--){
		cin >> n;
		cin >> s1 >> s2;
		s1 += '4';
		s2 += '4';
		
		i0 = i1 = j0 = j1 = 0;
		for(int i=0; i<n; i++){
			if(s1[i] == '0') i0++;
			else i1++;
			
			if(s2[i] == '0') j0++;
			else j1++;
		}
		
		for(int i=1; i<n; i++){
			cin >> a >> b;
			g[a].push_back(b);
			g[b].push_back(a);
		}
		
		if(s1 == s2){
			tak();
			goto A;
		}
		if((j0 && !i0) || (j1 && !i1)){
			nie();
			goto A;
		}		
		
		sc = 1;
		for(int i=1; i<=n; i++) if(g[i].size() > 2) sc = 0;
		
		if(sc){
			for(int i=1; i<=n; i++) if(g[i].size() == 1) v = i;
			dfs(v, n+1);
			if(xd.size() < xd2.size()) nie();
			else if(xd.size() > xd2.size()) tak();
			else if(xd.size() == xd2.size() && xd == xd2) tak();
			else nie();
		}
		else{
			for(int i=1; i<=n; i++){
				for(auto w:g[i]){
					if(s2[i-1] == s2[w-1]){
						tak();
						goto A;
					}
				}
			}
			nie();
		}
		
		
		A:;
		s1.clear();
		s2.clear();
		xd.clear();
		xd2.clear();
		for(int i=1; i<=n; i++) g[i].clear();
	}
	
	return 0;
}