import sys
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_int(): return list(map(int, sys.stdin.readline().strip().split()))[0]
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()

N = 0
a = []

def solve():
    global N, a
    n, m, k = get_ints()

    answer = 1e9 + 1
    for i in range(m):
        a, b, s = get_ints()
        answer = min(answer, abs(s-k))

    print(answer)



test_cases = get_int()

for _ in range(test_cases):
    solve()
