ca8ad706d68c82fae87600095a24c9629b13bd345a77fd37ca1eec2783cd31ba
// https://szkopul.edu.pl/problemset/problem/zdiC31EMJqdJtvrpCJgFlQD_/site/?key=statement
#include <bits/stdc++.h>
constexpr int sizik = 7 * 1001;
using P = std::array<int, 4>;
int rep[sizik];
void init(int op) {
for (int i = 0; i <= op; i++) {
rep[i] = i;
}
}
int Find(int a) {
if (rep[a] != a) rep[a] = Find(rep[a]);
return rep[a];
}
void Union(int a, int b) {
a = Find(a), b = Find(b);
rep[a] = b;
}
// P cp[300'030];
std::vector<std::array<int, 2>> kra[sizik];
int odl[sizik];
void DFS(int v, int p, int w) {
// odl[s][v] = w;
odl[v] = w;
for (const auto& [u, c] : kra[v]) {
if (u == p) continue;
DFS(u, v, std::max(w, c));
}
}
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<P> v(m);
int jakis_tam_counter_z_dluga_nazwa_zebym_nigdy_wiecej_go_nie_zshadolowal = 0;
for (auto& [a, b, c, d] : v) {
std::cin >> a >> b >> c;
d = jakis_tam_counter_z_dluga_nazwa_zebym_nigdy_wiecej_go_nie_zshadolowal++;
}
init(n);
std::sort(v.begin(), v.end(), [](const P& a, const P& b) { return a[2] < b[2]; });
std::vector<bool> isGood(m);
for (const auto& [a, b, c, d] : v) {
if (Find(a) != Find(b)) {
Union(a, b);
kra[a].push_back({b, c});
kra[b].push_back({a, c});
isGood[d] = true;
}
}
std::sort(v.begin(), v.end(), [](const P& a, const P& b) { return a[0] < b[0]; });
int prev = -1;
for (const auto& [a, b, c, d] : v) {
if (isGood[d]) continue;
if (prev != a) {
DFS(a, a, 0);
prev = a;
}
if (c == odl[b])
isGood[d] = true;
else
isGood[d] = false;
}
for (int i = 0; i < m; i++) {
if (isGood[i])
std::cout << "TAK\n";
else
std::cout << "NIE\n";
}
}
int32_t main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
int t = 1;
// std::cin >> t;
for (; t > 0; t--) {
solve();
// std::cout << "TAK\n";
}
return 0;
}