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

bool anyBlackInput;
bool anyBlackOutput;
bool anyRedInput;
bool anyRedOutput;
bool anyNotChangingColorEdgeOutput;
int howManyColorChangesInput;
int howManyColorChangesOutput;
bool crossingExist;
int startingLeaf;
int endingLeaf;
string input, output;
int degrees[100009];

void ClearGlobalVariables(int n)
{
    for(int i=1;i<=n;i++)
        degrees[i]=0;
    anyBlackInput=false;
    anyRedInput=false;
    anyBlackOutput=false;
    anyRedOutput=false;
    anyNotChangingColorEdgeOutput = false;
    howManyColorChangesInput=0;
    howManyColorChangesOutput=0;
    crossingExist=false;
    startingLeaf=0;
    endingLeaf=0;
}

void Debug(int n)
{
    cerr<<"n "<<n<<endl;
    cerr<<"anyBlackInput "<<anyBlackInput<<endl;
    cerr<<"anyBlackOutput "<<anyBlackOutput<<endl;
    cerr<<"anyRedInput "<<anyRedInput<<endl;
    cerr<<"anyRedOutput "<<anyRedOutput<<endl;
    cerr<<"anyNotChangingColorEdgeOutput "<<anyNotChangingColorEdgeOutput<<endl;
    cerr<<"howManyColorChangesInput "<<howManyColorChangesInput<<endl;
    cerr<<"howManyColorChangesOutput "<<howManyColorChangesOutput<<endl;
    cerr<<"crossingExist "<<crossingExist<<endl;
    cerr<<"startingLeaf "<<startingLeaf<<endl;
    cerr<<"endingLeaf "<<endingLeaf<<endl;
    cerr<<"input "<<input<<endl;
    cerr<<"output "<<output<<endl;
    cerr<<"degrees"<<endl;
    for(int i=1;i<=n;i++)
        cerr<<degrees[i]<<" ";
    cerr<<endl;
    cerr<<"input==output: "<<(input==output)<<endl;
    cerr<<"input.length()==1: "<<(input.length()==1)<<endl;
    cerr<<"!anyBlackInput and anyBlackOutput: "<<(!anyBlackInput and anyBlackOutput)<<endl;
    cerr<<"!anyBlackInput: "<<(!anyBlackInput)<<endl;
    cerr<<"!anyRedInput and anyRedOutput: "<<(!anyRedInput and anyRedOutput)<<endl;
    cerr<<"!anyRedInput: "<<(!anyRedInput)<<endl;
    cerr<<"crossingExist and anyNotChangingColorEdgeOutput: "<<(crossingExist and anyNotChangingColorEdgeOutput)<<endl;
    cerr<<"crossingExist: "<<(crossingExist)<<endl;
    cerr<<"howManyColorChangesInput>howManyColorChangesOutput: "<<(howManyColorChangesInput>howManyColorChangesOutput)<<endl;
    cerr<<"howManyColorChangesInput==howManyColorChangesOutput and input[startingLeaf-1]==output[startingLeaf-1] and input[endingLeaf-1]==output[endingLeaf-1]: "<<(howManyColorChangesInput==howManyColorChangesOutput and input[startingLeaf-1]==output[startingLeaf-1] and input[endingLeaf-1]==output[endingLeaf-1])<<endl;
}

void ReadInput(int n)
{
    int a,b;
    cin>>input>>output;
    anyBlackInput |= input[0]=='1';
    anyRedInput |= input[0]=='0';
    anyBlackOutput |= output[0]=='1';
    anyRedOutput |= output[0]=='0';
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        degrees[a]++;
        degrees[b]++;
        crossingExist |= degrees[a]>2 or degrees[b]>2;
        anyNotChangingColorEdgeOutput |= output[a-1]==output[b-1];
        anyBlackInput |= input[i]=='1';
        // cerr<<anyBlackInput<<" \""<<input[i]<<"\"  ";
        anyRedInput |= input[i]=='0';
        anyBlackOutput |= output[i]=='1';
        anyRedOutput |= output[i]=='0';
        howManyColorChangesInput += (input[a-1]!=input[b-1] ? 1 : 0);
        // cerr<<a<<" "<<b<<" "<<input[a-1]<<" "<<input[b-1]<<" "<<howManyColorChangesInput<<endl;
        howManyColorChangesOutput += output[a-1]!=output[b-1] ? 1 : 0;
    }
    bool foundStart=false;
    for(int i=1;i<=n;i++)
    {
        if(degrees[i]==1 and !foundStart)
        {
            foundStart=true;
            startingLeaf=i;
        }
        else if(degrees[i]==1)
        {
            endingLeaf=i;
            break;
        }
    }

}

string ResolvePath()
{
    if(howManyColorChangesInput>howManyColorChangesOutput)
        return "TAK";
    if(howManyColorChangesInput==howManyColorChangesOutput and input[startingLeaf-1]==output[startingLeaf-1] and input[endingLeaf-1]==output[endingLeaf-1])
        return "TAK";
    return "NIE";
}

string ResolveTree()
{
    if(input==output)
        return "TAK";
    if(input.length()==1)
        return "NIE";
    if(!anyBlackInput and anyBlackOutput)
        return "NIE";
    if(!anyBlackInput)
        return "TAK";
    if(!anyRedInput and anyRedOutput)
        return "NIE";
    if(!anyRedInput)
        return "TAK";
    if(crossingExist and anyNotChangingColorEdgeOutput)
        return "TAK";
    if(crossingExist)
        return "NIE";
    return ResolvePath();
}

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        cin>>n;
        ClearGlobalVariables(n);
        ReadInput(n);
        //Debug(n);
        cout<<ResolveTree()<<endl;
    }
    return 0;
}