#include <bits/stdc++.h>
using namespace std;
int const ca = 105;
int n, m, limit, arr[ca], sz[ca];
void re() {
  for (int i = 0; i <= n; i++) {
    arr[i] = i;
    sz[i] = 1;
  }
}
int parent(int p) {
  if (arr[p] == p) return p;
  return arr[p] = parent(arr[p]);
}
void uni(int a, int b) {
  int aa = parent(a), bb = parent(b);
  if (aa == bb) return;
  if (sz[bb] > sz[aa]) swap(a, b);
  arr[bb] = arr[aa];
  sz[aa] += sz[bb];
}
struct way {
  int x, y;
  long long c;
  way(int x, int y, long long c) : x(x), y(y), c(c) {}
  bool operator<(way const& other) { return c < other.c; }
};
int main() {
  int t;
  cin >> t;
  for (int k = 0; k < t; k++) {
    cin >> n >> m >> limit;
    int ans = 2e9;
    for (int i = 0; i < m; i++) {
      int x, y, c;
      cin >> x >> y >> c;
      ans = min(ans, abs(limit - c));
    }
    cout << ans << endl;
  }
}
