#include #include #include using namespace std; const int maxn = 100000 + 10; int f[maxn],h[maxn]; int find_ance(int x) { return x == f[x] ? x : (f[x] = find_ance(f[x])); } int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(int i = 1; i <= n; i++) { f[i] = i; h[i] = 1; } for(int i = 0; i < n - 1; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(w == 0) { int A = find_ance(u); int B = find_ance(v); if(A != B) { f[A] = B; h[B] += h[A]; } } } /*for(int i = 1; i <= n; i++) { if(f[i] == i) printf("%d:%d\n",i,h[i]); }*/ int ans = 0; for(int i = 1; i <= n; i++) { if(f[i] == i && h[i] % 2 == 1) ans ^= h[i]; } printf("%d\n",ans); } return 0; }