/*==========================================================================*/
/*
*		AUTHOR:    RonWonWon    
*		CREATED:   27.04.2021 02:46:37
*		EMAIL:     rachitpts.2454@gmail.com                                  
*/
/*==========================================================================*/
import java.io.*;
import java.util.*;

public class F {
	
	public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt(), tt = 0;
        while(t-->0) { 
        	int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
        	parent = new int[n+1]; size = new int[n+1];
        	for(int i=1;i<=n;i++) make(i);
        	Pair e[] = new Pair[m];
        	for(int i=0;i<m;i++) e[i] = new Pair(in.nextInt(),in.nextInt(),in.nextInt());
        	Arrays.sort(e);
        	int ans = 0;
        	for(int i=0;i<m;i++){
        		int x = e[i].x, y = e[i].y;
        		if(union(x,y)){
        			ans += Math.max(0,e[i].z-k);
        		}
        	}
        	if(ans==0){        
        		ans = Integer.MAX_VALUE;
        		for(int i=0;i<m;i++){
        			ans = Math.min(ans,Math.abs(k-e[i].z));
        		}
        	}
        	out.println(ans);
    		//tt++; out.println("Case #"+tt+": "+ans);
        }
        out.flush();
	}

	static int parent[], size[];

	static void make(int v){
		parent[v] = v; size[v] = 1;
	}

	static int find(int v){
		if(parent[v]==v) return v;
		else return parent[v] = find(parent[v]);
	}

	static boolean union(int u, int v){
		int a = find(u), b = find(v);
		if(a==b) return false;
		if(size[a]<size[b]){
			a = a+b; b = a-b; a = a-b;
		}
		parent[b] = a;
		size[a] += size[b];
		return true;
	}
	
	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		
		String next() {
			while(!st.hasMoreTokens())
				try { st = new StringTokenizer(br.readLine()); }
				catch(IOException e) {}
			return st.nextToken();
		}
		
		String nextLine(){
			try{ return br.readLine(); } 
			catch(IOException e) { } return "";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		int[] readArray(int n) {
			int a[] = new int[n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
	}

	static final Random random = new Random();

	static void ruffleSort(int[] a){
		int n = a.length;
		for(int i=0;i<n;i++){
			int j = random.nextInt(n), temp = a[j];
			a[j] = a[i]; a[i] = temp;
		}
		Arrays.sort(a); 	
	}

	static class Pair implements Comparable<Pair> {
		int x, y, z;
		Pair(int a, int b, int c){ x = a; y = b; z = c; }		
		@Override
		public int compareTo(Pair o) {
			if(o.z - this.z>0)
				return -1;
			else if(o.z - this.z<0)
				return 1;
			else 
				return 0;
		}

	}
	
	static int lcm(int a, int b) {
		return (a*b)/gcd(a,b);
	}
	
	static int gcd(int a, int b) {
		if(b==0) return a;
		else return gcd(b,a%b);
	}

	//modulo
	static final int mod=1_000_000_007;
	
	static long mul(long a, long b) {
		return a*b%mod;
	}
	
	static long fastPow(long base, long exp) {
		if (exp==0) return 1;
		long half=fastPow(base, exp/2);
		if (exp%2==0) return mul(half, half);
		return mul(half, mul(half, base));
	}
	
	static long modInv(long x) {
		return fastPow(x, mod-2);
	}
	
	static long fact(int x) {
		long ans=1;
		for (int i=2; i<=x; i++) ans=mul(ans, i);
		return ans;
	}
	
	static long nCk(int n, int k) {
		return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n-k))));
	}
	
}
