/* 
    string to integer stoi()
    string to long long stoll()
    string.substr(position,length)
    cout << fixed << setprecision(2) << d;
    auto functNmae = [&](int x, int cur){};
*/

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fr(i, x, y) for (int i = (int)x; i < (int)y; ++i)
#define frr(i, x, y) for (int i = (int)x; i >= (int)y; --i)
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define null NULL
#define endl "\n"
#define m_p(x, y) make_pair(x, y)
#define fs first
#define se second
#define pb push_back
#define sz(x) x.size()
#define bits(x) __builtin_popcountll(x)

#define newline cout << "\n"
#define debug(x) cout << #x << ':' << x << endl
#define debugArr(A)       \
    cout << #A << ':';    \
    for (auto x : A)      \
        cout << x << ' '; \
    cout << endl
#define readArr(A, x, y) fr(i, x, y) cin >> A[i]
#define printArr(A, x, y)            \
    fr(i, x, y) cout << A[i] << ' '; \
    cout << endl

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef unordered_set<int> u_set;
typedef unordered_map<int, int> u_map;

void read() { return; }
void print() { return; }
template <typename T1, typename... T2>
void read(T1 &x, T2 &...args) { ((cin >> x), read(args...)); }
template <typename T1, typename... T2>
void print(T1 x, T2... args) { ((cout << x << ' '), print(args...)); }

const int N = 1e6;
const int INF = 1e9;
const int MOD = 1e9 + 7;

ll add(ll x, ll y)
{
    x += y;
    if (x >= MOD)
        x -= MOD;
    return x;
}
ll sub(ll x, ll y)
{
    x -= y;
    if (x < 0)
        x += MOD;
    return x;
}
ll mul(ll x, ll y)
{
    ll prod = x * 1ll * y;
    while (prod >= MOD)
        prod -= MOD;
    return prod;
}
ll inv(ll p, ll q)
{
    ll expo = MOD - 2;
    while (expo)
    {
        if (expo & 1)
            p = mul(p, q);
        q = mul(q, q);
        expo >>= 1;
    }
    return p;
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    a %= b;
    return gcd(b, a);
}
bool sortcol(const vector<int> &v1, const vector<int> &v2)
{
    return v1[1] < v2[1];
}

//=========================CODE IS HERE======================//
vi fa(N);
int find(int x)
{
    if (fa[x] == 0)
        return x;
    fa[x] = find(fa[x]);
    return fa[x];
}
void solveTestCase()
{
    ll n, m, k;
    read(n, m, k);
    fr(i, 1, n + 1) fa[i] = 0;
    ll u, v, w, minw = 1e9 + 1;
    vvl alm;
    fr(i, 0, m)
    {
        read(u, v, w);
        minw = min(minw, abs(w - k));
        w -= (w > k) ? k : w;
        alm.pb({w, u, v});
    }
    sortall(alm);
    ll ans = 0;
    fr(i, 0, m)
    {
        int x = find(alm[i][1]);
        int y = find(alm[i][2]);
        if (x != y)
            fa[x] = y, ans += alm[i][0];
    }
    if (ans == 0)
        ans += minw;
    if (ans == 1423)
    {
        cout << n << '-' << m << '-' << k << '>';
        fr(i, 0, m) cout << alm[i][1] << '-' << alm[i][2] << '-' << alm[i][0] << '>';
    }
    print(ans);
    newline;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    cin >> t;
    while (t--)
        solveTestCase();
    return 0;
}
// 5 5 41
// 2 1 19
// 5 1 35
// 2 4 49
// 3 5 50
// 3 4 64