import java.io.*;

import java.util.*;
/*


 */









public class C {
    static FastReader sc=null;
    
    public static void main(String[] args) {
		sc=new FastReader();
		PrintWriter out=new PrintWriter(System.out);
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt();
			DU du=new DU(n);
			ArrayList<Edge> edges=new ArrayList<>();
			for(int i=0;i<m;i++) {
				int v=sc.nextInt()-1,w=sc.nextInt()-1;
				long c=sc.nextLong();
				edges.add(new Edge(v,w,c,k));
			}
			Collections.sort(edges);
			long ans=edges.get(0).diff;
			du.unify(edges.get(0).v, edges.get(0).w);
			Collections.sort(edges,new Comp());
			for(Edge e:edges) {
	    		if(du.connected(e.v, e.w))continue;
	    		ans+=Math.max(0, e.cost-k);
	    		du.unify(e.v, e.w);
	    	}
			out.println(ans);
			
		}
		out.close();
	
	}
    static class Comp implements Comparator<Edge>{

		@Override
		public int compare(Edge o1, Edge o2) {
			return Long.compare(o1.cost, o2.cost);
		}
		
    	
    }
    static class Edge implements Comparable<Edge>{
    	int v,w;
    	long cost=0,k=0;
    	long diff=0;
    	Edge(int v,int w,long cost,long k){
    		this.v=v;
    		this.w=w;
    		this.cost=cost;
    		this.k=k;
    		diff=Math.abs(cost-k);
    	}
    	@Override
    	public int compareTo(Edge o) {
    		return Long.compare(this.diff,o.diff);
    	}
    }
    static class DU{
		 int id[];
		 int size;
		 int comp;
		 int sz[];
		 Map<Integer,ArrayList<Integer>> map=new HashMap<>();
		
		 DU(int size){
			this.size=size;
			id=new int[size];
			sz=new int[size];
			for(int i=0;i<size;i++) {
				id[i]=i;
				sz[i]=1;
			}
			comp=size;
				
		}
		public int find(int p) {
			int root=p;
			
			while(id[root]!=root)
				root=id[root];
			
			while(p!=root) {
				
				int next=id[p];
				id[p]=root;
				p=next;
			}
			return root;
			
		}
		public boolean connected(int p,int q) {
			int r1=find(p),r2=find(q);
			return id[r1]==id[r2];
		}
		public int compsize(int p) {
			return sz[find(p)];
		}
		public void unify(int p,int q) {
			int r1=find(p),r2=find(q);
			
			
			if(r1==r2)return;
			
			if(sz[r1]<sz[r2]) {
				sz[r2]+=sz[r1];
				id[r1]=id[r2];
			}
			else {
				sz[r1]+=sz[r2];
				id[r2]=id[r1];
			}
			
			comp--;	
		}	
	}

 
    
    
  
   
    
    
   
 
   
    
    static void ruffleSort(int a[]) {
        ArrayList<Integer> al=new ArrayList<>();
        for(int i:a)al.add(i);
        Collections.sort(al);
        for(int i=0;i<a.length;i++)a[i]=al.get(i);
    }
    static void ruffleSort(long a[]) {
        ArrayList<Long> al=new ArrayList<>();
        for(long i:a)al.add(i);
        Collections.sort(al);
        for(int i=0;i<a.length;i++)a[i]=al.get(i);
    }
    
    static int[] reverse(int a[]) {
        ArrayList<Integer> al=new ArrayList<>();
        for(int i:a)al.add(i);
        Collections.sort(al,Collections.reverseOrder());
        for(int i=0;i<a.length;i++)a[i]=al.get(i);
        return a;
    }
 
    static int gcd(int a,int b) {
		if(b==0)return a;
		else return gcd(b,a%b);
	}  
    static long gcd(long a,long b) {
		if(b==0)return a;
		else return gcd(b,a%b);
	}  
        
 
    
    
    
    static void print(long a[]) {
        for(long e:a) {
            System.out.print(e+" ");
        }
        System.out.println();
    }
    static void print(char a[]) {
        for(char e:a) {
            System.out.print(e);
        }
        System.out.println();
    }
    
    
    static void print(int a[]) {
        for(int e:a) {
            System.out.print(e+" ");
        }
        System.out.println();
    }
    
    
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
        int[] readArray(int n) {
            int a[]=new int [n];
            for(int i=0;i<n;i++) {
                a[i]=sc.nextInt();
            }
            return a;
        }
    } 
}
