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
#include <stdlib.h>
#include <time.h>

#include <cassert>
#include <cstdio>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int MAXN = 100000 + 5;

int n;
char color[MAXN];
int g[MAXN][2];
string s, t;

set<string> visited;


bool dfs(string s) {
    if (visited.find(s) != visited.end()) return false;
    if(s == t) return true;
    visited.insert(s);
    for (int i = 0; i < n-1; i++) {
        int u = g[i][0];
        int v = g[i][1];
        if (s[u] == s[v]) continue;
        int tmp = s[u];
        s[u] = s[v];
        if(dfs(s)) return true;
        s[u] = tmp;
        tmp = s[v];
        s[v] = s[u];
        if(dfs(s)) return true;;
        s[v] = tmp;
    }
    return false;
}

bool solve() {
    visited.clear();
    scanf("%d", &n);
    scanf("%s\n%", color);
    s = string(color);
    scanf("%s\n%", color);
    t = string(color);

    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--;
        b--;
        g[i][0] = a;
        g[i][1] = b;
    }
    return dfs(s);
}

int main() {
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++) printf("%s\n", solve() ? "TAK" : "NIE");
}