import java.io.*;
import java.util.*;

public class Codeforces
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int t=Integer.parseInt(bu.readLine());
        while(t-->0)
        {
            String s[]=bu.readLine().split(" ");
            int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]),k=Integer.parseInt(s[2]);
            int i;
            ArrayList<Integer> al=new ArrayList<>();
            for(i=0;i<m;i++)
            {
                s=bu.readLine().split(" ");
                int u=Integer.parseInt(s[0])-1,v=Integer.parseInt(s[1])-1,w=Integer.parseInt(s[2]);
                w=Math.abs(k-w);
                al.add(u); al.add(v); al.add(w);
            }
            Graph g=new Graph(n,al);
            int ans=g.KruskalMST();
            sb.append(ans+"\n");
        }
        System.out.print(sb);
    }

    static class Graph
    {
        class Edge implements Comparable<Edge>
        {
            int src, dest, weight;
            public int compareTo(Edge compareEdge)
            {
                return this.weight - compareEdge.weight;
            }
        }

        class Subset
        {
            int parent, rank;
        }

        int V,E;
        Edge edge[];
        Graph(int v,ArrayList<Integer> e)
        {
            V=v;
            E=e.size()/3;
            edge=new Edge[E];
            int i,ptr=0;
            for(i=0;i<E;++i)
            {
                edge[i]=new Edge();
                edge[i].src=e.get(ptr++);
                edge[i].dest=e.get(ptr++);
                edge[i].weight=e.get(ptr++);
            }
        }

        int find(Subset subsets[],int i)
        {
            if(subsets[i].parent!=i)
            subsets[i].parent=find(subsets,subsets[i].parent);
            return subsets[i].parent;
        }

        void Union(Subset subsets[], int x, int y)
        {
            int xroot=find(subsets, x);
            int yroot=find(subsets, y);

            if(subsets[xroot].rank<subsets[yroot].rank) subsets[xroot].parent=yroot;
            else if(subsets[xroot].rank>subsets[yroot].rank) subsets[yroot].parent=xroot;
            else
            {
                subsets[yroot].parent=xroot;
                subsets[xroot].rank++;
            }
        }

        int KruskalMST()
        {
            int e=0,i;
            Arrays.sort(edge);

            Subset subsets[]=new Subset[V];
            for(i=0;i<V;i++)
            {
                subsets[i]=new Subset();
                subsets[i].parent=i;
                subsets[i].rank=0;
            }

            i=0; int cost=Integer.MAX_VALUE;
            while(e<V-1)
            {
                Edge next_edge=new Edge();
                next_edge=edge[i++];
                int x=find(subsets,next_edge.src),y=find(subsets,next_edge.dest);
                if(x!=y)
                {
                    cost=Math.min(cost,next_edge.weight);
                    Union(subsets,x,y);
                    e++;
                }
            }
            return cost;
        }
    }
}
