#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 countl(x) (__builtin_popcountll(x)) #define clz(x) (__builtin_clz(x)) #define clzl(x) (__builtin_clzll(x)) #define ctz(x) (__builtin_ctz(x)) #define ctzl(x) (__builtin_ctzll(x)) #define rep(i, n) for (int (i) = 0; (i) < (int)(n); ++(i)) #define X first #define Y second #define Error(x) cout << #x << " = " << x << endl 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; } const int MN = 300005; int n, m, T, cnt; VI con[MN]; int a[MN], b[MN], w[MN]; int vis[MN]; void dfs(int u) { cnt++; vis[u] = T; for (int i = 0; i < con[u].size(); i++) { int v = con[u][i]; if (vis[v] == T) continue; dfs(v); } } bool tree() { T++; cnt = 0; dfs(0); return cnt == n; } int main() { int tn; for (cin >> tn; tn--; ) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { con[i].clear(); } for (int i = 0; i < m; i++) { scanf("%d%d%d", a + i, b + i, w + i); a[i]--, b[i]--; con[a[i]].push_back(b[i]); con[b[i]].push_back(a[i]); } if (!tree()) { puts("0"); continue; } int mask = 0; for (int i = 0; i >= 0; i--) { mask += bit(i); for (int j = 0; j < n; j++) con[j].clear(); for (int j = 0; j < m; j++) { if ((mask & w[j]) == mask) { int u = a[j]; int v = b[j]; con[u].push_back(v); con[v].push_back(u); } } if (!tree()) { mask -= bit(i); } } printf("%d\n", mask); } return 0; }