#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include <unordered_map>
#include<climits>
#include<set>
#include <iomanip>
#include<numeric>
#include<queue>
#include<stack>
#include <map>

using namespace std;

#define ff              first
#define ss              second
#define ll              long long
#define ld              long double
#define pb              push_back
#define mp              make_pair
#define mt              make_tuple
#define pii             pair<ll,ll>
#define vi              vector<ll>
#define mii             map<ll,ll>
#define pqb             priority_queue<ll>
#define pqs             priority_queue<ll,vi,greater<ll> >
#define mod             1000000007
#define inf             1e18
#define all(v)          (v).begin(),(v).end()
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl            "\n"
class edge{
    public : 
    ll a , b , c, w ;

};
bool comp(edge a1 , edge a2){

    if(a1.w == a2.w){
        if(a1.a == a2.a){
            return a1.b < a2.b ; 
        }
        return a1.a < a2.a ; 
    }
    return a1.w < a2.w ; 

}
class mst{

    public :
    vi par ;
    vi sz ;

    mst(ll n){
        for(int i = 0 ; i <= n ;i++){
            par.pb(i) ; 
            sz.pb(i) ; 
        }
    }
    
    ll getp(ll p){
        if(p == par[p]){
            return p ;
        } 
        return getp(par[p]) ; 
    }
    
    bool checkp(ll a , ll b){
        return getp(a) != getp(b) ; 
    }

    void add(ll a, ll b){

        ll a1 = getp(a) ; 
        ll a2 = getp(b) ;

        if(sz[a1] > sz[a2]){
            par[a2] = a1 ;
            sz[a1] += sz[a2] ; 
        }
        else{
            par[a1] = a2 ; 
            sz[a2] +=sz[a1] ; 
        }
    }

};
int main(){

    FIO ;
    int t ;
    t =1  ;
    cin >> t ;

    while(t--){
      ll n,m,k ;
      cin >> n >> m >> k; 
      vector<edge> v ; 
      ll mn,mx ; 
      mx = 0 ; 
      mn = inf ; 
      for(int i = 0 ; i < m ; i++){
          ll x , y, z ; 
          cin >> x >> y >> z ; 
          edge e ; 
          e.a = x ; 
          e.b = y ; 
          e.w = z ; 
          if(z > k){
              e.c = z - k ; 
              mn = min(mn,z) ; 
          }
          else{
              mx = max(mx,z) ; 
              e.c = 0 ; 
          }
          v.pb(e) ; 
      }
      sort(all(v),comp)  ; 
      
      mst mt(n) ; 
      ll ans = 0 ; 
      ll cnt = 0 ; 
      for(int i = 0 ; i < v.size() ; i++){

          if(mt.checkp(v[i].a,v[i].b)){
              cnt++ ; 
              mt.add(v[i].a,v[i].b) ; 
              ans += v[i].c ; 
             
          }
          if(cnt == n - 1){
              break ; 
          }
      }
      
  
      if(ans == 0){
          ans = min(k-mx,mn-k) ;
      }
      cout << ans << endl ; 





    }
    return 0 ;

}
