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
#include <cstdio>
#include <vector>

const int NN = 100100;
typedef char byte;

byte source[NN];
byte target[NN];

struct Node {
  byte s_color;
  byte t_color;
  bool visited;
  std::vector<int> ns;
} nodes[NN];

bool line(int e) {
  int si = 0, ti = 0;
  while (true) {
    while (source[si] != target[ti]) {
      if (++si == e) {
        return false;
      }
    }
    while (source[si] == target[ti]) {
      if (++ti == e) {
        return true;
      }
    }
  }
}

bool exp_line(int e, byte color) {
  for (int i = 1; i < e; ++i) {
    if (target[i] == target[i - 1]) {
      return true;
    }
  }
  int si = e-1, ti = e-1;
  while (true) {
    while (source[si] != target[ti]) {
      if (si-- == 0) {
        return false;
      }
    }
    while (source[si] == target[ti]) {
      if (ti-- == 0) {
        for (int i = 0; i<=si; ++i) {
          if (source[i] == color) {
            return true;
          }
        }
        return false;
      }
    }
  }
}

bool dfs(int start, int lineIndex) {
  Node *current = nodes + start;
  current->visited = true;
  if (current->ns.size() == 0) {
    return current->s_color == current->t_color;
  }

  if (current->ns.size() > 2) {
    if (current->s_color != current->t_color) {
      return false;
    }
    if (lineIndex && !line(lineIndex)) {
      return false;
    }
    bool r = true;
    for (int n : current->ns) {
      if (!nodes[n].visited) {
        r &= dfs(n, 0);
      }
    }
    return r;
  }

  source[lineIndex] = current->s_color;
  target[lineIndex] = current->t_color;
  for (int n : current->ns) {
    if (!nodes[n].visited) {
      return dfs(n, lineIndex + 1);
    }
  }
  return line(lineIndex + 1);
}

bool is_expendable(int node, byte color, int lineIndex) {
  Node *current = nodes + node;
  current->visited = true;
  source[lineIndex] = current->s_color;
  target[lineIndex] = current->t_color;

  if(current->ns.size() != 2) {
    return exp_line(lineIndex + 1, color);
  } else {
    for (int n: current->ns) {
      if (!nodes[n].visited) {
        return is_expendable(n, color, lineIndex + 1);
      }
    }
  }

  return false;
}

int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; ++i) {


    int n, a, b;
    scanf("%d%s%s", &n, source, target);

    // printf("%d------------------------------\n%d\n%s\n%s\n-------------------------", i, n, source, target);
    bool s_colors[2] = {false, false};
    bool t_colors[2] = {false, false};

    for (int i = 0; i < n; ++i) {
      byte sc = source[i] & 1;
      byte tc = target[i] & 1;

      s_colors[(int)sc] = true;
      t_colors[(int)tc] = true;
      nodes[i + 1].s_color = sc;
      nodes[i + 1].t_color = tc;
    }

    for (int q = n; --q;) {
      scanf("%d%d", &a, &b);
      nodes[a].ns.push_back(b);
      nodes[b].ns.push_back(a);
    }

    if ((!s_colors[0] && t_colors[0]) || (!s_colors[1] && t_colors[1])) {
      printf("NIE");
    } else {
      bool found = false;
      for (int i = 1; !found && i <= n; ++i) {
        Node *current = nodes + i;
        if (current->ns.size() > 2) {
          current->visited = true;
          for (int n : current->ns) {
            found |= nodes[n].t_color == current->t_color || is_expendable(n, current->t_color, 0);
          }
        }
      }

      for (int i = 0; i <= n; ++i) {
        nodes[i].visited = false;
      }

      if (found) {
        printf("TAK");
      } else {
        int start = 1;
        while (nodes[start].ns.size() > 1) {
          ++start;
        }
        printf(dfs(start, 0) ? "TAK" : "NIE");
      }
    }

    printf("\n");

    for (int i = 0; i <= n; ++i) {
      nodes[i].visited = false;
      nodes[i].ns.clear();
    }
  }

  return 0;
}