import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.List;
 import java.util.*;
 public class realfast implements Runnable 
  {
    private static final int INF = (int) 1e9;
    long in= 1000000007;
    long fac[]= new long[1000001];
    long inv[]=new long[1000001];
    public  void solve() throws IOException 
    {

        //int t = readInt();
         int t = readInt();

         for(int f =0;f<t;f++)
         {
             int n = readInt();
             int m = readInt();
             int k = readInt();
             ArrayList<edge> le = new ArrayList<edge>();
             ArrayList<edge> big = new ArrayList<edge>();


             for(int i =0;i<m;i++)
             {
                 int u = readInt();
                 int v = readInt();
                 int w = readInt();
                 if(w<=k)
                 {
                    le.add(new edge(u,v,w));
                 }
                 else
                    big.add(new edge(u,v,w));
             }


             int parent1[]=new int[n+1];
             int parent2[]=new int[n+1];


             

             for(int i=1;i<=n;i++)
             {
                 parent1[i]=i;
                 parent2[i]=i;
             }

             Collections.sort(le);
             Collections.sort(big);



             int count =n;
             long ans1= 1000000000;
             for(int i=le.size()-1;i>=0;i--)
             {
                int u = le.get(i).u;
                int v = le.get(i).v;
                int w = le.get(i).w;
                //out.println(w);
                if(union(parent1,u,v))
                {
                     count--;
                     ans1= Math.min(ans1,k-w);
                }
             }


             long ans2 =1000000000;

         //    out.println(ans1);
             if(big.size()>=1)
             {
                     
                   ans2 = big.get(0).w-k;
              
                   union(parent2,big.get(0).u,big.get(0).v);
                   for(int i=0;i<le.size();i++)
                   {
                     int u = le.get(i).u;
                     int v = le.get(i).v;
                     union(parent2,u,v);
                   }


                   for(int i=1;i<big.size();i++)
                   {
                     int u = big.get(i).u;
                     int v = big.get(i).v;
                     int w = big.get(i).w;
                     if(union(parent2,u,v))
                     {
                         ans2= ans2+w-k;
                     }
                   }



             }

             //out.println(ans2);

             if(count==1)
             {
                 ans2=Math.min(ans2,ans1);
             }


             out.println(ans2);





         }
      

    }
    public boolean union(int parent[] , int u , int v)
    {

          int par1 = find(parent,u);
          int par2 = find(parent,v);

          if(par1==par2)
            return false;

          parent[par1]= par2;
          return true;

    }
    public int find(int parent[] , int a )
    {
       if(parent[a]==a)
       {
        return a;

       }
       parent[a]= find(parent,parent[a]);
       return parent[a];
    }
    public boolean intersect(long s1 , long e1 , long s2, long e2)
    {
        if(e2<s1)
            return false;
        if(e1<s2)
            return false;
        return true;
    }
    
    public int value (int seg[], int left , int right ,int index, int l, int r)
    {
            
            if(left>right)
            {
              return -100000000;
            }
            if(right<l||left>r)
                return -100000000;
            if(left>=l&&right<=r)
                return seg[index];
            int mid = left+(right-left)/2;
            int val = value(seg,left,mid,2*index+1,l,r);
            int val2 = value(seg,mid+1,right,2*index+2,l,r);
            return Math.max(val,val2);

    }
   
    public int gcd(int a , int b )
    {
      if(a<b)
      {
        int t =a;
        a=b;
        b=t;
      }
      if(a%b==0)
        return b ;
      return gcd(b,a%b);
    }
    public long pow(long n , long p,long m)
    {
         if(p==0)
            return 1;
        long val = pow(n,p/2,m);;
        val= (val*val)%m;
        if(p%2==0)
            return val;
        else
            return (val*n)%m;
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static void main(String[] args) {
        new Thread(null, new realfast(), "", 128 * (1L << 20)).start();
    }
 
    private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
    private BufferedReader reader;
    private StringTokenizer tokenizer;
    private PrintWriter out;
 
    @Override
    public void run() {
        try {
            if (ONLINE_JUDGE || !new File("input.txt").exists()) {
                reader = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                reader = new BufferedReader(new FileReader("input.txt"));
                out = new PrintWriter("output.txt");
            }
            solve();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                // nothing
            }
            out.close();
        }
    }
 
    private String readString() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }
 
    @SuppressWarnings("unused")
    private int readInt() throws IOException {
        return Integer.parseInt(readString());
    }
 
    @SuppressWarnings("unused")
    private long readLong() throws IOException {
        return Long.parseLong(readString());
    }
 
    @SuppressWarnings("unused")
    private double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }
}
class edge implements Comparable<edge>{
    int u ;
    int v;
    int w;
    edge(int u, int v, int w)
    {
       this.u=u;
       this.v=v;
       this.w=w;
    }
    public int compareTo(edge e)
    {
        return this.w-e.w;
    }
}