def main(): n, m = list(map(int, input().split())) node = [[] for _ in range(n)] for _ in range(m): u, v, x = list(map(lambda x: int(x)-1, input().split())) node[u].append((v, x)) node[v].append((u, x)) D = [-1] * n D[0] = 0 E = [-1] * n E[0] = 0 B = [0] while A:= B: B = [] for now in A: for nxt, x in node[now]: if D[nxt] == -1: if x == 0: D[nxt] = D[now]+1 E[nxt] = E[now] B.append(nxt) else: if E[now]: continue else: D[nxt] = D[now]+1 E[nxt] = 1 B.append(nxt) if D[n-1] == -1: print("Unknown") return 0 elif E[n-1] == 0: print("Same") return D[n-1] else: print("Different") return D[n-1] for _ in range(int(input())): ans = main() if ans: print(ans)