import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class SolutionE extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                                            InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionE(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    static class Node {
        //standard attributes
        int index;
        List<Node> neighbors;

        //attributes for dfs
        boolean visited;

        public Node(int index) {
            this.index = index;
            this.visited = false;
            this.neighbors = new ArrayList<>();
        }

        void initDfs() {
            visited = false;
        }

        void dfs() {
            visited = true;
            for (Node neighbor: neighbors) {
                if (!neighbor.visited) {
                    neighbor.dfs();
                }
            }
        }
    }
    static class Edge implements Comparable<Edge> {
        int city1Index;
        int city2Index;
        int weight;

        public Edge(int city1Index, int city2Index, int weight) {
            this.city1Index = city1Index;
            this.city2Index = city2Index;
            this.weight = weight;
        }

        @Override
        public int compareTo(Edge o) {
            return this.weight - o.weight;
        }
    }

    private static void solve() {
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        Node[] nodes = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node(i);
        }
        Edge[] edges = new Edge[m];
        for (int i = 0; i < m; i++) {
            int ui = scanner.nextInt();
            int vi = scanner.nextInt();
            int si = scanner.nextInt();
            nodes[ui-1].neighbors.add(nodes[vi-1]);
            nodes[vi-1].neighbors.add(nodes[ui-1]);
            edges[i] = new Edge(ui-1, vi-1, si);
        }
        Arrays.sort(edges);

        DSU dsu = new DSU(n);

        int startEdge = 0;
        for (int i = 0; i < edges.length; i++) {
            if (edges[i].weight >= k) {
                if (i == 0 || edges[i].weight - k <= k - edges[i-1].weight) {
                    startEdge = i;
                }
                break;
            }
            startEdge = i;
        }

        int changes = Math.abs(k - edges[startEdge].weight);
        for (int i = startEdge; i >= 0; i--) {
            dsu.union(edges[i].city1Index, edges[i].city2Index);
        }

        if (dsu.size[dsu.find(edges[startEdge].city1Index)] != n) {
            for (int i = startEdge+1; i < n; i++) {
                dsu.union(edges[i].city1Index, edges[i].city2Index);
                if (dsu.size[dsu.find(edges[i].city1Index)] == n) {
                    changes = edges[i].weight - k;
                    break;
                }
            }
        }
        out.println(changes);

    }

    static class DSU {
        int[] leaders;
        int[] rank;
        int[] max;
        int[] min;
        int[] size;

        public DSU(int n) {
            this.leaders = new int[n];
            this.rank = new int[n];
            this.max = new int[n];
            this.min = new int[n];
            this.size = new int[n];

            for (int i = 0; i < n; i++) {
                leaders[i] = i;
                max[i] = i;
                min[i] = i;
                size[i] = 1;
                rank[i] = 0;
            }
        }

        public void union(int a, int b) {
            int aLeader = find(a);
            int bLeader = find(b);

            if (aLeader == bLeader) {
                return;
            }

            if (rank[aLeader] == rank[bLeader]) {
                rank[aLeader]++;
            }

            if (rank[aLeader] > rank[bLeader]) {
                max[aLeader] = Math.max(max[aLeader], max[bLeader]);
                min[aLeader] = Math.min(min[aLeader], min[bLeader]);
                size[aLeader] += size[bLeader];
                leaders[bLeader] = aLeader;
            } else {
                max[bLeader] = Math.max(max[aLeader], max[bLeader]);
                min[bLeader] = Math.min(min[aLeader], min[bLeader]);
                size[bLeader] += size[aLeader];
                leaders[aLeader] = bLeader;
            }
        }

        public int find(int u) {
            Stack<Integer> s = new Stack<>();
            int leader = u;
            while (leaders[leader] != leader) {
                s.push(leader);
                leader = leaders[leader];
            }
            while (!s.isEmpty()) {
                leaders[s.pop()] = leader;
            }
            return leader;
        }
    }

    //REMINDERS:
    //- CHECK FOR INTEGER-OVERFLOW BEFORE SUBMITTING

    //- CAN U BRUTEFORCE OVER SOMETHING, TO MAKE IT EASIER TO CALCULATE THE SOLUTION
}