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
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <tuple>
#include <deque>
#include <string.h>
using namespace std;

#define LL long long

char dccin[100100];
//bool odw[100100];
char dccout[100100];
LL n;

bool dccinbad()
{
	for(int i=0;i<n;i++)
	{
//		printf("dccinbad: %d: %d %d\n",i, dccin[i],dccin[0]);
		if(dccin[i]!=dccin[0])return false;
	}
	return true;
}


bool dccoutbad(const map<LL,vector<LL>>&w)
{
		for(auto [i,v] : w)
		{
			for(auto itv : v)
			{
				if(dccout[i]==dccout[itv])return false;
			}
		}
		return true;
}

int main() {
	LL t;
	scanf("%lld",&t);
	while(t--)
	{
		LL a,b;
		char c;
		scanf("%lld\n%s\n%s\n",&n,dccin,dccout);
		//printf("%lld\n%s\n%s\n",n,dccin,dccout);
		map<LL,vector<LL>>w;
		for(int i=0;i<n-1;i++)
		{
			scanf("%lld %lld",&a,&b);
			w[a-1].push_back(b-1);
			w[b-1].push_back(a-1);
		}
		if(strcmp(dccin,dccout)==0)printf("TAK\n");
		else if(dccinbad())printf("NIE\n");
		else if(dccoutbad(w))printf("NIE\n");
		else printf("TAK\n");
	}
	return 0;
}