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)) D = [-1] * n E = [-1] * n E[0] = 0 D[0] = 0 B = [0] while A:= B: B = [] for now in A: for nxt, x in node[now]: if D[nxt] == -1: D[nxt] = D[now]+1 E[nxt] = E[now]^x B.append(nxt) if E[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)