#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e9 + 7;
const long long int INF = 1e9 + 7;
const double PI = acos(-1.0);
template <typename T>
void show(T *a, int size) {
  int i;
  cout << endl << "The array is ";
  for (long long i = 0; i < size; ++i) {
    cout << a[i] << " ";
  }
  cout << endl;
}
template <typename T>
void show_vector(vector<T> v) {
  cout << endl << "The vector is ";
  int i;
  for (long long i = 0; i < v.size(); ++i) {
    cout << v[i] << " ";
  }
  cout << endl;
}
const long long int M = 2e5 + 5;
long long int n, m, k;
vector<pair<long long, long long>> adj[M];
map<long long int, long long int> adj2[M];
long long int visited[M];
long long int group_of[M];
long long int dfs(long long int curr, long long int par, long long int mini,
                  long long int group) {
  long long int rv = 1;
  visited[curr] = 1;
  group_of[curr] = group;
  for (pair<long long, long long> adja : adj[curr]) {
    if (adja.first != par) {
      if (!visited[adja.first] && adja.second <= mini) {
        rv += dfs(adja.first, curr, mini, group);
      }
    }
  }
  return rv;
}
long long int func2() {
  long long int rv = 0;
  visited[0] = 1;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq;
  for (pair<long long, long long> adja : adj2[0]) {
    pq.push({adja.second, adja.first});
  }
  while (!pq.empty()) {
    pair<long long, long long> curr = pq.top();
    pq.pop();
    if (!visited[curr.second]) {
      visited[curr.second] = 1;
      rv += curr.first - k;
      for (pair<long long, long long> adja : adj2[curr.second]) {
        pq.push({adja.second, adja.first});
      }
    }
  }
  return rv;
}
long long int join() {
  for (long long i = 0; i < n + 1; ++i) {
    visited[i] = 0;
  }
  long long int comp = 0;
  for (long long i = 1; i < n + 1; ++i) {
    if (!visited[i]) {
      dfs(i, 0, k, comp);
      comp++;
    }
  }
  visited[0] = 0;
  for (long long i = 1; i < n + 1; ++i) {
    visited[i] = 0;
    for (pair<long long, long long> adja : adj[i]) {
      if (adj2[group_of[i]].count(group_of[adja.first])) {
        adj2[group_of[i]][group_of[adja.first]] =
            min(adj2[group_of[i]][group_of[adja.first]], adja.second);
      } else {
        adj2[group_of[i]][group_of[adja.first]] = adja.second;
      }
    }
  }
  return func2();
}
void solve() {
  cin >> n >> m >> k;
  long long int maxi_z = 0;
  long long int mini_z = 1e18;
  for (long long i = 0; i < n + 1; ++i) {
    adj[i + 1].clear();
    adj2[i + 1].clear();
    visited[i + 1] = 0;
  }
  for (long long i = 0; i < m; ++i) {
    long long int xx, yy, zz;
    cin >> xx >> yy >> zz;
    adj[xx].push_back({yy, zz});
    adj[yy].push_back({xx, zz});
    if (zz <= k)
      maxi_z = max(maxi_z, zz);
    else
      mini_z = min(mini_z, zz);
  }
  if (dfs(1, 0, k, 0) == n) {
    cout << min(k - maxi_z, mini_z - k) << endl;
  } else {
    cout << join() << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
