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
#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(c) ((int)(c).size())
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> PII;
typedef vector<vector<int> > vvi;
const int INFTY=20000000;
const int MAX=500100;
const int MOD=10000000;

void coutTab(int* tab,int n){
	loop(i,0,n){
		cout<<tab[i]<<" ";
	}
	cout<<"\n";
}

template<class T> void coutVec(vector<T> tab){
	for(T t : tab){
		cout<<t<<" ";
	}
	cout<<"\n";
}
//------------------------------------------
bool all(string s, char a) {
    loop(i,0,sz(s)) if(s[i] != a) return false;
    return true;
}

string compress(const vvi & G, const string & s, int f) {
    string res;
    res.pb(s[f]);
    int l = f;
    f = G[f][0];
    loop(i,1,sz(s)) {
        if(res[sz(res)-1] != s[f]) res.pb(s[f]);
        if(G[f][0] != l) {
            l = f;
            f = G[f][0];
        } else {
            l = f;
            f = G[f][1];
        }
    }
    return res;
}

bool dfs(const string & s, const vvi & G, int v, int l, int d, int e) {
    //ps("v:");ps(v);ps(l);ps(d);ps(s[v]);ps(e);pln(s);
    if(d>2) { return false; }
    if((s[v] - '0') != e) {
        //ps(s[v] - '0');ps(e);pln(v+1);
        return true;
    }
    bool res = false;
    loop(j,0,sz(G[v])) {
        if(G[v][j] != l) {
            res =  res || dfs(s, G, G[v][j], v, d+1, 1-e);
        }
    }
    return res;
}

int find_first(const vvi & G) {
    loop(i,0,sz(G)) if(sz(G[i]) == 1) return i;
}

bool bfs(set<string> & vis, const vvi & G, string bs, const string & exp) {
    //pln(s);
    vis.insert(bs);
    queue<string> Q;
    Q.push(bs);
    while(!Q.empty()) {
        string s = Q.front();
        Q.pop();
        int n = sz(s);
        loop(i,0,n) {
            loop(j,0,sz(G[i])) {
                string s1(s);
                s1[G[i][j]] = s1[i];
                if(vis.find(s1) == vis.end()) {
                    // P[s1] = s;
                    if(exp == s1) return true;
                    vis.insert(s1);
                    Q.push(s1);
                    // if(!pwas && was) pln(s1);
                }
            }
        }
    }
    return false;
}

bool solve() {
    int n;
    cin>>n;
    string s1,s2;
    cin>>s1;
    cin>>s2;
    vvi G(n);
    int a,b;
    loop(i,0,n-1) {
        cin>>a>>b;
        G[a-1].pb(b-1);
        G[b-1].pb(a-1);
    }

    if(s1 == s2) return true;
    if(all(s1, '0') || all(s1, '1')) return false;

    bool exists3 = false;
    loop(i,0,n) if(sz(G[i]) >= 3) { 
        exists3 = true;
        //ps(i);pln("MAM");
        if(dfs(s2, G, i, -1, 0, s2[i] - '0')) {
            return true;
        }
    }

    if(exists3) {
        //pln("COMPRESS");
        set<string> vis;
        return bfs(vis,G,s1, s2);
    } else {
        int f = find_first(G);
        string cs1 = compress(G,s1,f); 
        string cs2 = compress(G,s2,f);

        //ps(cs1);pln(cs2);
        if(cs1[0] == cs2[0] && sz(cs2) <= sz(cs1)) return true;
        else if(cs1[0] != cs2[0] && sz(cs2) <= sz(cs1) - 1) return true;

        return false;
    }
}
int main(){
	ios_base::sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--) {
        if(solve()) {
            pln("TAK");
        } else {
            pln("NIE");
        }
    }
}