////***************Copy Right 2021 < BUNNY />  *********************////
//////************* ولسوف يعطيك ربك فترضي *****************/
// Sometimes, the questions are complicated - and the answers are simple. //
///////*********************** (ACPC Next Year) *************************////
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define f first
#define s second
#define Ya_Rab_Accepted ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

void LoL_Code_Bela_Hadaf_() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
int MOD = 1e9 + 7;
const long long INF = 1e18, N = 1e6;
ll p[N], n, m;
ll sz[N];


int group(int x) { // let me know the ID of the group
    if (x == p[x])return x;
    return p[x] = group(p[x]);
}

void connect(int x, int y) { // make union
    int g1 = group(x), g2 = group(y);
    if (g1 == g2)
        return;
    if (sz[g1] < sz[g2]) swap(g1, g2); // make g2 alwayes smaller
    p[g2] = g1;
    sz[g1] += sz[g2];

}

void init() {
    for (int i = 1; i <= n; i++) { // init the elements
        p[i] = i;
        sz[i] = 1;
    }

}



int main() {
    Ya_Rab_Accepted
    LoL_Code_Bela_Hadaf_();

    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        init();
        vector<pair<ll, pair<int, int >>> adj;
        
        for (int i = 1; i <= m; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            adj.push_back({z, {x, y}});
        }
        
        // sort from big to small
        sort(adj.rbegin(), adj.rend());
        bool ans = 0;
        
        for (auto i : adj) {
            int a = i.s.f, b = i.s.s, c = i.f;
            if (group(a) != group(b)) {
                connect(a, b);
                
                if (group(1) == group(n)) { // here if we could reach n from 1 the we print the smallest element we get
                    cout << i.f << '\n';
                    ans = 1;
                }

            }
        }

        if (!ans) {
            cout << "-1\n";
        }


    }


    return 0;
}