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
187
188
189
190
191
192
193
194
195
196
197
198
#include<iostream>
#include<vector>

using namespace std;

void dfs_line(int x,vector<vector<int>>& con,vector<int>& visited,vector<int>& post_order)
{

    //cout<<"$"<<x<<endl;
    visited[x]=1;

    post_order.push_back(x);

    for(int i=0;i<con[x].size();i++)
    {
        int y=con[x][i];

        if(visited[y]==0)
        {
            dfs_line(y,con,visited,post_order);
        }
    }
}

int main()
{
    int z;
    cin>>z;

    for(int g=0;g<z;g++)
    {
        int n;
        cin>>n;

        string start_seq,end_seq;
        cin>>start_seq>>end_seq;

        vector<vector<int>> con(n+1);
        vector<int> deg(n+1,0);

        for(int i=1;i<n;i++)
        {
            int a,b;
            cin>>a>>b;

            con[a].push_back(b);
            con[b].push_back(a);

            deg[a]++;
            deg[b]++;
        }

        int deg_1_counter=0;
        int deg_2_counter=0;
        int start_v=-1;

        for(int x=1;x<=n;x++)
        {
            if(deg[x]==1)
            {
                deg_1_counter++;
                start_v=x;
            }
            else if (deg[x]==2)
            {
                deg_2_counter++;
            }
            
        }

        vector<int> start_color(n+1);
        vector<int> end_color(n+1);

        int red_counter=0;
        int black_counter=0;

        int red_counter_2=0;
        int black_counter_2=0;

        for(int i=1;i<=n;i++)
        {
            if(start_seq[i-1]=='0')
            {
                start_color[i]=0;
                red_counter++;
            }
            else
            {
                start_color[i]=1;
                black_counter++;
            }

            if(end_seq[i-1]=='0')
            {
                end_color[i]=0;
                red_counter_2++;
            }
            else
            {
                end_color[i]=1;
                black_counter_2++;
            }
            
        }

        if((red_counter==0 && red_counter_2!=0)|| (black_counter==0 && black_counter_2!=0))
        {
            cout<<"NIE"<<endl;
        }
        else
        {
            if(deg_1_counter==2 && deg_2_counter==(n-2))
            {
                vector<int> post_order;
                vector<int> visited(n+1,0);

                /*for(int i=1;i<=n;i++)
                {
                    cout<<start_color[i]<<" "<<end_color[i]<<endl;
                }*/

                dfs_line(start_v,con,visited,post_order);

                vector<int> start_arg;
                vector<int> end_arg;

                int start_id=-1;
                int end_id=-1;

                for(int i=0;i<n;i++)
                {
                    int x=post_order[i];

                    //cout<<"#"<<x<<endl;

                    if(start_color[x]!=start_id)
                    {
                        start_arg.push_back(start_color[x]);
                    }

                    start_id=start_color[x];
                }

                /*for(int i=0;i<start_arg.size();i++)
                {
                    cout<<start_arg[i]<<" ";
                }

                cout<<endl;*/

                for(int i=0;i<n;i++)
                {
                    int x=post_order[i];

                    if(end_color[x]!=end_id)
                    {
                        end_arg.push_back(end_color[x]);
                    }

                    end_id=end_color[x];
                }

                /*for(int i=0;i<end_arg.size();i++)
                {
                    cout<<end_arg[i]<<" ";
                }

                cout<<endl;*/

                int st_size=start_arg.size();
                int end_size=end_arg.size();

                if(start_arg[0]!=end_arg[0])
                {
                    st_size--;
                }

                if(st_size>=end_size)
                {
                    cout<<"TAK"<<endl;
                }
                else
                {
                    cout<<"NIE"<<endl;
                }
                
            }
            else
            {
                cout<<"TAK"<<endl;
            }

        }
        
    }

    return 0;
}