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
#include <bits/stdc++.h>
using namespace std;

vector<int> G[100005];
int a[100005];
int b[100005];


void dfs_path(int v, int p, int &ch, int pr, int c[])
{
    if(c[v]!=pr)ch++;
    for(int u:G[v])if(u!=p)
    { 
        dfs_path(u,v,ch,c[v],c);
    }
}

void solve()
{
    int n;
    cin>>n;
    for(int i=0;i<=n;i++)G[i].clear();
    string s;
    cin>>s;
    for(int i=0;i<n;i++)a[i+1]=s[i]-'0';
    cin>>s;
    for(int i=0;i<n;i++)b[i+1]=s[i]-'0';
    for(int i=1;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int suma=0, sumb=0;
    for(int i=1;i<=n;i++)suma+=a[i];
    for(int i=1;i<=n;i++)sumb+=b[i];
    if(suma==0 && sumb!=0)return cout<<"NIE"<<endl, (void)0;
    if(suma==n && sumb!=n)return cout<<"NIE"<<endl, (void)0;

    int mdeg=0;
    for(int i=0;i<=n;i++)mdeg=max(mdeg, (int)G[i].size());
    if(mdeg<=2)
    {
        int leaf=0;
        for(int i=1;i<=n;i++)if(G[i].size()==1)leaf=i;
        int cha=0, chb=0;
        dfs_path(leaf,0,cha,a[leaf],a);
        dfs_path(leaf,0,chb,a[leaf],b);
        if(chb>cha)return cout<<"NIE"<<endl, (void)0;
        else return cout<<"TAK"<<endl, (void)0;
    }
    else
    {
        bool ident=true;
        for(int i=1;i<=n;i++)if(a[i]!=b[i])ident=false;
        if(ident)return cout<<"TAK"<<endl, (void)0;
        bool same=false;
        for(int i=1;i<=n;i++)for(int u: G[i])if(b[i]==b[u])same=true;
        if(same)return cout<<"TAK"<<endl, (void)0;
        else return cout<<"NIE"<<endl, (void)0;
    }
}


int main()
{
    ios_base::sync_with_stdio(0);
    int Z;
    cin>>Z;
    while(Z--)solve();
}