#include using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) template bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} const long long mod=998244353; const long long mod2=469762049; struct UnionFind{ int n; vectorsiz,par; UnionFind(int N):n(N){ siz.assign(N+1,1); par.assign(N+1,-1); } int root(int p){ assert(1<=p && p<=n); int res=p; while(true){ if(par[res]==-1) break; res=par[res]; } int a; while(true){ if(par[p]==-1) return p; a=p; p=par[p]; par[a]=res; } } bool unite(int u,int v){ int rootU=root(u),rootV=root(v); if(rootU==rootV) return 0; if(siz[rootU]>=siz[rootV]){ siz[rootU]+=siz[rootV]; par[rootV]=rootU; } else{ siz[rootV]+=siz[rootU]; par[rootU]=rootV; } return 1; } bool same(int a,int b){ return root(a)==root(b); } }; void solve(){ int N,M;cin>>N>>M; int A[M+1],B[M+1],C[M+1]; vector>G[N+1]; UnionFind UF(N); for(int i=1;i<=M;i++){ cin>>A[i]>>B[i]>>C[i]; G[A[i]].push_back({C[i],B[i]}); G[B[i]].push_back({C[i],A[i]}); UF.unite(A[i],B[i]); } if(UF.same(1,N)==false){ cout<<"Unknown"<que; que.push(1); color[1]=0; dist[1]=0; while(!que.empty()){ int pos=que.front(); que.pop(); if(already[pos]) continue; already[pos]=true; for(auto nex:G[pos]){ if(already[nex.second]) continue; if(nex.first==1){ que.push(nex.second); color[nex.second]=color[pos]; } else{ if(color[nex.second]==-1){ que.push(nex.second); } color[nex.second]=color[pos]^1; } if(dist[nex.second]>dist[pos]+1){ dist[nex.second]=dist[pos]+1; que.push(nex.second); } } } if(color[N]==0){ cout<<"Same"<sync_with_stdio(0); cout.tie(0); int T;cin>>T; while(T--) solve(); }