#include <bits/stdc++.h>
#define ll long long
const int mod = 1000000007;
const int modx = 998244353;
#define readi(x) scanf("%d", &x)
#define reads(x) scanf("%s", x)
#define readl(x) scanf("%I64d", &x)
#define rep(i, n) for (auto i = 0; i < n; i++)
#define rep1(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a, b) for (auto i = b; i >= a; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define bpc(x) __builtin_popcount(x)
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<double, double>
#define pic pair<int, char>
#define ddouble long double
#define PI 3.1415926535
#define endl "\n"
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define prod(x, y) ((x % mod) * (y % mod)) % mod
#define add(x, y) ((x % mod) + (y % mod)) % mod
#define ub(a, b) upper_bound(all(a), b)
#define lb(a, b) lower_bound(all(a), b)
#define pqs priority_queue<int, vector<int>, greater<int>>
#define pqb priority_queue<int>
#define mii map<int, int>
#define mll map<ll, ll>

using namespace std;
void inputfromtext()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//using namespace __gnu_pbds;
/*ll power(ll x, ll y)
{
	ll r = 1;
	x = x % mod;
	while (y > 0)
	{
		if (y & 1)
			r = (r*x) % mod;

		y = y>>1;
		x = (x*x) % mod;
	}
	return r;
}*/
/*class segtree
{
public:
	ll len;
	vll tree, arr;
	segtree(ll n, vll &a)
	{
		len = n;
		tree.resize(4 * n + 5);
		arr = a;
		build(0, len - 1, 1);
	}
	void build(ll low, ll high, ll node)
	{
		if (low > high)
			return;
		if (low == high)
		{
			tree[node] = arr[low];
			return;
		}
		ll mid = low + (high - low) / 2;
		build(low, mid, 2 * node);
		build(mid + 1, high, 2 * node + 1);
		tree[node] = add(tree[2 * node], tree[2 * node + 1]);
		return;
	}
	void treeupdate(ll low, ll high, ll node, ll val, ll idx)
	{
		if (low == high)
		{
			tree[node] = val;
			return;
		}
		ll mid = low + (high - low) / 2;
		if (idx <= mid && low <= idx)
			treeupdate(low, mid, 2 * node, val, idx);
		else
			treeupdate(mid + 1, high, 2 * node + 1, val, idx);
		tree[node] = add(tree[2 * node], tree[2 * node + 1]);
		return;
	}
	ll treequery(int node, int low, int high, int l, int r)
	{
		if (low > r || high < l || low > high)
			return 0;
		if (low >= l && high <= r)
			return tree[node];
		int mid = low + (high - low) / 2;
		ll p1 = treequery(2 * node, low, mid, l, r);
		ll p2 = treequery(2 * node + 1, mid + 1, high, l, r);
		return p1 + p2;
	}
	ll query(ll l, ll r)
	{
		return treequery(1, 0, len - 1, l, r);
	}
	void update(ll value, ll index)
	{
		treeupdate(0, len - 1, 1, value, index);
		return;
	}
};*/
vll parent, r;
ll find(ll s)
{
	while (parent[s] != s)
		s = parent[s];
	return s;
}
void un(ll x, ll y)
{
	x = find(x);
	y = find(y);
	if (r[x] > r[y])
		parent[y] = x;
	else if (r[y] > r[x])
		parent[x] = y;
	else
	{
		parent[y] = x;
		r[x]++;
	}
	return;
}
void thunder()
{
	int n, m, k;
	cin >> n >> m >> k;
	vector<vector<ll>> g;
	parent.assign(n + 1, -1);
	rep(i, parent.size()) parent[i] = i;
	r.assign(n + 1, 0);
	ll fin = 2e9;
	rep(i, m)
	{
		ll u, v, s, w, flg = 0;
		cin >> u >> v >> s;
		u--;
		v--;
		//		s = abs(k - s);

		w = max(0ll, s - k);
		flg = s >= k;
		fin = min(fin, abs(s - k));
		g.pb({w, u, v, flg});
	}
	sort(all(g));
	ll ans = 0, f = 0;
	for (auto a : g)
	{
		ll x = find(a[1]), y = find(a[2]), w = a[0], flg = a[3];
		if (x == y)
			continue;
		ans += w;
		f |= flg;
		//cout << a[1] + 1 << " " << a[2] + 1 << " " << w << endl;
		un(x, y);
	}
	if (!f)
		ans += fin;
	cout << ans << endl;
	return;
}
int main()
{
	inputfromtext();
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll tt = 1;
	cin >> tt;
	for (ll x = 1; x <= tt; x++)
	{
		//cout << "Case #" << x << ": ";
		thunder();
	}
	return 0;
}
