import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353
# from sys import stdin
# input = stdin.readline
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
try:
    # sys.setrecursionlimit(int(pow(10,7)))
    sys.stdin = open("input.txt", "r")
    # sys.stdout = open("../output.txt", "w")
except:
    pass
def pmat(A):
    for ele in A:
        print(*ele,end="\n")
def seive():
    prime=[1 for i in range(10**6+1)]
    prime[0]=0
    prime[1]=0
    for i in range(10**6+1):
        if(prime[i]):
            for j in range(2*i,10**6+1,i):
                prime[j]=0
    return prime

for _ in range(L()[0]):
    n,m,k=L()
    def rec(i,cost,travelled,mx):
        global ans
        if travelled == n:
            # print(i,cost,travelled,mx)
            if mx<k:
                ans=min(ans,k-mx)
            else:
                ans=min(cost,ans)
            return
        for neigh in g[i]:
            if vis[neigh[0]]==1:
                continue
            # print(i,neigh)
            vis[neigh[0]]=1
            rec(neigh[0],cost+max(0,neigh[1]-k),travelled+1,min(k,max(mx,neigh[1])))
            vis[neigh[0]]=0
    global ans
    ans=10**20
    vis=[0 for i in range(n+1)]
    g=[[] for i in range(n+1)]
    for i in range(m):
        x,y,s=L()
        g[x].append([y,s])
        g[y].append([x,s])
    vis[1]=1
    # print(k,g)
    rec(1,0,1,0)
    print(ans if ans!=10**20 else 0)


                            





    












endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")

