#include #define NTop 100010 int n,fa[NTop],size[NTop]; inline void clearFa(int n){for(int i = 1;i <= n;++i) fa[i] = i,size[i] = 1;} int getFa(int id){if (id != fa[id]) fa[id] = getFa(fa[id]);return fa[id];} inline void joinFa(int x,int y){x = getFa(x),y = getFa(y);if (x == y) return;fa[y] = x;size[x] += size[y];size[y] = 0;} int main() { #ifdef local freopen("tree_test.txt","r",stdin); #endif int T,u,v,w,ans; scanf("%d",&T); for(int i = 1;i <= T;++i) { scanf("%d",&n); clearFa(n); for(int j = 1;j < n;++j) { scanf("%d%d%d",&u,&v,&w); if (w == 0) joinFa(u,v); } ans = (size[n] & 1)?size[n]:0; for(int j = 1;j < n;++j) // printf("[%d=%d]",j,size[j]), ans = ans ^ ((size[j] & 1)?size[j]:0); // printf("[%d=%d]",n,size[n]); printf("%d\n",ans); } return 0; }