#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } void solve() { int n, m; cin >> n >> m; vector a(m), b(m), c(m); rep(i, 0, m) cin >> a[i] >> b[i] >> c[i]; rep(i, 0, m) a[i]--, b[i]--; atcoder::dsu uf(n); vector> g(n); rep(i, 0, m) if (c[i] == 1) { uf.merge(a[i], b[i]); g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } vector dist(n, 1e9); dist[0] = 0; int s = uf.leader(0), t = uf.leader(n - 1); vector v = {0}; rep(i, 0, v.size()) { int nw = v[i]; for (int nx : g[nw]) { if (chmin(dist[nx], dist[nw] + 1)) v.push_back(nx); } } if (s == t) { cout << "Same\n"; cout << dist[n - 1] << '\n'; return; } v = {n - 1}; dist[n - 1] = 0; rep(i, 0, v.size()) { int nw = v[i]; for (int nx : g[nw]) { if (chmin(dist[nx], dist[nw] + 1)) v.push_back(nx); } } if (s > t) swap(s, t); int ans = 1e9; rep(i, 0, m) if (c[i] == 2) { auto [l, r] = minmax({uf.leader(a[i]), uf.leader(b[i])}); if (l == s && r == t) { chmin(ans, dist[a[i]] + dist[b[i]] + 1); } } if (ans == 1e9) cout << "Unknown\n"; else { cout << "Different\n"; cout << ans << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int t = 1; cin >> t; while (t--) solve(); }