Codefores Round #764 Div3
// Steins;Gate
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define jiasu ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define don(i, a, b) for (int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define endl '\n'
// Codefores Round #764 Div3 最小或生成树
const int inf = 1e9;
int n, m;
vector<vector<pii>> mp;
int ans;
void dfs(int u, vector<bool>& st) {
st[u] = 1;
for (auto e : mp[u]) {
int v = e.first, w = e.second;
if (!st[v] && (ans | w) == ans) {
dfs(v, st);
}
}
}
void cnt(int pw) {
if (pw < 0)
return;
int d = 1 << pw;
ans -= d; // 尝试删除每一位的1
vector<bool> st(n, 0);
dfs(0, st); // 判断删除后是否连通
for (auto t : st) {
if (!t) {
ans += d;
break;
}
}
cnt(pw - 1);
}
void solve() {
cin >> n >> m;
mp.assign(n, vector<pii>(0));
rep(i, 1, m) {
int x, y, w;
cin >> x >> y >> w;
x--, y--;
mp[x].emplace_back(y, w);
mp[y].emplace_back(x, w);
}
ans = 1;
int bit = 0;
for (; ans < inf; bit++) {
ans = ans << 1 | 1;
}
// bit:最多左移了多少位
cnt(bit);
cout << ans << endl;
}
signed main() {
jiasu;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}