#include #include #include using namespace std; using namespace atcoder; //loop #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) //vector #define ALL(A) A.begin(), A.end() #define RV(A) reverse(ALL(A)) #define RALL(A) A.rbegin(), A.rend() #define SORT(A) sort(ALL(A)) #define RSORT(A) sort(RALL(A)) //input template inline void input(T& a) { cin >> a; } template inline void input_li(T& a) {for(auto &ob:a) cin >> ob;} template inline void input(T&... a) { ((cin >> a), ...); } //output #define Yes(bo) cout << ((bo) ? "Yes":"No") << endl #define YES(bo) cout << ((bo) ? "YES":"NO") << endl #define yes(bo) cout << ((bo) ? "yes":"no") << endl #define Taka(bo) cout << ((bo) ? "Takahashi":"Aoki") << endl //other #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define sz size #define is insert #define ps push #define tp top #define ft front #define pp pop template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;} template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;} //const #define I_MAX 2147483647 #define I_MIN -2147483647 #define UI_MAX 4294967295 #define LL_MAX 9223372036854775807 #define LL_MIN -9223372036854775808 #define ULL_MAX 18446744073709551615 //type using ll = long long; using ull = unsigned long long; using ld = long double; using Pair = pair; using vll = vector; using mint = modint998244353; using mint1 = modint1000000007; const ll Inf = 1LL << 60; //debug #ifdef _DEBUG #define debug(x) cerr << "dbg_var : " << #x << ": " << x << endl #define debug2(x,y) cerr << "dbg_var : " << #x << ": " << x << " "<< #y << ": " << y << endl #define debug3(x,y,z) cerr << "dbg_var : " << #x << ": " << x << " "<< #y << ": " << y << " " << #z << ": " << z < b) {ll res=0;for(auto v:b){res+=v;}return res;} ll GCD(ll a, ll b) {if (b == 0) return a;else return GCD(b, a % b);} ll LCM(ll a, ll b) {ll d=GCD(a,b);return (a/d)*(b/d)*d;} /*zahyou to ka*/ bool poich(ll P,ll Q){return(0<=P&&P dxy{{1,0},{-1,0},{0,1},{0,-1}}; //https://algo-logic.info/calc-pow/ ll dpow(ll x, ll n,ll mod) { ll ret = 1; while (n > 0) { if (n & 1) ret = ret * x % mod; x = x * x % mod; n >>= 1; } return ret; } ll chd21(ll N,ll i,ll j){ return N*i+j; } Pair chd12(ll N,ll X){ return {X/N,X%N}; } //input template void input_v(vector &vec){ for(auto &v:vec)cin >> v; } vector MakePrime(long long MAX_N){ vector Isprime,Prime; Isprime.resize(MAX_N+2,true); Isprime[0]=false; Isprime[1]=false; for(long long i=2;i*i<=MAX_N;i++){ if(Isprime[i]){ for(long long j=i*i;j<=MAX_N;j+=i)Isprime[j]=false; } } for(long long i=0;i struct comb{ public: comb():comb(10000000){} comb(ll N):A(N,1),B(N,1){ for(ll i=0;i A; vector B; }; vector> totu_cover(vector> points){ vector> p=points; sort(p.begin(),p.end()); ll N=p.size(); auto func=[&](bool bo){ stack> st; queue> que; ll k=2*bo-1; for(ll i=N-1;i>=0;i--){ que.push(p[i]); } while(!que.empty()){ auto v=que.front(); if(st.size()<2){ st.push(v); que.pop(); }else{ auto o=st.top();st.pop(); auto t=st.top(); auto[a,b]=t; auto[c,d]=o; auto[e,f]=v; if((f-b)*(a-c)*k>(d-b)*(a-e)*k){ continue; }else{ st.push(o); st.push(v); que.pop(); } } } vector> res; while(!st.empty()){ res.push_back(st.top()); st.pop(); } return res; }; auto v=func(1); auto u=func(0); ll vn=v.size(),un=u.size(); vector> ans; set> cot; for(ll i=vn-1;i>=0;i--){ ans.push_back(v[i]); cot.insert(v[i]); } for(ll i=0;i(const WeightEdge &other)const{ return cost>other.cost; } bool operator<=(const WeightEdge &other)const{ return cost<=other.cost; } bool operator>=(const WeightEdge &other)const{ return cost>=other.cost; } }; //Graph struct Graph{ public: Graph(int n,ll e=0) : graph(n),N(n){ } void addedge(ll u,ll v){ graph[u].push_back(Edge(v)); } int size(){ return N; } int jisuu(int p){ return graph[p].size(); } ll N; vector> graph; }; struct WeightGraph{ public: WeightGraph(int n,ll e=0) : graph(n),N(n){ } void addedge(ll u,ll v,ll c){ graph[u].push_back(WeightEdge(v,c)); } int size(){ return N; } int jisuu(int p){ return graph[p].size(); } ll N; vector> graph; }; vector normal_bfs(Graph graph,ll s=0){ vector dist(graph.size(),Inf); dist[s]=0; queue que; que.push(s); while(!que.empty()){ ll a=que.front();que.pop(); for(auto v:graph.graph[a]){ if(dist[v.to]==Inf){ dist[v.to]=dist[a]+1; que.push(v.to); } } } return dist; } void solve(){ ll N,M;cin >> N >> M; Graph graph(2*N); for(ll i=0;i> a >> b >> c; a--,b--; if(c==1){ graph.addedge(a,b); graph.addedge(b,a); graph.addedge(a+N,b+N); graph.addedge(b+N,a+N); }else{ graph.addedge(a,b+N); graph.addedge(b,a+N); } } auto dist=normal_bfs(graph,0); if(dist[2*N-1]!=Inf){ cout << "Different" << endl; cout << dist[2*N-1] << endl; }else if(dist[N-1]!=Inf){ cout << "Same" << endl; cout << dist[N-1] << endl; }else{ cout << "Unknown" << endl; } } int main(){ ll T=1;cin >> T; while(T--){ solve(); } return 0; }