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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

const int SZ = 1000010;
char A[SZ];
char B[SZ];

vector<int> G[100010];

int Q[SZ];
int sn[SZ];

int main()
{
    int TT,n;
    int a,b;
    scanf("%d",&TT);
    while(TT--)
    {
        scanf("%d",&n);
        cin >> A >> B;
        for (int i=0; i<n; i++)
        {
            Q[i]=sn[i]=0;
            G[i].clear();
        }

        for (int i=0; i<n-1; i++)
        {
            scanf("%d%d",&a,&b);
            G[a-1].push_back(b-1);
            G[b-1].push_back(a-1);
        }
        int ah0 = 0, bh0 = 0, ah1 = 0, bh1 = 0;
        for (int i=0; i<n; i++)
        {
            ah0 |= (A[i]=='0');
            ah1 |= (A[i]=='1');
            bh0 |= (B[i]=='0');
            bh1 |= (B[i]=='1');
        }
        if ((bh0 && !ah0) || (bh1 && !ah1))
        {
            cout << "NIE" << endl;
            continue;
        }
        if ((ah0 && !ah1 && bh0 && !bh1) || (!ah0 && ah1 && !bh0 && bh1))
        {
            cout << "TAK" << endl;
            continue;
        }


        int ai=1;
        int hs=0;
        for (int i=0; i<n; i++)
        {
            if (G[i].size() > 2) hs = 1;
            for (int j=0; j<G[i].size(); j++)
            {
                if (B[i]==B[G[i][j]])
                {
                    ai=0;
                    break;
                }
            }
        }
        if (ai)
        {
            int eq = 1;
            for (int i=0; i<n; i++) eq &= (A[i]==B[i]);
            if (eq)
            {
                cout << "TAK" << endl;
                continue;
            }
            else
            {
                cout << "NIE" << endl;
                continue;
            }

        }
        if (hs)
        {
            cout << "TAK" << endl;
            continue;
        }
        else
        {
            int pp;
            for (int i=0; i<n; i++)
            {
                if (G[i].size() == 1)
                {
                    Q[0]=i;
                    sn[i]=1;
                    break;
                }
            }
            int h = 1, tl = 0;
            while (tl < h)
            {
                int ac = Q[tl++];
                for (int i=0; i<G[ac].size(); i++)
                {
                    if (!sn[G[ac][i]])
                    {
                        sn[G[ac][i]]=1;
                        Q[h++]=G[ac][i];
                    }
                }
            }

            /*
            for (int i=0;i<n;i++) cout << Q[i] << " ";
            cout << endl;

            for (int i=0;i<n;i++) cout << A[Q[i]] << " ";
            cout << endl;

            for (int i=0;i<n;i++) cout << B[Q[i]] << " ";
            cout << endl;
            */

            int zA = 0, zB = 0;
            for (int i=1; i<n; i++)
            {
                if (A[Q[i]] != A[Q[i-1]]) zA++;
                if (B[Q[i]] != B[Q[i-1]]) zB++;
            }
            if (zA == zB)
            {
                if (A[Q[0]] == B[Q[0]])
                {
                    cout << "TAK" << endl;
                    continue;
                }
                else
                {
                    cout << "NIE" << endl;
                    continue;
                }
            }
            else if (zA > zB)
            {
                cout << "TAK" << endl;
                continue;
            }
            else
            {
                cout << "NIE" << endl;
                continue;
            }

        }
    }
}