#include<bits/stdc++.h>
using namespace std;
#define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  all(v)     v.begin(),v.end()
#define  rall(v)    v.rbegin(),v.rend()
#define MEM(a,x)    memset(a,x,sizeof(a))
#define pi          acos(-1.0)
#define  ll     long long
#define  pb     push_back
#define  mp     make_pair
#define  pii    pair< int,int >
#define  pll    pair< ll,ll >
#define  vii    vector< int >
#define  vll    vector< ll >
#define  vpi    vector< pii >
#define  MX     200005
#define  EPS    1e-12
#define  ss     second
#define  ff     first
int cases=1;
#ifdef ppqq
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<"  ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
///******************************************START******************************************
struct info
{
    ll u,v;
    ll w;

};
vector<info> graph;
ll par[MX],cnt,rnk[MX];
bool cmp(const info&a,const info&b)
{
    if(a.w<b.w) return true;
    return false;
}
ll find_set(ll v)
{
    if(v==par[v]) return v;
    return par[v]=find_set(par[v]);
}
void mrg(ll a,ll b)
{
    if(rnk[a]<=rnk[b])
    {
        par[a]=b;
        rnk[b]++;
    }
    else
    {
        par[b]=a;
        rnk[a]++;
    }
}
ll MST(ll n)
{
    ll sum=0;
    for(ll i=0;i<graph.size();i++)
    {
        ll a=find_set(graph[i].u);
        ll b=find_set(graph[i].v);
        if(a!=b)
        {
            mrg(a,b);
            cnt++;
            sum+=graph[i].w;
        }
        if(cnt==n-1) break;
    }
    return sum;
}
bool tree(ll n)
{
    for(ll i=1;i<n;i++)
    {
        if(find_set(i)!=find_set(i+1)) return false;
    }
    return true;
}
void CLR(ll n)
{
    graph.clear();
    cnt=0;
    for(ll i=1;i<=n;i++) par[i]=0,rnk[i]=0;
}
int main()
{
    FIO;
    int t;
    cin>>t;
    while(t--) {
        ll n,m;
        ll k,mini=LLONG_MAX;
        cin>>n>>m>>k;
        vector<pair<ll,pll>> kom,shb;
        for(ll i=0;i<m;i++)
        {
            ll u,v;
            ll w;
            cin>>u>>v>>w;
            info temp;
            temp.u=u;
            temp.v=v;
            temp.w=w;
            if(w<=k) graph.pb(temp);
            shb.pb({w,{u,v}});
        }
        sort(all(shb));
        sort(all(graph),cmp);
        for (ll i = 1; i <= n; i++) par[i] = i;
        MST(n);

        if(tree(n))
        {
            ll ans=LLONG_MAX;
            for(auto it: shb) ans=min(ans,abs(it.ff-k));
            cout<<ans<<"\n";
        }
        else
        {
            for (ll i = 1; i <= n; i++) par[i] = i;
            for(ll i=0;i<graph.size();i++)
                graph[i].w=0;
            cnt=0;
            for(auto it: shb)
            {
                if(it.ff>k){
                    info temp;
                    temp.u=it.ss.ff;temp.v=it.ss.ss;
                    temp.w=it.ff-k;
                    graph.pb(temp);
                }
            }
            sort(all(graph),cmp);
            cout<<MST(n)<<"\n";
        }

        CLR(n+2);
    }
}
