#include #include #include #include #include #include #include #include #define inf 0x3f3f3f3f #define mod 9999991 using namespace std; void fre(){freopen("d://input.txt","r",stdin);freopen("d://output.txt","w",stdout);} #define MS(x,y) memset(x,y,sizeof(x)) #define rep(i,x,y) for (int i=x;i<=y;i++) typedef __int64 LL; const int N=100005; int fa[N],num[N]; int visit[N],tot,n; int find_fa(int a){ int r=a,k,l; while(r!=fa[r]) r=fa[r]; k=a; while(k!=r){ l=fa[k]; fa[k]=r; num[r]+=num[k]; num[k]=0; k=l; } return r; } void hebing(int a,int b){ fa[a]=b; num[b]+=num[a];num[a]=0; } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); rep(i,1,n) {num[i]=1;fa[i]=i;} int x,y,v; rep(i,1,n-1){ scanf("%d %d %d",&x,&y,&v); if(v==0){ int x1=find_fa(x); int y1=find_fa(y); if(x1!=y1) hebing(x1,y1); } } int ans=0; rep(i,1,n){ if(fa[i]==i && (num[i]&1))ans=ans^num[i]; } printf("%d\n",ans); } return 0; }