R=range J=lambda:map(int,input().split()) O=print import heapq for t in R(sum(J())): N,M=J();E,W,S=[[]for i in R(N+1)],[N*2]*(N+1),[[0,1]];W[1]=0 for j in R(M):A,B,C=J();w=1-N+C*N;E[A]+=[[B,w]];E[B]+=[[A,w]] while S: w,i=heapq.heappop(S) if w>W[i]:continue for j,v in E[i]: if w+v