#include #include #include #include #include #include #include #include #include #define son e[i].t #define ls (p << 1) #define rs (p << 1) | 1 #define mp make_pair #define fr first #define sc second #define maxn 100010 typedef long long ll; using namespace std; int T, n, u, v, w; int ans[maxn], tot[maxn], pre[maxn]; struct edge{ int t, w, nxt; }e[maxn * 2]; int g[maxn], ei; void ae(int a, int b, int c) { e[++ ei].t = b; e[ei].w = c; e[ei].nxt = g[a]; g[a] = ei; } void dfs(int x) { tot[x] = 1; for (int i = g[x]; i; i = e[i].nxt) if (son != pre[x]){ pre[son] = x; dfs(son); if (e[i].w == 0) tot[x] += tot[son]; } } void dfs2(int x, int fw) { ans[x] = tot[x]; if (fw == 0) ans[x] += ans[pre[x]] - tot[x]; for (int i = g[x]; i; i = e[i].nxt) if (son != pre[x]) dfs2(son, e[i].w); } int read() { int x = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x; } void solve() { memset(tot, 0, sizeof(tot)); memset(ans, 0, sizeof(ans)); memset(pre, 0, sizeof(pre)); memset(g, 0, sizeof(g)); ei = 0; scanf("%d", &n); for (int i = 1; i < n; ++ i){ u = read(); v = read(); w = read(); ae(u, v, w); ae(v, u, w); } dfs(1); dfs2(1, 1); int anss = 0; for (int i = 1; i <= n; ++ i) anss ^= ans[i]; printf("%d\n", anss); } int main() { scanf("%d", &T); while (T --) solve(); return 0; }