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

using namespace std;

char src[102030];
char dst[102030];

bool prefixesEqual(int n) {
  for(int i=0;i<n;i++) {
    if(src[i] != dst[i]) {
      return false;
    }
  }
  return true;
}

bool allOnesOrAllZeros(char* a, int n) {
  bool zeroExists = false;
  bool oneExists = false;
  for(int i=0;i<n;i++) {
    if(a[i] == '0') {
      zeroExists = true;
      if(oneExists) break;
    } else {
      oneExists = true;
      if(zeroExists) break;
    }
  }
  return zeroExists ^ oneExists;
}

bool solve(vector<vector<int>>& ed) {
  int n = ed.size();
  if(prefixesEqual(n)) {
    return true;
  }
  if(allOnesOrAllZeros(src, n)) {
    return false;
  }
  if(allOnesOrAllZeros(dst, n)) {
    return true;
  }

  bool linear = true;
  bool q = false;
  int single = -1;
  int i = 0;
  while((i<n) && (linear || !q)) {
    if(single == -1) {
      if(ed[i].size() == 1) {
        single = i;
      }
    }
    if(ed[i].size() >= 3) {
      linear = false;
    }
    if(!q) {
      vector<int>& neighbours = ed[i];
      for(auto it=neighbours.begin();it!=neighbours.end();it++) {
        if(dst[i] == dst[*it]) {
          q = true;
          break;
        }
      }
    }
    i++;
  }
  if(!linear) {
    return q;
  }
  int cur = single;
  int prev = -1;
  int srcStart = src[single];
  int dstStart = dst[single];
  int srcPrev = srcStart;
  int dstPrev = dstStart;
  int srcLen = 0;
  int dstLen = 0;
  while((ed[cur].size() == 2) || (cur == single)) {
    vector<int>& neighbours = ed[cur];
    for(auto it=neighbours.begin();it!=neighbours.end();it++) {
      if((*it) != prev) {
        prev = cur;
        cur = *it;
        break;
      }
    }
    if(srcPrev != src[cur]) {
      srcLen++;
      srcPrev = src[cur];
    }
    if(dstPrev != dst[cur]) {
      dstLen++;
      dstPrev = dst[cur];
    }
  }
  if(srcLen < dstLen) {
    return false;
  }
  if(srcLen == dstLen) {
    return srcStart == dstStart;
  }
  return true;
}

void read01Str(char* a, int n) {
  int i = 0;
  for(;;) {
    a[i] = getchar();
    if(a[i] >= '0' && a[i] <= '1') {
      i++;
      break;
    }
  }
  for(;i<n;i++) {
    a[i] = getchar();
  }
}

int main(int argc, char** argv) {
  int t, n, a, b;
  scanf("%d",&t);
  for(int i=0;i<t;i++) {
    scanf("%d",&n);
    read01Str(src, n);
    read01Str(dst, n);
    vector<vector<int>> ed(n, vector<int>());
    for(int j=0;j<n-1;j++) {
      scanf("%d%d", &a, &b);
      a--;
      b--;
      ed[a].push_back(b);
      ed[b].push_back(a);
    }
    bool res = solve(ed);
    printf(res?"TAK\n":"NIE\n");
  }
  return 0;
}