import heapq from itertools import product from itertools import permutations import random import math import bisect import sys from collections import defaultdict from itertools import combinations inf = 1 << 60 def exit(): sys.exit() def LI(): return list(map(int, input().split())) def II(): return int(input()) def SI(): return input() class dijkstra: def __init__(self, g): self.g = g def shortest_path(self, start): import heapq n = len(self.g) dist = [float("inf")] * n dist[start] = 0 pq = [(0, start)] while pq: d, u = heapq.heappop(pq) if dist[u] < d: continue for v, w in self.g[u]: nd = d + w if dist[v] > nd: dist[v] = nd heapq.heappush(pq, (nd, v)) return dist t = II() for _ in range(t): n, m = LI() items = [LI() for _ in range(m)] g = [[] for _ in range(n)] g2 = [[] for _ in range(n)] for item in items: a, b, c = item a, b = a-1, b-1 if c == 1: g[a].append((b, 1)) g[b].append((a, 1)) elif c == 2: g2[a].append((b, 1)) g2[b].append((a, 1)) dij = dijkstra(g).shortest_path(0) dij2 = dijkstra(g2).shortest_path(0) if dij[-1] != float("inf"): print("Same") print(dij[-1]) elif dij2[-1] != float("inf"): print("Different") print(dij2[-1]) else: print("Unknown")