#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {
  _out << _p.first << " " << _p.second;
  return _out;
}
template <typename T, typename U>
inline istream &operator>>(istream &_in, pair<T, U> &_p) {
  _in >> _p.first >> _p.second;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const vector<T> &_v) {
  if (_v.empty()) return _out;
  _out << _v.front();
  for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline istream &operator>>(istream &_in, vector<T> &_v) {
  for (auto &_i : _v) _in >> _i;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const set<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const multiset<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const map<T, U> &_m) {
  if (_m.empty()) return _out;
  _out << "{\"" << _m.begin()->first << "\", \"" << _m.begin()->second << "\"}";
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", { \"" << _it->first << "\", \"" << _it->second << "\"}";
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) {
  if (_m.empty()) return _out;
  _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", (" << _it->first << ": " << _it->second << ')';
  return _out;
}
template <typename T>
void out(const vector<T> &a, int n) {
  for (int i = 1; i <= n; ++i) {
    cout << a[i] << ' ';
  }
  cout << "\n";
}
template <typename T>
void in(const vector<T> &a, int n) {
  for (int i = 1; i <= n; ++i) cin >> a[i];
}
template <typename T>
void out(const vector<vector<T>> &a, int n, int m) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cout << a[i][j] << ' ';
    }
    cout << '\n';
  }
}
template <typename T>
void in(const vector<vector<T>> &a, int n, int m) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) cin >> a[i][j];
  }
}
struct DSU {
  int n;
  vector<int> par, sz;
  explicit DSU(int n) : n(n) {
    par.resize(n + 1), sz.resize(n + 1);
    for (int i = 0; i <= n; ++i) {
      par[i] = i, sz[i] = 1;
    }
  }
  int getRoot(int i) {
    if (i == par[i]) return i;
    return par[i] = getRoot(par[i]);
  }
  bool merge(int i, int j) {
    int ri = getRoot(i), rj = getRoot(j);
    if (ri == rj) return false;
    if (sz[ri] > sz[rj]) swap(ri, rj);
    if (sz[ri] <= sz[rj]) {
      sz[rj] += sz[ri];
      par[ri] = rj;
    }
    return true;
  }
  vector<int> componentSizes() {
    vector<int> result;
    vector<int> grp_size(n + 1);
    for (int i = 1; i <= n; i++) {
      grp_size[getRoot(i)]++;
    }
    for (int i = 1; i <= n; i++) {
      if (grp_size[i]) result.push_back(grp_size[i]);
    }
    result.erase(
        remove_if(result.begin(), result.end(), [&](int &i) { return i == 0; }),
        result.end());
    return result;
  }
  vector<vector<int>> getComponents() {
    vector<vector<int>> result(n + 1);
    vector<int> grp_size(n + 1), leader(n + 1);
    for (int i = 1; i <= n; i++) {
      leader[i] = getRoot(i);
      grp_size[leader[i]]++;
    }
    for (int i = 1; i <= n; i++) result[i].reserve(grp_size[i]);
    for (int i = 1; i <= n; i++) result[leader[i]].push_back(i);
    result.erase(remove_if(result.begin(), result.end(),
                           [&](const vector<int> &v) { return v.empty(); }),
                 result.end());
    return result;
  }
};
struct Edge {
  long long i, j, w;
};
bool cmp(const Edge &e1, const Edge &e2) { return e1.w < e2.w; }
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  DSU dsu(n);
  vector<Edge> edges;
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    edges.push_back(Edge{a, b, c});
  }
  sort(edges.begin(), edges.end(), cmp);
  long long nearest = INF, a, b;
  for (Edge &e : edges) {
    if ((abs(e.w - k) < abs(k - nearest)) ||
        (abs(e.w - k) == abs(k - nearest) && e.w > nearest))
      nearest = e.w, a = e.i, b = e.j;
  }
  vector<long long> wts;
  int cnt = 1;
  dsu.merge(a, b), wts.emplace_back(nearest);
  long long changes = 0;
  for (Edge &e : edges) {
    if (cnt == n - 1) break;
    if (dsu.merge(e.i, e.j)) {
      cnt++;
      wts.emplace_back(e.w);
    }
  }
  sort(wts.begin(), wts.end());
  if (wts.back() <= k) {
    changes = k - wts.back();
  } else {
    for (auto wt : wts) changes += max(0LL, wt - k);
  }
  cout << changes << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  startTime = clock();
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
