#include<bits/stdc++.h> #define MAX_N 300000 usingnamespace std; int n, m, a[MAX_N+5], b[MAX_N+5], d[MAX_N+5], pa[MAX_N+5], dep[MAX_N+5]; vector <int> G[MAX_N+5]; voidDFS(int u, int fa){ for (int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i]; if (v == fa) continue; pa[v] = u, dep[v] = dep[u]+1, DFS(v, u); } } intmain(){ int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) G[i].clear(); for (int i = 1, u, v; i < n; i++) scanf("%d%d", &u, &v), G[u].push_back(v), G[v].push_back(u); dep[1] = 0, DFS(1, 0); int p = 1, mx = 0; for (int i = 1; i <= m; i++) { scanf("%d%d%d", a+i, b+i, d+i); if ((dep[a[i]]+dep[b[i]]-d[i]+1)/2 > mx) p = a[i], mx = (dep[a[i]]+dep[b[i]]-d[i]+1)/2; } for (; dep[p] > mx; p = pa[p]) ; dep[p] = 0, DFS(p, 0); bool flag = true; for (int i = 1; i <= m; i++) if (dep[a[i]]+dep[b[i]] > d[i]) {flag = false; break;} if (flag) printf("TAK %d\n", p); elseputs("NIE"); } return0; }