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
#include <bits/stdc++.h>
#define REP(i,n) for (int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl;
#define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl;

void init_io() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
}

struct Vertex {
  int color0 = 0;
  int color1 = 0;
  std::vector<Vertex*> edges;
};

std::vector<Vertex> vertices;

void read_input() {
  int n;
  std::cin >> n;
  vertices.clear();
  vertices.resize(n);
  for (Vertex &v : vertices) {
    char c;
    std::cin >> c;
    v.color0 = c-'0';
  }
  for (Vertex &v : vertices) {
    char c;
    std::cin >> c;
    v.color1 = c-'0';
  }
  REP(i, n-1) {
    int a, b;
    std::cin >> a >> b;
    --a; --b;
    vertices[a].edges.push_back(&vertices[b]);
    vertices[b].edges.push_back(&vertices[a]);
  }
}

bool identical() {
  for (const Vertex &v : vertices) {
    if (v.color0 != v.color1) return false;
  }
  return true;
}

bool single_color_start() {
  int color = vertices[0].color0;
  for (const Vertex &v : vertices) {
    if (v.color0 != color) return false;
  }
  return true;
}

bool alternate_goal() {
  for (const Vertex &v : vertices) {
    for (Vertex *w : v.edges) {
      if (v.color1 == w->color1) return false;
    }
  }
  return true;
}

bool path() {
  for (const Vertex &v : vertices) {
    if (v.edges.size() > 2) return false;
  }
  return true;
}

bool solve_path() {
  Vertex *start = nullptr;
  for (Vertex &v : vertices) {
    if (v.edges.size() == 1) {
      start = &v;
      break;
    }
  }
  assert(start != nullptr);
  int groups0 = 1;
  int groups1 = 1;
  Vertex *prev = nullptr;
  Vertex *cur = start;
  for (;;) {
    Vertex *next = nullptr;
    for (Vertex *e : cur->edges) {
      if (e != prev) {
        next = e;
        break;
      }
    }
    if (next == nullptr) break;
    if (cur->color0 != next->color0) ++groups0;
    if (cur->color1 != next->color1) ++groups1;
    prev = cur;
    cur = next;
  }
  if (groups0 > groups1) return true;
  if (groups0 < groups1) return false;
  return start->color0 == start->color1;
}

bool solve() {
  if (identical()) return true;
  if (single_color_start()) return false;
  if (alternate_goal()) return false;
  if (path()) return solve_path();
  return true;
}

int main() {
  init_io();
  int ntc;
  std::cin >> ntc;
  REP(tc, ntc) {
    read_input();
    const bool res = solve();
    std::cout << (res ? "TAK" : "NIE") << '\n';
  }
}