#include #include using namespace std; using namespace atcoder; using ld = long double; using ll = long long; using ull = unsigned long long; using VB = vector; using VVB = vector; using VC = vector; using VVC = vector; using VI = vector; using VVI = vector; using VVVI = vector; using VL = vector; using VVL = vector; using VVVL = vector; using VD = vector; using VVD = vector; using VT = vector; using VVT = vector; using P = pair; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++) #define ALL(a) (a).begin(),(a).end() //constexpr int INF = 1001001001; //constexpr ll LINF = 1001001001001001001ll; //constexpr ll LINF = 8e18; //constexpr int DX[] = {-1, -1, 0, 0, 1, 1}; //constexpr int DY[] = {-1, 0, -1, 1, 0, 1}; constexpr int DX[] = {1, 0, -1, 0, 0, 0}; constexpr int DY[] = {0, 1, 0, -1, 0, 0}; //constexpr int DZ[] = {0, 0, 0, 0, 1, -1}; template< typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true); } template< typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true); } using mint = modint998244353; //using mint = modint1000000007; using VM = vector; using VVM = vector; using VVVM = vector; using VVVVM = vector; // テンプレートに追加推奨 inline ll divceil(ll x, ll y) { if(x >= 0) return (x + y - 1) / y; else return -((-x) / y); } inline ll divfloor(ll x, ll y) { if(x >= 0) return x / y; else return -((- x + y - 1) / y); } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; VI a(m), b(m), c(m), d1(n, 1e9), dn(n, 1e9); VVI e(n); REP(i, m) { cin >> a[i] >> b[i] >> c[i]; a[i]--; b[i]--; if (c[i] == 1) { e[a[i]].push_back(b[i]); e[b[i]].push_back(a[i]); } } queue q; q.push(0); d1[0] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : e[u]) { if (d1[v] < 1e9) continue; q.push(v); d1[v] = d1[u] + 1; } } if (d1[n - 1] < 1e9) { cout << "Same\n" << d1[n - 1] << "\n"; continue; } q.push(n - 1); dn[n - 1] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : e[u]) { if (dn[v] < 1e9) continue; q.push(v); dn[v] = d1[u] + 1; } } int ans = 1e9; REP(i, m) { if (c[i] == 2) { chmin(ans, d1[a[i]] + dn[b[i]] + 1); chmin(ans, dn[a[i]] + d1[b[i]] + 1); } } if (ans == 1e9) cout << "Unknown\n"; else cout << "Different\n" << ans << "\n"; } }