import java.util.*;

public class A {
    static class Pair {
        int u;
        int v;
        int speed;

        Pair(int u, int v, int speed) {
            this.u = u;
            this.v = v;
            this.speed = speed;
        }
    }

    static int parent[];
    static int sizes[];

    static int findParent(int a) {
        if (parent[a] == -1 || parent[a] == a)
            return a;
        return parent[a] = findParent(parent[a]);
    }

    static boolean isConnected(int a, int b) {
        if (findParent(a) == findParent(b))
            return true;
        return false;
    }

    static void connect(int a, int b) {
        if (isConnected(a, b))
            return;
        if (sizes[findParent(a)] > sizes[findParent(b)]) {
            int temp = a;
            a = b;
            b = temp;
        }
        parent[findParent(a)] = findParent(b);
        sizes[findParent(b)] += sizes[findParent(a)];
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int testCases = sc.nextInt();

        for (int t = 0; t < testCases; t++) {

            int n = sc.nextInt();
            int m = sc.nextInt();
            int k = sc.nextInt();

            parent = new int[n + 1];
            sizes = new int[n + 1];

            for (int i = 0; i < n + 1; i++) {
                parent[i] = i;
            }

            ArrayList<Pair> al = new ArrayList<>();

            for (int i = 0; i < m; i++) {
                int u = sc.nextInt();
                int v = sc.nextInt();
                int s = sc.nextInt();

                al.add(new Pair(u, v, s));
            }

            Collections.sort(al, (Pair a, Pair b) -> {
                return a.speed - b.speed;
            });

            int count = 0;

            int index = -1;

            for (int i = al.size() - 1; i >= 0; i--) {
                if (al.get(i).speed <= k) {
                    index = i;
                    break;
                }
            }

            if (index == al.size() - 1) {
                sb.append(Math.abs(al.get(index).speed - k)).append("\n");
                continue;
            }

            int ans = Integer.MAX_VALUE;

            for (int i = index; i >= 0 && count < n - 1; i--) {
                Pair cur = al.get(i);

                int u = cur.u;
                int v = cur.v;
                int s = cur.speed;

                if (!isConnected(u, v)) {
                    connect(u, v);
                    ans = Math.min(ans, Math.abs(k - s));
                }

                count++;

            }

            if (count == n - 1) {
                if (Math.abs(al.get(index + 1).speed - k) >= ans) {
                    sb.append(ans).append("\n");
                } else {
                    sb.append(Math.abs(al.get(index + 1).speed - k)).append("\n");
                }
                continue;
            }

            ans = 0;

            for (int i = index + 1; i < al.size() && count < n - 1; i++) {
                Pair cur = al.get(i);

                int u = cur.u;
                int v = cur.v;
                int s = cur.speed;

                if (!isConnected(u, v)) {
                    connect(u, v);
                    ans = ans + s - k;
                }

                count++;
            }

            sb.append(ans).append("\n");

        }

        System.out.print(sb);
    }
}