import java.io.*;
import java.util.*;
public class Main {
    public static void main(String args[])
    {
        FastReader input=new FastReader();
        PrintWriter out=new PrintWriter(System.out);
        int T=input.nextInt();
        while(T-->0)
        {
            int n,m,k;
            n=input.nextInt();
            m=input.nextInt();
            k=input.nextInt();
            ArrayList<Pair> list=new ArrayList<>();
            int adj[]=new int[n+1];
            for(int i=0;i<m;i++)
            {
                int u=input.nextInt();
                int v=input.nextInt();
                int s=input.nextInt();
                list.add(new Pair(s,u,v));
                adj[u]++;
                adj[v]++;
            }
            mergeSort(list,0,list.size()-1);
            int in=-1;
            for(int i=m-1;i>=0;i--)
            {
                int u=list.get(i).u;
                int v=list.get(i).v;
                if(adj[u]==1 || adj[v]==1)
                {
                    in=i;
                    break;
                }
                else
                {
                    adj[u]--;
                    adj[v]--;
                }
            }
            int min=Integer.MAX_VALUE;
            int k1=m-(n-1);
            for(int i=in;i<m;i++)
            {
                int r=m-1-i;
                int s=list.get(i).s;
                if(r<=k1)
                {
                    min=Math.min(min,Math.abs(s-k));
                }
            }
            out.println(min);
        }
        out.close();
    }
    public static void mergeSort(ArrayList<Pair> a,int p,int r)
    {
        if(p<r)
        {
            int q=(p+r)/2;
            mergeSort(a,p,q);
            mergeSort(a,q+1,r);
            merge(a,p,q,r);
        }
    }
    public static void merge(ArrayList<Pair> a,int p,int q,int r)
    {
        int n1=q-p+2;
        Pair L[]=new Pair[n1];
        int n2=r-q+1;
        Pair R[]=new Pair[n2];
        for(int i=p;i<=q;i++)
        {
            L[i-p]=a.get(i);
        }
        L[n1-1]=new Pair(Integer.MAX_VALUE,0,0);
        for(int i=q+1;i<=r;i++)
        {
            R[i-q-1]=a.get(i);
        }
        R[n2-1]=new Pair(Integer.MAX_VALUE,0,0);
        int x=0,y=0;
        for(int i=p;i<=r;i++)
        {
            if(L[x].s<=R[y].s)
            {
                a.set(i,L[x]);
                x++;
            }
            else
            {
                a.set(i,R[y]);
                y++;
            }
        }
    }
    static class Pair
    {
        int s,u,v;
        Pair(int s,int u,int v)
        {
            this.s=s;
            this.u=u;
            this.v=v;
        }
    }
    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;
        }
    }
}