//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> bridgesAboveK = findBridgesAboveK(edgeList, k, n);

            BigInteger result = BigInteger.ZERO;

            if (!bridgesAboveK.isEmpty()) {
                for (Edge e : bridgesAboveK) {
                    result = result.add(BigInteger.valueOf(Math.abs(e.w - k)));

                    e.w = k;
                }
            } else {
                // find closest to k
                Edge closestToK = edgeList.get(0);

                for (Edge e : edgeList) {
                    if (Math.abs(e.w - k) < Math.abs(closestToK.w - k)) {
                        closestToK = e;
                    }
                }

                result = BigInteger.valueOf(Math.abs(k - closestToK.w));

                closestToK.w = 0; // making sure it appears in the mst
            }

            List<Edge> edgesInMst = findMST(edgeList);

            for (Edge mstE : edgesInMst) {
                result = result.add(BigInteger.valueOf(Math.max(0, mstE.w - k)));
            }

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

        System.out.print(output);
    }

    private static List<Edge> findBridgesAboveK(List<Edge> edgeList, int k, int n) {
        List<Edge> bridgeList = new ArrayList<>();

        for (int i = 0; i < edgeList.size(); i++) {
            Edge edge = edgeList.get(i);

            if (edge.w > k) {
                Set<Integer> visited = new HashSet<>();
                List<Edge> filteredEdges = new ArrayList<>();

                filteredEdges.addAll(edgeList.subList(0, i));
                filteredEdges.addAll(edgeList.subList(i + 1, edgeList.size()));

                dfs(filteredEdges, visited);

                if (visited.size() < n) {
                    bridgeList.add(edge);
                }
            }
        }

        return bridgeList;
    }

    private static void dfs(List<Edge> filteredEdges, Set<Integer> visited) {
        Map<Integer, List<Integer>> adjacencies = new HashMap<>();

        for (Edge edge : filteredEdges) {
            adjacencies.putIfAbsent(edge.u, new ArrayList<>());
            adjacencies.putIfAbsent(edge.v, new ArrayList<>());

            adjacencies.get(edge.u).add(edge.v);
            adjacencies.get(edge.v).add(edge.u);
        }

        if (filteredEdges.size() > 0) {
            dfs(filteredEdges.get(0).u, adjacencies, visited);
        }
    }

    private static void dfs(int u, Map<Integer, List<Integer>> adjacencies, Set<Integer> visited) {
        if (visited.contains(u)) {
            return;
        }

        visited.add(u);

        for (int v : adjacencies.get(u)) {
            if (!visited.contains(v)) {
                dfs(v, adjacencies, visited);
            }
        }
    }

//    private static List<Edge> findBridges(List<Edge> edgeList, int k) {
//        Map<Pair, Integer> weightMap = new HashMap<>();
//        Map<Integer, List<Integer>> adjacencies = new HashMap<>();
//
//        for (Edge edge : edgeList) {
//            adjacencies.putIfAbsent(edge.u, new ArrayList<>());
//            adjacencies.putIfAbsent(edge.v, new ArrayList<>());
//
//            adjacencies.get(edge.u).add(edge.v);
//            adjacencies.get(edge.v).add(edge.u);
//
//            weightMap.put(new Pair(edge.u, edge.v), edge.w);
//            weightMap.put(new Pair(edge.v, edge.u), edge.w);
//        }
//
//        Set<Integer> visited = new HashSet<>();
//        Map<Integer, Integer> tin = new HashMap<>(); // time of entry
//        Map<Integer, Integer> low = new HashMap<>();
//
//        List<Pair> bridgeList = new ArrayList<>();
//
//        dfsForBridge(edgeList.get(0).u, -1, adjacencies, visited, tin, low, 0, bridgeList);
//
//        List<Edge> filteredBridgeList = new ArrayList<>();
//
//        bridgeList.forEach(b -> {
//            if (weightMap.getOrDefault(b, 0) > k) {
//                filteredBridgeList.add(new Edge(b.u, b.v, weightMap.get(b)));
//            }
//        });
//
//        return filteredBridgeList;
//    }

//    private static void dfsForBridge(int u, int parent, Map<Integer, List<Integer>> adjacencies,
//                                     Set<Integer> visited, Map<Integer, Integer> tin,
//                                     Map<Integer, Integer> low, int entry, List<Pair> bridgeList) {
//
//        if (visited.contains(u)) {
//            return;
//        }
//
//        visited.add(u);
//
//        low.put(u, entry);
//        tin.put(u, entry);
//
//        for (int v : adjacencies.get(u)) {
//            if (v == parent) {
//                continue;
//            }
//
//            if (visited.contains(v)) {
//                low.put(u, Math.min(low.get(u), tin.get(v)));
//            } else {
//
//                dfsForBridge(v, u, adjacencies, visited, tin, low, entry + 1, bridgeList);
//
//                low.put(u, Math.min(low.get(u), low.get(v)));
//
//                if (low.get(v) > low.get(u)) {
//                    bridgeList.add(new Pair(u, v));
//                }
//            }
//        }
//    }

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

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

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

        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);
        }

        for (Edge edge : edgeList) {
            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 Pair {
        int u, v;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            return (this.u == pair.u && this.v == pair.v) || (this.v == pair.u && this.u == pair.v);
        }

        @Override
        public int hashCode() {
            int result = u;
            result = 31 * result + v;
            return result;
        }
    }

    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;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Edge edge = (Edge) o;

            if (u != edge.u) return false;
            if (v != edge.v) return false;
            return w == edge.w;
        }

        @Override
        public int hashCode() {
            int result = u;
            result = 31 * result + v;
            result = 31 * result + w;
            return result;
        }
    }

    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;
        }
    }
}
