import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.AbstractCollection;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author kamel
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskJ solver = new TaskJ();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class TaskJ {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int k = in.nextInt();
            PriorityQueue<Data> pq = new PriorityQueue<>(Comparator.comparingInt(l -> l.weight));
            for (int i = 0; i < m; i++) {
                int x = in.nextInt();
                int y = in.nextInt();
                int weight = in.nextInt();
                pq.add(new Data(x, y, weight));
            }

            DSU dsu = new DSU(n + 1);
            int result = Integer.MAX_VALUE;
            int cost = 0;
            while (!pq.isEmpty()) {
                Data top = pq.poll();
                cost += Math.max(0, top.weight - k);
                int size = dsu.merge(top.x, top.y);
                if (size == n) {
                    if (top.weight < k) {
                        result = Math.min(result, k - top.weight);
                    } else {
                        result = Math.min(result, cost);
                    }
                }
            }
            out.println(result);
        }

        class DSU {
            int n;
            int[] parent;
            int[] size;

            DSU(int n) {
                this.n = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            int find(int x) {
                int px = parent[x];
                if (x == px) {
                    return px;
                }
                parent[x] = find(px);
                return parent[x];
            }

            int merge(int x, int y) {
                int px = find(x);
                int py = find(y);
                if (px == py) {
                    return size[px];
                }
                size[px] += size[py];
                parent[py] = px;
                return size[px];
            }

        }

        class Data {
            int x;
            int y;
            int weight;

            public Data(int x, int y, int weight) {
                this.x = x;
                this.y = y;
                this.weight = weight;
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }
}

