#include<bits/stdc++.h>
using namespace std;
#define in(x) int x; cin>>x;
#define sin(s) string s; cin>>s;
#define ll long long 
#define lin(x) ll x; cin>>x;
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define pb push_back
#define re(i,n) for(int i=0; i<n; i++)
#define pe(i,a,b) for(ll i=a; i<=b; i++)
#define rre(i,n) for(int i=n-1; i>-1; i--) 
#define mp make_pair
#define pll pair<ll,ll>
#define pii pair<int,int>
#define endl "\n"
#define pi 3.1415926536 
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fio freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define mod 1000000007
//#define inf LLONG_MAX
#define ninf -INT_MAX
#define inf 1000000
#define MAXN 10000001
 
 
bool compare(const pair<ll,ll> &i, const pair<ll,ll> &j) 
{ 
        if(j.second+j.first==i.first+i.second)
                return j.second>j.first;
        return j.second+j.first<i.first+i.second;
}
 
/*ll sum(ll arr[], int mid, int w, ll n)
{
        ll sum=0;
        re(i,w)
        {
                sum+=min(abs(mid-arr[i]),n-abs(mid-arr[i]));
        }
        return sum;
}*/
/*vi v;
void SieveOfEratosthenes(int n)   
{   
        bool prime[n+1];   
        memset(prime, true, sizeof(prime));   
        for (int p=2; p*p<=n; p++)   
        {    
                if (prime[p] == true)   
                {   
                        for (int i=p*p; i<=n; i += p)   
                        prime[i] = false;   
                }   
        }   
        for (int p=2; p<=n; p++)   
                if (prime[p])   
                {
                        v.pb(p); 
                        //cout<<p<<endl;
                } 
} */
 
#define MOD 1000000007 
ll p(ll a, ll b)
{ 
        long long x=1, y=a; 
        while (b > 0) 
        { 
                if (b%2 == 1) 
                { 
                        x = (x*y) % MOD; 
                } 
                y = (y*y) % MOD; 
                b /= 2; 
        } 
        return x % MOD; 
} 
 
ll inv(ll a){ 
        return p(a, MOD-2); 
} 
 
 
ll gcdExtended(ll a, ll b, ll *x, ll *y)  
{  
    // Base Case  
            if (a == 0)  
            {  
                *x = 0;  
                *y = 1;  
                return b;  
            }  
          
            ll x1, y1; // To store results of recursive call  
            ll gcd = gcdExtended(b%a, a, &x1, &y1);  
          
            // Update x and y using results of  
            // recursive call  
            *x = y1 - (b/a) * x1;  
            *y = x1;  
          
            return gcd;  
} 

string tobin(ll n)
{
        string s = "";
        while(n)
        {
                if(n%2)
                {
                        s="1"+s;
                        n/=2;
                }
                else
                {
                        s="0"+s;
                        n/=2;
                }
        }
        return s;
}

const int INF = 1000000000;

struct Edge 
{
        int w = INF, to = -1;
        bool operator<(Edge const& other) const {
                return make_pair(w, to) < make_pair(other.w, other.to);
        }
};

int prim(vector <vector<Edge>> adj, int n) {
        int total_weight = 0;
        vector<Edge> min_e(n);
        min_e[0].w = 0;
        set<Edge> q;
        q.insert({0, 0});
        vector<bool> selected(n, false);
        for (int i = 0; i < n; ++i) {
                if (q.empty()) 
                {
                        //cout << "No MST!" << endl;
                        return 0;
                }

                int v = q.begin()->to;
                selected[v] = true;
                total_weight += q.begin()->w;
                q.erase(q.begin());

                // if (min_e[v].to != -1)
                // {
                //         //cout << v+1 << " " << min_e[v].to+1 << endl;
                //         p[{v+1,min_e[v].to+1}] = 1;
                //         p[{min_e[v].to+1,v+1}] = 1;
                // }
                for (Edge e : adj[v]) {
                        if (!selected[e.to] && e.w < min_e[e.to].w) 
                        {
                                q.erase({min_e[e.to].w, e.to});
                                min_e[e.to] = {e.w, v};
                                q.insert({e.w, e.to});
                        }
                }
        }
        return total_weight;
}

int bfs(int node, vi adj[], int n)
{
        int cnt=0;
        int vis[n+1]={};
        vis[node]=1;
        queue <int> q;
        q.push(node);
        while(q.size())
        {
                int u = q.front();
                q.pop();
                cnt++;
                re(i,adj[u].size()) 
                { 
                        if(!vis[adj[u][i]]) 
                        { 
                                vis[adj[u][i]]=1;  
                                q.push(adj[u][i]);      
                        } 
                } 
        }
        //cout<<"cnt "<<cnt<<endl;
        return cnt;
}

int main()
{
        FAST
        in(t)
        while(t--)
        {
                in(n)
                in(m)
                in(k)
                vi adj[n+1];
                int edge[m][3];
                int mi = INT_MAX;
                re(i,m)
                {
                        cin>>edge[i][0]>>edge[i][1]>>edge[i][2];
                        mi=min(mi,abs(k-edge[i][2]));
                        if(edge[i][2]<=k)
                        {
                                adj[edge[i][0]].pb(edge[i][1]);
                                adj[edge[i][1]].pb(edge[i][0]);
                        }
                }
                // cout<<bfs(1,adj,n);
                // return 0;
                int cas=1;
                // pe(i,1,n)
                // {
                //         for(auto x: adj[i])
                //                 cout<<x<<" ";
                //         cout<<endl;
                // }
                //cout<<"count.size() "<<count.size()<<endl;
                
                                if(bfs(1,adj,n)!=n)
                                        cas=1;
                                else
                                        cas=2;
                vector<vector<Edge>> ad(n);
                re(i,m)
                {
                        Edge e;
                        e.to = edge[i][1]-1;
                        e.w = max(0,edge[i][2]-k);
                        ad[edge[i][0]-1].pb(e); 
                        e.to = edge[i][0]-1;
                        e.w = max(0,edge[i][2]-k);
                        ad[edge[i][1]-1].pb(e); 
                }
                //sort(ad.begin(),ad.end());
                int ans = prim(ad,n);
                //cout<<cas<<endl;
                if(cas==1)
                {
                        cout<<ans<<endl;
                }
                else
                {      
                        cout<<mi<<endl;
                }
        }
        return 0;
}