//package codeforces;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.*;

/**
 * @author muhossain
 * @since 2020-12-26
 */

public class J1468 {

    public static void main(String[] args) {
        FasterScanner fs = new FasterScanner();
        StringBuilder output = new StringBuilder();

        int t = fs.nextInt();

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

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

            List<Edge> edgeList = new ArrayList<>();

            for (int j = 0; j < m; j++) {
                Edge e = new Edge(fs.nextInt(), fs.nextInt(), fs.nextInt());
                edgeList.add(e);
            }

            List<Edge> edgesInMst = findMST(edgeList, k);

            BigInteger result = BigInteger.ZERO;
            int reduceCount = 0;

            for (Edge e : edgesInMst) {
                if (e.w > k) {
                    result = result.add(BigInteger.valueOf(e.w - k));
                    reduceCount++;
                }
            }

            if (reduceCount == 0) {
                result = result.add(BigInteger.valueOf(k - edgesInMst.get(0).w));
            }

            output.append(result).append("\n");
        }

        System.out.print(output);
    }

    private static List<Edge> findMST(List<Edge> edgeList, int k) {
        List<Edge> edgesInMst = new ArrayList<>();

        edgeList.sort(Comparator.comparingInt(e -> e.w - k));

        Map<Integer, Integer> sizeMap = new HashMap<>();
        Map<Integer, Integer> connectivityMap = new HashMap<>();

        Edge closestEdgeToK = edgeList.get(0);

        for (Edge e : edgeList) {
            connectivityMap.put(e.u, e.u);
            connectivityMap.put(e.v, e.v);

            sizeMap.put(e.u, 1);
            sizeMap.put(e.v, 1);

            if (Math.abs(e.w - k) < Math.abs(closestEdgeToK.w - k)) {
                closestEdgeToK = e;
            }
        }

        edgeList.remove(closestEdgeToK);

        List<Edge> newEdgeList = new ArrayList<>();
        newEdgeList.add(closestEdgeToK);
        newEdgeList.addAll(edgeList);

        for (Edge edge : newEdgeList) {
            int pU = find(edge.u, connectivityMap);
            int pV = find(edge.v, connectivityMap);

            if (pU != pV) {
                if (sizeMap.get(pU) > sizeMap.get(pV)) {
                    connectivityMap.put(pV, pU);

                    sizeMap.put(pU, sizeMap.get(pU) + sizeMap.get(pV));
                } else {
                    connectivityMap.put(pU, pV);

                    sizeMap.put(pV, sizeMap.get(pU) + sizeMap.get(pV));
                }

                edgesInMst.add(edge);
            }
        }

        return edgesInMst;
    }

    private static int find(int u, Map<Integer, Integer> connectivityMap) {
        if (connectivityMap.get(u) == u) {
            return connectivityMap.get(u);
        }

        int pU = find(connectivityMap.get(u), connectivityMap);

        connectivityMap.put(u, pU);

        return pU;
    }

    private static class Edge {
        int u;
        int v;
        int w;

        Edge(int u, int v, int w) {
            this.u = u;
            this.v = v;
            this.w = w;
        }
    }

    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = System.in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
}
