#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define vi vector<ll>
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define pii pair<ll,ll>
#define vii vector<pii>
#define MOD 1000000007LL
#define calc_fact(n) tgamma(n+1)
//disjoint set Union
ll find_set(ll x,vi& parent)
{
if(parent[x]==-1)return x;
return find_set(parent[x],parent);
}
void union_set(ll x,ll y,vi& parent)
{
ll xroot=find_set(x,parent),yroot=find_set(y,parent);
if(xroot!=yroot)
parent[yroot]=xroot;
}
signed main()
{
FIO;
ll t;
cin>>t;
while(t--){
ll n,m,ans=LONG_LONG_MAX;
cin>>n>>m;
vector<pii> adj[n];
vi parent(n,-1);
vector<pair<ll,pii>> edge;
// Kruskal Algorithmn for maximum Spanning Tree
for(ll i=0;i<m;i++)
{
ll a,b,c;
cin>>a>>b>>c;a--;b--;
edge.pb({c,{a,b}});
}
//sorting edge weight in decreasing order
sort(edge.rbegin(),edge.rend());
for(ll i=0;i<m;i++)
{
ll x=find_set(edge[i].S.F,parent),y=find_set(edge[i].S.S,parent);
if(x!=y)
{
union_set(x,y,parent);
adj[edge[i].S.F].pb({edge[i].S.S,edge[i].F});
adj[edge[i].S.S].pb({edge[i].S.F,edge[i].F});
}
}
vector<bool> visited(n,false);
//BFS
queue<ll> q;
q.push(0);
while(!q.empty())
{
ll v=q.front();
q.pop();
for(ll u=0;u<adj[v].size();u++)
{
if(!visited[adj[v][u].F])
{
visited[adj[v][u].F]=true;
q.push(adj[v][u].F);
ans=min(ans,adj[v][u].S);
}
}
}
// if not reachable or graph is disconnected
if(!visited[n-1])
ans=-1;
cout<<ans<<"\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBlbmRsICJcbiIKI2RlZmluZSBGSU8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRp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QogICAgewogICAgICAgIGxsIGEsYixjOwogICAgICAgIGNpbj4+YT4+Yj4+YzthLS07Yi0tOwogICAgICAgIGVkZ2UucGIoe2Mse2EsYn19KTsKICAgIH0KICAgIC8vc29ydGluZyBlZGdlIHdlaWdodCBpbiBkZWNyZWFzaW5nIG9yZGVyCiAgICAKICAgIHNvcnQoZWRnZS5yYmVnaW4oKSxlZGdlLnJlbmQoKSk7CiAgICBmb3IobGwgaT0wO2k8bTtpKyspCiAgICB7CiAgICAgICAgbGwgeD1maW5kX3NldChlZGdlW2ldLlMuRixwYXJlbnQpLHk9ZmluZF9zZXQoZWRnZVtpXS5TLlMscGFyZW50KTsKICAgICAgICBpZih4IT15KQogICAgICAgIHsKICAgICAgICAgICAgdW5pb25fc2V0KHgseSxwYXJlbnQpOwogICAgICAgICAgICBhZGpbZWRnZVtpXS5TLkZdLnBiKHtlZGdlW2ldLlMuUyxlZGdlW2ldLkZ9KTsKICAgICAgICAgICAgYWRqW2VkZ2VbaV0uUy5TXS5wYih7ZWRnZVtpXS5TLkYsZWRnZVtpXS5GfSk7CiAgICAgICAgfQogICAgfQogICAgdmVjdG9yPGJvb2w+IHZpc2l0ZWQobixmYWxzZSk7CiAgICAKICAgIC8vQkZTCiAgICAKICAgIHF1ZXVlPGxsPiBxOwogICAgcS5wdXNoKDApOwogICAgd2hpbGUoIXEuZW1wdHkoKSkKICAgIHsKICAgICAgICBsbCB2PXEuZnJvbnQoKTsKICAgICAgICBxLnBvcCgpOwogICAgICAgIGZvcihsbCB1PTA7dTxhZGpbdl0uc2l6ZSgpO3UrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKCF2aXNpdGVkW2Fkalt2XVt1XS5GXSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdmlzaXRlZFthZGpbdl1bdV0uRl09dHJ1ZTsKICAgICAgICAgICAgICAgIHEucHVzaChhZGpbdl1bdV0uRik7CiAgICAgICAgICAgICAgICBhbnM9bWluKGFucyxhZGpbdl1bdV0uUyk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAKICAgIC8vIGlmIG5vdCByZWFjaGFibGUgb3IgZ3JhcGggaXMgZGlzY29ubmVjdGVkCiAgICAKICAgIGlmKCF2aXNpdGVkW24tMV0pCiAgICBhbnM9LTE7CiAgICBjb3V0PDxhbnM8PCJcbiI7CiAgICB9Cn0=