#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<pi> vpi;
typedef vector<bool> vbool;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define forall(it,s) for(auto it = s.begin(); it != s.end(); ++it)
#define F0(i,n) for(int i = 0; i < n; i++)
#define F1(i,n) for(int i = 1; i <= n; i++)
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define per(i,a,b) for(int i = b-1; i >= a; i--)
#define FIN ios::sync_with_stdio(false);cin.tie(0);
#define db(x) cout<<#x<<" = "<<x<<"\n"
#define all(v) v.begin(),v.end()
#define modulo(a,b) ((a%b)+b)%b
#define F first
#define S second
#define INF 1000000010
#define INFLL 1000000000000000000
#define eps 0.000000001
#define mod 1000000007
template<class C> void mini(C&a, C b){a=min(a,b);}
template<class C> void maxi(C&a, C b){a=max(a,b);}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

//=======================//


struct Edge {
    int u, v;
    ll weight;
    bool operator<(Edge const& other) {
        return weight < other.weight;
    }
};


ll n,m,k;
vector<Edge> adj;
vector<vector<int>> menorK;
vector<bool> visited;

void dfs(int node) {
	if(visited[node]) return;
	visited[node] = true;
	for(auto v : menorK[node]) {
		dfs(v);
	}
}
vector<int> parent, _rank;

void make_set(int v) {
    parent[v] = v;
    _rank[v] = 0;
}

int find_set(int v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (_rank[a] < _rank[b])
            swap(a, b);
        parent[b] = a;
        if (_rank[a] == _rank[b])
            _rank[a]++;
    }
}

ll kruskal() {

	ll cost = 0;
	parent.resize(n);
	_rank.resize(n);
	for (int i = 0; i < n; i++)
	    make_set(i);

	sort(adj.begin(), adj.end());

	for (Edge e : adj) {
	    if (find_set(e.u) != find_set(e.v)) {
	        cost += e.weight;
	        union_sets(e.u, e.v);
	    }
	}
	return cost;
}

void solve() {
	cin >> n >> m >> k;
	adj = vector<Edge>(n);
	menorK = vector<vector<int>>(n);
	ll abajo=-INF, arriba=INF;
	for(int i = 0; i < m; i++) {
		ll a,b,p; cin >> a >> b >> p;
		a--; b--;
		adj.push_back({a,b,max(p-k, 0LL)});
		/*adj[a].push_back({max(p-k, 0),b});
		adj[b].push_back({max(p-k, 0),a});*/
		if(p <= k) {
			menorK[a].push_back(b);
			menorK[b].push_back(a);
		}
		if(p <= k && p > abajo) {
			abajo = p;
		}
		if(p >= k && p < arriba) {
			arriba = p;
		}
	}
	visited = vector<bool>(n, false);
	dfs(0);
	bool listo = true;
	for(int i = 0; i < n; i++) {
		if(!visited[i]) listo=false;
	}
	if(listo) {
		cout << min(k-abajo, arriba-k) << "\n";
		return;
	}
	else {
		cout << kruskal() << "\n";
		return;
	}

}



int main () {
	FIN
	int t; cin >> t; while(t--) solve();
	return 0;
}
