#include #include using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define FAST_IO \ ios::sync_with_stdio(false); \ cin.tie(0); const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; int main() { FAST_IO int T; cin >> T; while (T--) { int N, M; cin >> N >> M; vector A(M), B(M), C(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i] >> C[i]; A[i]--; B[i]--; } vector>> G(N); for (int i = 0; i < M; i ++) { G[A[i]].push_back({B[i], C[i]}); G[B[i]].push_back({A[i], C[i]}); } int inf = 1e9; vector ved(N * 2, false); vector d(N * 2, inf); deque q; q.push_back(0); ved[0] = true; d[0] = 0; while (q.size() > 0) { auto cur = q.front(); q.pop_front(); int x = 0; if (cur >= N) { x = N; } for (auto [nex, c] : G[cur - x]) { nex += x; if (x > 0 && c == 2) continue; if (c == 2) { nex += N; } if (ved[nex]) continue; if (d[nex] < d[cur] + 1) continue; d[nex] = d[cur] + 1; ved[nex] = true; q.push_back(nex); } } if (!ved[N-1] && !ved[N * 2 - 1]) { cout << "Unknown" << endl; } else if (ved[N-1]) { cout << "Same" << endl; cout << d[N-1] << endl; } else { cout << "Different" << endl; cout << d[N*2-1] << endl; } } }