#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef pair PII; typedef vector VI; typedef vector VPII; typedef pair PLL; typedef pair PIL; typedef pair PLI; typedef double DB; #define pb push_back #define mset(a, b) memset(a, b, sizeof a) #define all(x) (x).begin(), (x).end() #define bit(x) (1 << (x)) #define bitl(x) (1LL << (x)) #define sqr(x) ((x) * (x)) #define sz(x) ((int)(x.size())) #define counti(x) (__builtin_popcount(x)) #define clz(x) (__builtin_clz(x)) #define ctz(x) (__builtin_ctz(x)) #define countl(x) (__builtin_popcountll(x)) #define rep(i, n) for (int (i) = 0; (i) < (int)(n); ++(i)) #define Error(x) cout << #x << " = " << endl #define X first #define Y second template inline void chkmax(T& x, U y) { if (x < y) x = y; } template inline void chkmin(T& x, U y) { if (y < x) x = y; } #define MX 312345 struct Edge { int u, v, w; void input() { scanf("%d%d%d", &u, &v, &w); } }e[MX]; int n, m; int root[MX]; int Find_root(int u) { return root[u] == u ? u : root[u] = Find_root(root[u]); } int solve() { int ret = 0; for (int i = 30; i >= 0; i--) { int cnt = 0; ret |= bit(i); for (int j = 1; j <= n; j++) root[j] = j; for (int j = 0; j < m; j++) if ((ret & e[j].w) == ret) { int u = e[j].u, v = e[j].v; u = Find_root(u), v = Find_root(v); if (u != v) { root[u] = v; cnt++; } } if (cnt != n - 1) { ret ^= bit(i); } } return ret; } int main() { int Tcase; for (scanf("%d", &Tcase); Tcase--;) { cin >> n >> m; for (int i = 0; i < m; i++) { e[i].input(); } printf("%d\n", solve()); } return 0; }