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

#define FOR(i, n) for (int i = 0; i < int(n); ++i)
#define FO(i, a, b) for (int i = (a); i < int(b); ++i)
#define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((b) < (a) ? (a) : (b))

#define REMIN(a, b) ((a) = min(a, b))
#define REMAX(a, b) ((a) = max(a, b))

#define ALL(c) (c).begin(), (c).end()

#define SQR(x) ((x) * (x))

#define PRINT(x) cerr << #x << " == " << x << endl;

#define ZERO(x) memset(x, 0, sizeof(x));

#define endl '\n'

//

const int N = 1e5 + 9;

struct V {
  char fr, to;
  vector<int> tos;

  bool can_give = false;
  bool can_take = false;
};

V vs[N];

void dfs(int v, int parent) {
  bool can_give = false;
  for (auto &vv : vs[v].tos) {
    if (vv == parent)
      continue;

    dfs(vv, v);

    if (vs[vv].to == vs[v].to || vs[vv].can_give)
      can_give = true;
  }
  vs[v].can_give = can_give;
}

void dfs2(int v, int parent) {
  int num_fixers = vs[v].can_take;
  int fixer = -1;

  for (auto &vv : vs[v].tos) {
    bool can_fix = vs[vv].can_give;
    num_fixers += can_fix;

    if (can_fix)
      fixer = vv;
  }

  for (auto &vv : vs[v].tos) {
    vs[vv].can_take = vs[v].to == vs[vv].to || num_fixers > 1 ||
                      (num_fixers == 1 && vv != fixer);
  }

  for (auto &vv : vs[v].tos) {
    if (vv == parent)
      continue;

    dfs2(vv, v);
  }
}

char visited[N];
char to_delete[N];

int have_0 = -1;
int have_len = 0;

int want_0 = -1;
int want_len = 0;

int dfs_line_find(int v, int parent) {
  if (vs[v].tos.size() <= 1u)
    return v;

  for (auto &vv : vs[v].tos) {
    if (vv == parent)
      continue;

    int r = dfs_line_find(vv, v);
    if (r != -1)
      return r;
  }

  return -1;
}

void dfs_line_rec(int v, int parent) {
  //   cerr << "visit " << (int)vs[v].to << endl;

  visited[v] = 1;

  if (have_len == 0) {
    ++have_len;
    have_0 = vs[v].fr;
  } else if ((have_0 ^ !(have_len % 2)) != vs[v].fr)
    ++have_len;

  if (want_len == 0) {
    ++want_len;
    want_0 = vs[v].to;
  } else if ((want_0 ^ !(want_len % 2)) != vs[v].to)
    ++want_len;

  for (auto &vv : vs[v].tos) {
    if (vv == parent)
      continue;

    dfs_line_rec(vv, v);
  }
}

bool dfs_line(int v) {
  int idx = dfs_line_find(v, -1);

  have_0 = -1;
  have_len = 0;

  want_0 = -1;
  want_len = 0;

  dfs_line_rec(idx, -1);

  if (have_0 != want_0) {
    --have_len;
    have_0 ^= 1;
  }

  //   PRINT(want_len)
  //   PRINT(have_len)

  return want_len <= have_len;
}

bool solve() {

  int n;
  cin >> n;

  FOR(i, n) vs[i] = V();

  string s;
  cin >> s;

  FOR(i, n) vs[i].fr = s[i] == '1';

  cin >> s;

  FOR(i, n) vs[i].to = s[i] == '1';

  FOR(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;

    vs[a].tos.push_back(b);
    vs[b].tos.push_back(a);
  }

  bool fr_same = true;
  FOR(v, n - 1) if (vs[v].fr != vs[v + 1].fr) fr_same = false;

  bool to_same = true;
  FOR(v, n - 1) if (vs[v].to != vs[v + 1].to) to_same = false;

  if (fr_same && !to_same) {
    return false;
  }

  if (to_same && !fr_same) {
    return true;
  }

  if (fr_same && to_same) {
    return vs[0].fr == vs[0].to;
  }

  int max_degree = 0;

  FOR(i, n) REMAX(max_degree, (int)vs[i].tos.size());

  if (max_degree <= 2) {
    return dfs_line(0);
  }

  FOR(v, n) if (vs[v].tos.size() >= 3u) {
    for (auto vv : vs[v].tos)
      if (vs[vv].to == vs[v].to) {
        return true;
      }
  }

  dfs(0, -1);
  dfs2(0, -1);

  FOR(v, n) if (vs[v].tos.size() >= 3u) {
    if (vs[v].can_take)
      return true;

    for (auto vv : vs[v].tos)
      if (vs[vv].can_give) {
        return true;
      }

    if (vs[v].fr != vs[v].to)
      return false;
  }

  //   vector<char> to_delete(n, 0);
  FOR(i, n) to_delete[i] = 0;

  FOR(v, n) if (vs[v].tos.size() >= 3u) to_delete[v] = 1;

  FOR(v, n) if (to_delete[v]) { vs[v].tos.clear(); }

  FOR(v, n) FOR(ith, vs[v].tos.size()) if (to_delete[vs[v].tos[ith]]) {
    vs[v].tos[ith] = vs[v].tos.back();
    vs[v].tos.pop_back();
    --ith;
  }

  FOR(i, n) visited[i] = 0;

  FOR(v, n) if (!visited[v]) {
    auto r = dfs_line(v);
    if (!r)
      return false;
  }

  return true;
}

int main() {
  ios_base::sync_with_stdio(0);

  int tc;
  cin >> tc;

  FOR(i, tc) {
    auto r = solve();
    if (r)
      cout << "TAK" << endl;
    else
      cout << "NIE" << endl;
  }

  return 0;
}