#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 int N =1e5+5;
ll n,m,k;
int d(ll w,vector<pair<pair<ll,ll>,ll>>adj[])
{
priority_queue<pair<ll,ll>>pq;
vector<ll>dis(N,1e18);
pq.push({0,1});
dis[1]=0;
while(!pq.empty())
{
int node= pq.top().second;
int cost= -pq.top().first;
pq.pop();
if(dis[node]<cost) continue;
if(n==node) break;
for(auto i: adj[node])
{
if(i.second> w) continue;
ll d= i.first.second,ch= i.first.first;
if(dis[ch]>d+cost)
{
dis[ch]=d+cost;
pq.push({-dis[ch],ch});
}
}
}
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<pair<pair<ll,ll>,ll>>adj[N];
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 r=1e10,l=1,anss=-1;
while(l<=r)
{
ll m= (l+r)/2;
ll ans = d(m,adj);
if(ans<k)
{
anss=m;
r=m-1;
}
else
{
l=m+1;
}
}
cout << anss<< endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgdWxsIHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIG1vZCAxMDAwMDAwMDA3CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbGwgbG9uZyBsb25nCmNvbnN0IGludCBOID0xZTUrNTsKbGwgbixtLGs7CmludCBkKGxsIHcsdmVjdG9yPHBhaXI8cGFpcjxsbCxsbD4sbGw+PmFkaltdKQp7CiAgICBwcmlvcml0eV9xdWV1ZTxwYWlyPGxsLGxsPj5wcTsKICAgIHZlY3RvcjxsbD5kaXMoTiwxZTE4KTsKICAgIHBxLnB1c2goezAsMX0pOwogICAgZGlzWzFdPTA7CiAgICB3aGlsZSghcHEuZW1wdHkoKSkKICAgIHsKICAgICAgICBpbnQgbm9kZT0gcHEudG9wKCkuc2Vjb25kOwogICAgICAgIGludCBjb3N0PSAtcHEudG9wKCkuZmlyc3Q7CiAgICAgICAgcHEucG9wKCk7CiAgICAgICAgaWYoZGlzW25vZGVdPGNvc3QpIGNvbnRpbnVlOwogICAgICAgIGlmKG49PW5vZGUpIGJyZWFrOwogICAgICAgIGZvcihhdXRvIGk6IGFkaltub2RlXSkKICAgICAgICB7CiAgICAgICAgICAgIGlmKGkuc2Vjb25kPiB3KSBjb250aW51ZTsKICAgICAgICAgICAgbGwgZD0gaS5maXJzdC5zZWNvbmQsY2g9IGkuZmlyc3QuZmlyc3Q7CiAgICAgICAgICAgIGlmKGRpc1tjaF0+ZCtjb3N0KQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBkaXNbY2hdPWQrY29zdDsKICAgICAgICAgICAgICAgIHBxLnB1c2goey1kaXNbY2hdLGNofSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZGlzW25dOwp9CmludCBtYWluKCkKewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOwogICAgY291dC50aWUoMCk7CiAgICBpbnQgdDsKICAgIGNpbiA+PnQ7CiAgICB3aGlsZSh0LS0pCiAgICB7CiAgICAgICAgY2luID4+IG4gPj4gbSA+Pms7CiAgICAgICAgdmVjdG9yPHBhaXI8cGFpcjxsbCxsbD4sbGw+PmFkaltOXTsKICAgICAgICBmb3IoaW50IGk9MDtpPG07aSsrKQogICAgICAgIHsKICAgICAgICAgICAgbGwgZnJvbSx0byxjLHc7CiAgICAgICAgICAgIGNpbiA+PiBmcm9tPj50bz4+Yz4+dzsKICAgICAgICAgICAgYWRqW2Zyb21dLnBiKHt7dG8sY30sd30pOwogICAgICAgICAgICBhZGpbdG9dLnBiKHt7ZnJvbSxjfSx3fSk7CiAgICAgICAgfQogICAgICAgIGlmKG49PTEpCiAgICAgICAgewogICAgICAgICAgICBjb3V0IDw8IDAgPDwgZW5kbDsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGlmKG09PTApCiAgICAgICAgewogICAgICAgICAgICBjb3V0IDw8IC0xIDw8IGVuZGw7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KICAgICAgICBsbCByPTFlMTAsbD0xLGFuc3M9LTE7CiAgICAgICAgd2hpbGUobDw9cikKICAgICAgICB7CiAgICAgICAgICAgIGxsIG09IChsK3IpLzI7CiAgICAgICAgICAgIGxsIGFucyA9IGQobSxhZGopOwogICAgICAgICAgICBpZihhbnM8aykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGFuc3M9bTsKICAgICAgICAgICAgICAgICAgICByPW0tMTsKCiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBsPW0rMTsKICAgICAgICAgICAgfQoKICAgICAgICB9CiAgICAgICAgY291dCA8PCBhbnNzPDwgZW5kbDsKICAgIH0KfQo=