/***********Template Starts Here***********/
#include <bits/stdc++.h>
#define pb push_back
#define nl puts ("")
#define sp printf ( " " )
#define phl printf ( "hello\n" )
#define ff first
#define ss second
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define MP make_pair
#define FOR(i,x,y) for(int i = (x) ; i <= (y) ; ++i)
#define ROF(i,x,y) for(int i = (y) ; i >= (x) ; --i)
#define CLR(x,y) memset(x,y,sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define NUMDIGIT(x,y) (((int)(log10((x))/log10((y))))+1)
#define SQ(x) ((x)*(x))
#define ABS(x) ((x)<0?-(x):(x))
#define FABS(x) ((x)+eps<0?-(x):(x))
#define ALL(x) (x).begin(),(x).end()
#define LCM(x,y) (((x)/gcd((x),(y)))*(y))
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long vlong;
typedef unsigned long long uvlong;
typedef pair < int, int > pii;
typedef pair < vlong, vlong > pll;
typedef vector<pii> vii;
typedef vector<int> vi;
const vlong inf = 2147383647;
/***********Template Ends Here***********/
vii adj[1000100];
int ign;
int vis[1000010], col[1000010], vv;
int n;
bool bicolor;
///Bicolor the graph
int dfs ( int s, int p ) {
vis[s] = vv;
FOR(i,0,SZ(adj[s])-1) {
pii temp = adj[s][i];
int t = temp.ff;
int w = temp.ss;
if ( w >= ign ) continue;
if ( t == p ) continue;
if ( vis[t] != vv ) {
col[t] = 1 - col[s];
dfs ( t, s );
}
else {
if ( col[t] == col[s] ) {
bicolor = false;
}
}
}
}
///Check if graph is bipartite
bool bipartite ( int len ) {
ign = len;
vv++;
bicolor = true;
FOR(i,1,n) {
if ( vis[i] != vv ) {
col[i] = 0; ///White
dfs ( i, -1 );
if ( bicolor == false ) return false;
}
}
return true;
}
int main () {
int kase;
scanf ( "%d", &kase );
while ( kase-- ) {
int m;
scanf ( "%d %d", &n, &m );
FOR(i,1,n) adj[i].clear();
int mx = 0, mn = inf;
FOR(i,0,m-1) {
int a, b, c;
scanf ( "%d %d %d", &a, &b, &c );
adj[a].pb ( MP(b,c) );
adj[b].pb ( MP(a,c) );
mx = MAX(mx,c);
mn = MIN(mn,c);
}
bool res = bipartite( mx + 1 ); ///Keep all the edges
if ( res ) {
printf ( "%d\n", 0 );
continue;
}
int low = 1, high = mx;
while ( low <= high ) {
int mid = ( low + high ) / 2;
res = bipartite ( mid );
if ( res ) {
low = mid + 1;
}
else high = mid - 1;
}
if ( low - 1 == mn ) low = 0; ///Need to remove all edges. So answer is -1
printf ( "%d\n", low - 1 );
}
return 0;
}
LyoqKioqKioqKioqVGVtcGxhdGUgU3RhcnRzIEhlcmUqKioqKioqKioqKi8KI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG5sIHB1dHMgKCIiKQojZGVmaW5lIHNwIHByaW50ZiAoICIgIiApCiNkZWZpbmUgcGhsIHByaW50ZiAoICJoZWxsb1xuIiApCiNkZWZpbmUgZmYgZmlyc3QKI2RlZmluZSBzcyBzZWNvbmQKI2RlZmluZSBQT1BDT1VOVCBfX2J1aWx0aW5fcG9wY291bnRsbAojZGVmaW5lIFJJR0hUTU9TVCBfX2J1aWx0aW5fY3R6bGwKI2RlZmluZSBMRUZUTU9TVCh4KSAoNjMtX19idWlsdGluX2NsemxsKCh4KSkpCiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgRk9SKGkseCx5KSBmb3IoaW50IGkgPSAoeCkgOyBpIDw9ICh5KSA7ICsraSkKI2RlZmluZSBST0YoaSx4LHkpIGZvcihpbnQgaSA9ICh5KSA7IGkgPj0gKHgpIDsgLS1pKQojZGVmaW5lIENMUih4LHkpIG1lbXNldCh4LHksc2l6ZW9mKHgpKQojZGVmaW5lIFVOSVFVRShWKSAoVikuZXJhc2UodW5pcX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AgICAgICBwaWkgdGVtcCA9IGFkaltzXVtpXTsKICAgICAgICBpbnQgdCA9IHRlbXAuZmY7CiAgICAgICAgaW50IHcgPSB0ZW1wLnNzOwoKICAgICAgICBpZiAoIHcgPj0gaWduICkgY29udGludWU7CgogICAgICAgIGlmICggdCA9PSBwICkgY29udGludWU7CgogICAgICAgIGlmICggdmlzW3RdICE9IHZ2ICkgewogICAgICAgICAgICBjb2xbdF0gPSAxIC0gY29sW3NdOwogICAgICAgICAgICBkZnMgKCB0LCBzICk7CiAgICAgICAgfQogICAgICAgIGVsc2UgewogICAgICAgICAgICBpZiAoIGNvbFt0XSA9PSBjb2xbc10gKSB7CiAgICAgICAgICAgICAgICBiaWNvbG9yID0gZmFsc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCi8vL0NoZWNrIGlmIGdyYXBoIGlzIGJpcGFydGl0ZQpib29sIGJpcGFydGl0ZSAoIGludCBsZW4gKSB7CiAgICBpZ24gPSBsZW47CiAgICB2disrOwoKICAgIGJpY29sb3IgPSB0cnVlOwogICAgRk9SKGksMSxuKSB7CiAgICAgICAgaWYgKCB2aXNbaV0gIT0gdnYgKSB7CiAgICAgICAgICAgIGNvbFtpXSA9IDA7IC8vL1doaXRlCiAgICAgICAgICAgIGRmcyAoIGksIC0xICk7CiAgICAgICAgICAgIGlmICggYmljb2xvciA9PSBmYWxzZSApIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIHRydWU7Cn0KCmludCBtYWluICgpIHsKICAgIGludCBrYXNlOwogICAgc2NhbmYgKCAiJWQiLCAma2FzZSApOwoKICAgIHdoaWxlICgga2FzZS0tICkgewogICAgICAgIGludCBtOwogICAgICAgIHNjYW5mICggIiVkICVkIiwgJm4sICZtICk7CgogICAgICAgIEZPUihpLDEsbikgYWRqW2ldLmNsZWFyKCk7CgogICAgICAgIGludCBteCA9IDAsIG1uID0gaW5mOwogICAgICAgIEZPUihpLDAsbS0xKSB7CiAgICAgICAgICAgIGludCBhLCBiLCBjOwogICAgICAgICAgICBzY2FuZiAoICIlZCAlZCAlZCIsICZhLCAmYiwgJmMgKTsKCiAgICAgICAgICAgIGFkalthXS5wYiAoIE1QKGIsYykgKTsKICAgICAgICAgICAgYWRqW2JdLnBiICggTVAoYSxjKSApOwoKICAgICAgICAgICAgbXggPSBNQVgobXgsYyk7CiAgICAgICAgICAgIG1uID0gTUlOKG1uLGMpOwogICAgICAgIH0KCiAgICAgICAgYm9vbCByZXMgPSBiaXBhcnRpdGUoIG14ICsgMSApOyAvLy9LZWVwIGFsbCB0aGUgZWRnZXMKICAgICAgICBpZiAoIHJlcyApIHsKICAgICAgICAgICAgcHJpbnRmICggIiVkXG4iLCAwICk7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KCiAgICAgICAgaW50IGxvdyA9IDEsIGhpZ2ggPSBteDsKCiAgICAgICAgd2hpbGUgKCBsb3cgPD0gaGlnaCApIHsKICAgICAgICAgICAgaW50IG1pZCA9ICggbG93ICsgaGlnaCApIC8gMjsKCiAgICAgICAgICAgIHJlcyA9IGJpcGFydGl0ZSAoIG1pZCApOwoKICAgICAgICAgICAgaWYgKCByZXMgKSB7CiAgICAgICAgICAgICAgICBsb3cgPSBtaWQgKyAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaGlnaCA9IG1pZCAtIDE7CiAgICAgICAgfQoKICAgICAgICBpZiAoIGxvdyAtIDEgPT0gbW4gKSBsb3cgPSAwOyAvLy9OZWVkIHRvIHJlbW92ZSBhbGwgZWRnZXMuIFNvIGFuc3dlciBpcyAtMQogICAgICAgIHByaW50ZiAoICIlZFxuIiwgbG93IC0gMSApOwoKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=