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

public class Practice {
	static class Node {
		int u, v;
		long cost;

		public Node(int u, int v, long cost) {
			this.u = u;
			this.v = v;
			this.cost = cost;
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		while (t-- > 0) {
			String[] s1 = br.readLine().split(" ");

			int n = Integer.valueOf(s1[0]);
			int m = Integer.valueOf(s1[1]);
			long k = Long.valueOf(s1[2]);
			// HashMap[] edg = new HashMap[(int) n + 1];
			// for (int i = 0; i <= n; i++) {
			// edg[i] = new HashMap<Integer, Long>();
			// }
			Node[] arr = new Node[(int) m];
			for (int i = 0; i < m; i++) {
				String[] ss = br.readLine().split(" ");
				int u = Integer.valueOf(ss[0]);
				int v = Integer.valueOf(ss[1]);
				long p = Long.valueOf(ss[2]);
				// edg[u].put(v, p);
				// edg[v].put(u, p);
				arr[i] = new Node(u, v, p);
			}
			long ans = getAns(n, m, k, arr);
			System.out.println(ans);
		}
	}

	private static long getAns(long n, long m, long k, Node[] arr) {
		// TODO Auto-generated method stub
		Arrays.sort(arr, new Comparator<Node>() {

			@Override
			public int compare(Node o1, Node o2) {
				// TODO Auto-generated method stub
				return (o1.cost - o2.cost > 0) ? 1 : -1;
			}
		});
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		// for(int i=0;i<n;i++) {
		// map.put(i+1, 1);
		// }
		ArrayList<HashMap<Integer, Integer>> list = new ArrayList<HashMap<Integer, Integer>>();
		int c = 0;
		int i = 0;
		for (i = 0; i < m; i++) {
			if (arr[i].cost > k) {
				break;
			}
			if (!map.containsKey(arr[i].u) && !map.containsKey(arr[i].v)) {
				map.put(arr[i].u, c);
				map.put(arr[i].v, c);
				list.add(new HashMap<Integer, Integer>());
				c++;
			} else {
				if (map.containsKey(arr[i].u) && map.containsKey(arr[i].v)) {
					if (map.get(arr[i].u) != map.get(arr[i].v)) {
						list.get(map.get(arr[i].u)).put(map.get(arr[i].v), 1);
						list.get(map.get(arr[i].v)).put(map.get(arr[i].u), 1);
					}
				} else if (map.containsKey(arr[i].u)) {
					map.put(arr[i].v, map.get(arr[i].u));
				} else {
					map.put(arr[i].u, map.get(arr[i].v));
				}
			}
		}
		long currAns = 0;
		if (i != 0) {
			currAns = k - arr[i - 1].cost;
		}

		if (i < m) {
			currAns = Math.min(currAns, arr[i].cost - k);
		}
	//	System.out.println(currAns);
	//	System.out.println(list + " " + i + " " + c);
		long ans = 0;
		for (; i < m; i++) {
			if (!map.containsKey(arr[i].u) && !map.containsKey(arr[i].v)) {
				map.put(arr[i].u, c);
				map.put(arr[i].v, c);
				list.add(new HashMap<Integer, Integer>());
				ans += arr[i].cost - k;
				c++;
			} else {
				if (map.containsKey(arr[i].u) && map.containsKey(arr[i].v)) {
					if (map.get(arr[i].u) != map.get(arr[i].v)
							&& !list.get(map.get(arr[i].u)).containsKey(map.get(arr[i].v))) {
						list.get(map.get(arr[i].u)).put(map.get(arr[i].v), 1);
						list.get(map.get(arr[i].v)).put(map.get(arr[i].u), 1);
						ans += arr[i].cost - k;
					}
				} else if (map.containsKey(arr[i].u)) {
					map.put(arr[i].v, map.get(arr[i].u));
					ans += arr[i].cost - k;
				} else {
					map.put(arr[i].u, map.get(arr[i].v));
					ans += arr[i].cost - k;
				}
			}
		}//System.out.println(map);
		//
		return (ans == 0) ? currAns : ans;
	}
}
