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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Patryk Jędrzejczak

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, a, b) for(int i = b - 1; i >= a; --i)
#define EB emplace_back
#define MP make_pair
#define ST first
#define ND second
#define S size
#define RS resize
#define endl '\n'
#define L long long

template<class T> using V = vector<T>;
using PI = pair<int, int>;

void generate_paths(V<V<int>>& g, int v, int p, V<V<int>>& paths) {
  if ((p == -1 || g[p].S() > 2) && g[v].S() < 3) {
    V<int> new_path(1, v);
    paths.EB(new_path);
  }
  else if (g[v].S() < 3) {
    paths.back().EB(v);
  }

  for (int u : g[v]) {
    if (u != p) {
      generate_paths(g, u, v, paths);
    }
  }
}

bool check_path(V<int>& path, V<int>& from, V<int>& to) {
  int from_count = 1;
  int to_count = 1;
  int s = int(path.S());

  FOR(i, 1, s) {
    if (from[path[i]] != from[path[i - 1]]) {
      from_count++;
    }
    if (to[path[i]] != to[path[i - 1]]) {
      to_count++;
    }
  }

  if (from[path[0]] != to[path[0]]) {
    from_count--;
  }
  if (s > 1 && from[path[s - 1]] != to[path[s - 1]]) {
    from_count--;
  }

  return to_count <= from_count;
}

bool check_path2(V<int>& path, V<int>& from, V<int>& to) {
  int from_count = 1;
  int to_count = 1;
  int s = int(path.S());

  FOR(i, 1, s) {
    if (from[i] != from[i - 1]) {
      from_count++;
    }
    if (to[path[i]] != to[path[i - 1]]) {
      to_count++;
    }
  }

  if (from[0] != to[path[0]]) {
    from_count--;
  }
  if (s > 1 && from[s - 1] != to[path[s - 1]]) {
    from_count--;
  }

  return to_count <= from_count;
}

void get_path(V<V<int>>& g, int v, int p, V<int>& path) {
  path.EB(v);
  if (g[v].S() == 2) {
    for (int u : g[v]) {
      if (u != p) {
        get_path(g, u, v, path);
      }
    }
  }
}

bool check_swap(V<int>& path, V<int>& to) {
  if (int(path.S()) == 1) {
    return false;
  }

  V<int> new_from1(path.S(), 1 - to[path[0]]);
  FOR(i, 1, int(path.S())) {
    new_from1[i] = 1 - new_from1[i - 1];
  }
  V<int> new_from2(path.S(), 1 - to[path[0]]);
  new_from2[1] = new_from2[0];
  FOR(i, 2, int(path.S())) {
    new_from2[i] = 1 - new_from2[i - 1];
  }

  /*for (int v : path) {
    cout << v << ' ';
  }
  cout << endl;
  for (int v : new_from1) {
    cout << v << ' ';
  }
  cout << endl;
  for (int v : new_from2) {
    cout << v << ' ';
  }
  cout << endl << endl;*/

  return check_path2(path, new_from1, to) || check_path2(path, new_from2, to);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin >> t;
  REP(f, t) {
    int n;
    cin >> n;

    V<int> from(n), to(n);
    string s1, s2;
    int red_from = 0;
    int red_to = 0;
    cin >> s1 >> s2;
    REP(i, n) {
      from[i] = s1[i] - '0';
      to[i] = s2[i] - '0';
      if (from[i] == 0) {
        red_from++;
      }
      if (to[i] == 0) {
        red_to++;
      }
    }

    V<V<int>> g(n);
    V<int> red_adj(n);
    V<int> black_adj(n);
    REP(i, n - 1) {
      int a, b;
      cin >> a >> b;
      a--, b--;

      g[a].EB(b);
      g[b].EB(a);

      if (to[b] == 0) {
        red_adj[a]++;
      }
      else {
        black_adj[a]++;
      }
      if (to[a] == 0) {
        red_adj[b]++;
      }
      else {
        black_adj[b]++;
      }
    }

    if (red_from == 0) {
      if (red_to == 0) {
        cout << "TAK\n";
      }
      else {
        cout << "NIE\n";
      }
      continue;
    }
    if (red_from == n) {
      if (red_to == n) {
        cout << "TAK\n";
      }
      else {
        cout << "NIE\n";
      }
      continue;
    }

    bool achievable = false;
    int start = -1;
    REP(v, n) {
      if (g[v].S() > 2) {
        start = v;
        if (to[v] == 0) {
          if (red_adj[v] > 0) {
            achievable = true;
          }
          for (int u : g[v]) {
            V<int> path;
            get_path(g, u, v, path);
            if (check_swap(path, to)) {
              achievable = true;
            }
          }
        }
        else {
          if (black_adj[v] > 0) {
            achievable = true;
          }
          for (int u : g[v]) {
            V<int> path;
            get_path(g, u, v, path);
            if (check_swap(path, to)) {
              achievable = true;
            }
          }
        }
      }
    }

    if (achievable) {
      cout << "TAK\n";
      continue;
    }

    if (start == -1) {
      start++;
      while (g[start].S() == 2) {
        start++;
      }
    }

    bool wrong = false;
    REP(v, n) {
      if (g[v].S() > 2 && from[v] != to[v]) {
        wrong = true;
      }
    }
    if (wrong) {
      cout << "NIE\n";
      continue;
    }

    V<V<int>> paths;
    generate_paths(g, start, -1, paths);

    for (V<int>& path : paths) {
      if (!check_path(path, from, to)) {
        wrong = true;
      }
    }

    if (wrong) {
      cout << "NIE\n";
    }
    else {
      cout << "TAK\n";
    }
  }
}