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
#include <bits/stdc++.h>
#define FOR(i,n) for(int i = 0; i<n; ++i)
#define FOREACH(v, a) for(auto & v : (a))
#define X first
#define Y second
#define PR std::pair
#define MPR std::make_pair
typedef long long ll;
typedef std::vector<int> VI;

using namespace std;

#pragma region Ready functions

int fastPower(int a, int b){
    int res=1, pop_a = a;
    while(b){
        if(b&1) res *= pop_a;
        pop_a *= pop_a;
        b>>=1;
    }
    return res;
}

#pragma endregion


bitset<100003> desired,current;

int stp[100003];

vector<int> graph[100003];
int n;

int currentCount[2], desiredCount[2];

vector<bool> desiredSegs;
void DFSDesired(int u, int popu, bool currentColor, bool starterColor){
    
    if(desired[u] != currentColor){
        currentColor = !currentColor;
        desiredSegs.push_back(currentColor);
    }

    for(int v : graph[u]){
        if(v==popu) continue;
        DFSDesired(v, u, currentColor, starterColor);
    }
}

vector<bool> currentSegs;
void DFSCurrent(int u, int popu, bool currentColor, bool starterColor){
    
    if(current[u] != currentColor){
        currentColor = !currentColor;
        currentSegs.push_back(currentColor);
    }

    for(int v : graph[u]){
        if(v==popu) continue;
        DFSCurrent(v, u, currentColor, starterColor);
    }
}

bool CheckPathGraph(){

    //first we check whether beginning and ending are switched
    // then its bad
    //then we che

    //search for path ends (1 and 1)

    int firstEnd=-1, secondEnd;

    for(int i = 0; i<n; i++){
        if(stp[i]==1 && firstEnd==-1) firstEnd = i;
        if(stp[i]==1 && firstEnd!=-1) secondEnd = i;
    }

    //cout<<firstEnd<<" "<<secondEnd<<endl;

    desiredSegs.push_back(desired[firstEnd]);
    currentSegs.push_back(current[firstEnd]);

    DFSDesired(firstEnd, firstEnd, desired[firstEnd], desired[firstEnd]);
    DFSCurrent(firstEnd, secondEnd, current[firstEnd], current[firstEnd]);
    

    if(desiredSegs.size() < currentSegs.size()) return true;
    if(desiredSegs.size() == currentSegs.size() && desiredSegs[0] == currentSegs[0]) return true;

    return false;

}

void ClearEverything(){
    FOR(i,n){
        graph[i].clear();
        stp[i] = 0;
        current[i]=0;
        desired[i]=0;
    }
    currentCount[0] = 0; currentCount[1]=0;
    desiredCount[0] = 0; desiredCount[1]=0;
    currentSegs.clear();
    desiredSegs.clear();

}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    
    int t;
    
    cin>>t;


    while(t--){

        ClearEverything();

        cin>>n;

        for(int i = 0; i<n; i++){
            char x;
            cin>>x;
            current[i] = x-'0';
            currentCount[x-'0']++;
        }
        for(int i = 0; i<n; i++){
            char x;
            cin>>x;
            desired[i] = x-'0';
            desiredCount[x-'0']++;
        }


        bool trzyistnieje = 0;

        for(int i = 0; i<n-1; i++){
            int a,b;
            cin>>a>>b;
            a--; b--;
            stp[a]++;
            stp[b]++;
            graph[a].push_back(b);
            graph[b].push_back(a);
            if(stp[a] >= 3) trzyistnieje=true;
            if(stp[b] >= 3) trzyistnieje=true;
        }

        if(desired == current){
            cout<<"TAK"<<endl;
            continue;
        }

        //zamalowac graf jednym kolorem
        if(desiredCount[0]==n || desiredCount[1]==n){
            cout<<"TAK"<<endl;
            continue;
        }

        //zamalowac graf na kolor ktorego nie ma
        if((currentCount[0]==0 && desiredCount[0]>0) || (currentCount[1]==0 && desiredCount[1]>0)){
            cout<<"NIE"<<endl;
            continue;
        }

        if(trzyistnieje){
            cout<<"TAK"<<endl;
            continue;
        }


        //do the path check

        if(CheckPathGraph()){
            cout<<"TAK"<<endl;
        }
        else{
            cout<<"NIE"<<endl;
        }


    }


    
}