#include using namespace std; #include using namespace atcoder; using ll=long long; using ld=long double; using uint=unsigned int; using ull=unsigned long long; using vi=vector; using vvi=vector; using vvvi=vector; using vd=vector; using vvd=vector; using vvvd=vector; using vld=vector; using vvld=vector; using vvvld=vector; using vc=vector; using vvc=vector; using vvvc=vector; using vs=vector; using vvs=vector; using vvvs=vector; using vl=vector; using vvl=vector; using vvvl=vector; using vb=vector; using vvb=vector; using vvvb=vector; using pi=pair; using vpi=vector; using vvpi=vector; using vvvpi=vector; using pl=pair; using vpl=vector; using vvpl=vector; using vvvpl=vector; using si=set; using vsi=vector; using vvsi=vector; using vvvsi=vector; using sl=set; using vsl=vector; using vvsl=vector; using vvvsl=vector; using ss=set; using svi=set; using svvi=set; using svvvi=set; using svl=set; using svvl=set; using svvvl=set; using svs=set; using sb=set; using spi=set; using svpi=set; using svvpi=set; using svvvpi=set; using spl=set; using svpl=set; using svvpl=set; using svvvpl=set; template using PQ=priority_queue; template using PQG=priority_queue,greater>; template using uset=unordered_set; template using mset=multiset; template using umset=unordered_multiset; template using umap=unordered_map; template using mmap=multimap; template using ummap=unordered_multimap; template void set_MOD(T a){ atcoder::modint::set_mod(a); } using mint=atcoder::modint; //using mint=modint998244353; //using mint=modint1000000007; using vm=vector; using vvm=vector; using vvvm=vector; using vvvvm=vector; #define each(i,...) for(auto&& i:__VA_ARGS__) #define each2(i,j,...) for(auto&& [i,j]:__VA_ARGS__) #define rep(i, n) for (ll i = 0; i < (int)(n); i++) #define rep2(i,s,n) for (ll i = (s); i < (int)(n); i++) #define rep3(i,n) for (ll i = 1; i <= (int)(n); i++) #define rep4(i,n) for (ll i = (int)(n)-1;i >= 0;i--) #define Yes(a) cout<<(a?"Yes":"No")<<"\n" #define YES(a) cout<<(a?"YES":"NO")<<"\n" #define Takahashi(a) cout<<(a?"Takahashi":"Aoki")<<"\n" #define Alice(a) cout<<(a?"Alice":"Bob")<<"\n" #define First(a) cout<<(a?"First":"Second")<<"\n" #define Possible(a) cout<<(a?"Possible":"Impossible")<<"\n" #define which(a,s,t) cout<<(a?s:t)<<"\n" #define fls flush(cout) #define fix(a) cout< T SUM(const S &v){return accumulate(all(v),T(0));} #define SZ(v) (ll)(v.size()) #define UNIQ(v) v.erase(unique(all(v),v.end())) template bool chmax(T &a, const S &b){return(a bool chmin(T &a, const S &b){return(a>b?a=b,1:0);} #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define INTD(...) int __VA_ARGS__;in2(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define LLD(...) ll __VA_ARGS__;in2(__VA_ARGS__) #define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__) #define ULLD(...) ull __VA_ARGS__;in2(__VA_ARGS__) #define STR(...) string __VA_ARGS__;in(__VA_ARGS__) #define CHR(...) char __VA_ARGS__;in(__VA_ARGS__) #define DBL(...) double __VA_ARGS__;in(__VA_ARGS__) #define LD(...) ld __VA_ARGS__;in(__VA_ARGS__) #define VI(name,size) vi name(size);in(name) #define VID(name,size) vi name(size);in2(name) #define VL(name,size) vl name(size);in(name) #define VLD(name,size) vl name(size);in2(name) #define VS(name,size) vs name(size);in(name) #define VEC(type,name,size) vector name(size);in(name) #define VV(type,name,h,w) vector> name(h,vector(w));in(name) template pair &operator++(pair &p){p.fi++;p.se++;return p;} template pair operator++(pair &p, int){auto r=p;p.fi++;p.se++;return r;} template pair &operator--(pair &p){p.fi--;p.se--;return p;} template pair operator--(pair &p, int){auto r=p;p.fi--;p.se--;return r;} template vector &operator++(vector &v){for(auto&& a:v)a++;return v;} template vector operator++(vector &v,int){auto r=v;for(auto&& a:v)a++;return r;} template vector &operator--(vector &v){for(auto&& a:v)a--;return v;} template vector operator--(vector &v,int){auto r=v;for(auto&& a:v)a--;return r;} namespace io { inline void scan(){} inline void scan(int &a){cin>>a;} inline void scan(ll &a){cin>>a;} inline void scan(uint &a){cin>>a;} inline void scan(ull &a){cin>>a;} inline void scan(char &a){cin>>a;} inline void scan(string &a){cin>>a;} inline void scan(float &a){cin>>a;} inline void scan(double &a){cin>>a;} inline void scan(ld &a){cin>>a;} inline void scan(mint &a){ ll b;scan(b); a=b; } inline void scan(vb &v){ rep(i,v.size()){ int a; scan(a); v[i]=a; } } template inline void scan(vector &v); template inline void scan(array &v); template inline void scan(pair &p); template inline void scan(T (&v)[size]); template inline void scan(vector &v){for(auto &a:v)scan(a);} template inline void scan(deque &v){for(auto &a:v)scan(a);} template inline void scan(array &v){for(auto &a:v)scan(a);} template inline void scan(pair &p){scan(p.fi);scan(p.se);} template inline void scan(T (&v)[size]){for(auto &a:v)scan(a);} template inline void scan(T &a){cin>>a;} inline void in(){} inline void in2(){} template inline void in(F &f,B &...b){ scan(f); in(b...); } template inline void in2(F &f,B &...b){ scan(f); --f; in2(b...); } inline void print(){cout<<" ";} inline void print(const bool &a){cout< inline void print(const vector &v); template inline void print(const array &v); template inline void print(const pair &p); template inline void print(const T (&v)[size]); template inline void print(const vector &v){ if(v.empty())return; print(v[0]); for(auto i=v.begin();++i!=v.end();){ cout<<' '; print(*i); } } template inline void print(const deque &v){ if(v.empty())return; print(v[0]); for(auto i=v.begin();++i!=v.end();){ cout<<' '; print(*i); } } template inline void print(const array &v){ print (v[0]); for(auto i=v.begin();++i!=v.end();){ cout<<' '; print(*i); } } template inline void print(const pair &p){print(p.fi);print();print(p.se);} template inline void print(const T (&v)[size]){ print(v[0]); for(auto i=v;++i!=end(v);){ cout<<' '; print(*i); } } template inline void print(const T &a){cout< inline void out(const T &t){ print (t); cout<<"\n"; } template inline void out2(T &t){ ++t; out(t); } template inline void out(const F &f,const B &...b){ print(f); cout<<' '; out(b...); } template inline void out2(F &f,B &...b){ ++f; print(f); cout<<' '; out2(b...); } struct IOsetup{ IOsetup(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(16); } }iosetup; } using namespace io; template using fentree=fenwick_tree; const int INF=1<<30; const ll LINF=1LL<<60; const ld EPS=1e-9; const ld Pi=3.141592653589793238; const int dx[8]={0,1,0,-1,-1,-1,1,1}; const int dy[8]={1,0,-1,0,-1,1,-1,1}; namespace my_grids { bool outgrid(int x,int y,int H,int W){ return !(0<=x&&x prime_table(ll a){//O(N log log N) vb prime(a+1,true); if(a>=0)prime[0]=false; if(a>=1)prime[1]=false; for(int i=2;i*i<=a;i++){ if(!prime[i])continue; for(int j=i+i;j<=a;j+=i){ prime[j]=false; } } return prime; } vector divisor(ll a){//O(N) every query vl rtn(0); vl big(0); for(ll i=1;i*i<=a;i++){ if(a%i!=0)continue; rtn.pb(i); if(i*i!=a)big.pb(a/i); } while(big.size()){ rtn.pb(big.back()); big.ppb(); } return rtn; } vector> prime_factorize(ll a){//O(N) every query vpl rtn(0); for(ll p=2;p*p<=a;p++){ if(a%p!=0)continue; int num=0; while(a%p==0){ num++; a/=p; } rtn.pb({p,num}); } if(a!=1)rtn.pb({a,1}); return rtn; } template T extgcd(T a,T b,T &x,T &y){//O(log N), ax+by=gcd(a,b) T d=a; if(b!=0){ d=extgcd(b,a%b,y,x); y-=(a/b)*x; }else{ x=1; y=0; } return d; } namespace mintnCk{ const int nCkmax=5000000; vector fac(0),finv(0),inv(0); void nCkinit(){ const ll mod=mint::mod(); fac.resize(nCkmax,0); finv.resize(nCkmax,0); inv.resize(nCkmax,0); fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; rep2(i,2,nCkmax){ fac[i]=fac[i-1]*i; inv[i]=mod-inv[mod%i]*(mod/i); finv[i]=finv[i-1]*inv[i]; } } template mint nCk(T n,S k){ if(n fac(0),finv(0),inv(0); template void nCkinit(T a){ mod=a; fac.resize(nCkmax,0); finv.resize(nCkmax,0); inv.resize(nCkmax,0); fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; for(int i=2;i ll nCk(T n,S k){ if(n void useintnCk(T a){ using namespace intnCk; nCkinit(a); } } using namespace my_numbers; void solve(){ INT(N,M); vvi ok(N),ng(N); rep(i,M){ INTD(A,B,C); if(C==0){ ok[A].pb(B); ok[B].pb(A); }else{ ng[A].pb(B); ng[B].pb(A); } } queue q;q.em(0); vi distok(N,-1);distok[0]=0; while(q.size()){ int now=q.front();q.pop(); each(i,ok[now]){ if(distok[i]==-1){ distok[i]=distok[now]+1; q.em(i); } } } q.em(N-1); vi distng(N,-1);distng[N-1]=0; while(q.size()){ int now=q.front();q.pop(); each(i,ng[now]){ if(distng[i]==-1){ distng[i]=distng[now]+1; q.em(i); } } } bool ac=false,wa=false; int ans=INF; rep(i,N){ if(distok[i]!=-1&&distng[i]!=-1){ wa=true; chmin(ans,distok[i]+distng[i]); } } if(distok[N-1]!=-1)ac=true,chmin(ans,distok[N-1]); if(distng[0]!=-1)wa=true,chmin(ans,distng[0]); if(ac){ out("Same"); out(ans); }else if(wa){ out("Different"); out(ans); }else{ out("Unknown"); } } int main(){ set_MOD(1000000007); set_MOD(998244353); int T=1; in(T); while(T--)solve(); }