#if __has_include() #include using namespace atcoder; #else #include #if __has_include() #include using namespace atcoder; #endif #endif using namespace std; #define int long long #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for(int i = (int)((n) - 1); i >= 0; i--) template bool chmax(T &a,const T &b){if(a bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;} // using mint = modint; void solve(){ int n, m; cin >> n >> m; vector equal(n, vector()); auto notequal(equal); while(m--){ int a, b, c; cin >> a >> b >> c; a--; b--; auto&& target = c == 1 ? equal : notequal; target.at(a).emplace_back(b); target.at(b).emplace_back(a); } // const int inf = 1e9; { vector d(n, -1); d.at(0) = 0; deque bfs{0}; while(!bfs.empty()){ auto from = bfs.front(); bfs.pop_front(); for(auto to : equal.at(from)){ if(d.at(to) != -1) continue; d.at(to) = d.at(from) + 1; bfs.emplace_back(to); } } if(d.back() != -1){ println("Same\n{}", d.back()); return; } } { vector d(n, vector(2, -1)); d.at(0).at(0) = 0; deque> bfs{{0, 0}}; while(!bfs.empty()){ auto [fromi, fromj] = bfs.front(); bfs.pop_front(); if(fromj == 0){ for(auto to : equal.at(fromi)){ if(d.at(to).at(0) != -1) continue; d.at(to).at(0) = d.at(fromi).at(0) + 1; bfs.emplace_back(to, 0); } for(auto to : notequal.at(fromi)){ if(d.at(to).at(1) != -1) continue; d.at(to).at(1) = d.at(fromi).at(0) + 1; bfs.emplace_back(to, 1); } } else{ for(auto to : equal.at(fromi)){ if(d.at(to).at(1) != -1) continue; d.at(to).at(1) = d.at(fromi).at(1) + 1; bfs.emplace_back(to, 1); } } } if(d.back().at(1) != -1){ println("Different\n{}", d.back().at(1)); return; } } println("Unknown"); } signed main(){ int t; cin >> t; while(t--){ solve(); } } /* =が証明できる場合は、0からn-1までを=で繋げる必要があるので、 c=1の辺のみを追加したグラフでbfsすればよい !=が証明できる場合は、0からiまでが=で繋がり、n-1からjまでが=で繋がり、 iとjが!=で繋がっている必要がある */