#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ull unsigned long long
#define mod 1000000007
#define pb push_back
#define ll long long
const ll INF = 1e18;
ll n, m, k;
ll d(ll w, const vector<vector<pair<ll, pair<ll, ll>>>>& adj) {
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
vector<ll> dis(n + 1, INF);
dis[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
ll cost = pq.top().first;
ll node = pq.top().second;
pq.pop();
if (cost > dis[node]) continue;
for (auto& edge : adj[node]) {
ll neigh = edge.first;
ll c = edge.second.first;
ll weight = edge.second.second;
if (weight > w) continue;
if (dis[neigh] > cost + c) {
dis[neigh] = cost + c;
pq.push({dis[neigh], neigh});
}
}
}
return dis[n];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> m >> k;
vector<vector<pair<ll, pair<ll, ll>>>> adj(n + 1);
for (int i = 0; i < m; i++) {
ll from, to, c, w;
cin >> from >> to >> c >> w;
adj[from].pb({to, {c, w}});
adj[to].pb({from, {c, w}});
}
if (n == 1) {
cout << "0" << endl;
continue;
}
if (m == 0) {
cout << "-1" << endl;
continue;
}
ll l = 0, r = 2e9, anss = -1;
while (l <= r) {
ll mid = l + (r - l) / 2;
ll cost = d(mid, adj);
if (cost < k) {
anss = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << anss << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgdWxsIHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIG1vZCAxMDAwMDAwMDA3CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbGwgbG9uZyBsb25nCmNvbnN0IGxsIElORiA9IDFlMTg7CmxsIG4sIG0sIGs7CmxsIGQobGwgdywgY29uc3Qg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AgICBjaW4gPj4gbiA+PiBtID4+IGs7CiAgICAgICAgdmVjdG9yPHZlY3RvcjxwYWlyPGxsLCBwYWlyPGxsLCBsbD4+Pj4gYWRqKG4gKyAxKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG07IGkrKykgewogICAgICAgICAgICBsbCBmcm9tLCB0bywgYywgdzsKICAgICAgICAgICAgY2luID4+IGZyb20gPj4gdG8gPj4gYyA+PiB3OwogICAgICAgICAgICBhZGpbZnJvbV0ucGIoe3RvLCB7Yywgd319KTsKICAgICAgICAgICAgYWRqW3RvXS5wYih7ZnJvbSwge2MsIHd9fSk7CiAgICAgICAgfQogICAgICAgIGlmIChuID09IDEpIHsKICAgICAgICAgICAgY291dCA8PCAiMCIgPDwgZW5kbDsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGlmIChtID09IDApIHsKICAgICAgICAgICAgY291dCA8PCAiLTEiIDw8IGVuZGw7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KICAgICAgICBsbCBsID0gMCwgciA9IDJlOSwgYW5zcyA9IC0xOwogICAgICAgIHdoaWxlIChsIDw9IHIpIHsKICAgICAgICAgICAgbGwgbWlkID0gbCArIChyIC0gbCkgLyAyOwogICAgICAgICAgICBsbCBjb3N0ID0gZChtaWQsIGFkaik7CiAgICAgICAgICAgIGlmIChjb3N0IDwgaykgewogICAgICAgICAgICAgICAgYW5zcyA9IG1pZDsKICAgICAgICAgICAgICAgIHIgPSBtaWQgLSAxOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgbCA9IG1pZCArIDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBhbnNzIDw8IGVuZGw7CiAgICB9Cn0K