/*并查集 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
#define INF 0x3f3f3f3f
int father[N],a[N];
int find(int x)
{
return x==father[x]?x:(father[x]=find(father[x]));
}
int main()
{
int t,i,j,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int u,v,w,b,c;
memset(a,0,sizeof(a));
for(i=0; i<=n; i++)
father[i]=i;
for(i=1; i<n; i++)
{
scanf("%d%d%d",&u,&v,&w);
if(w)
continue;
b=find(u);
c=find(v);
if(b!=c)
father[b]=c;
}
for(i=1; i<=n; i++)
b=find(i);
for(i=1; i<=n; i++)/* 该集合有多少个人 */
a[father[i]]++;
int sum=0;
for(i=1; i<=n; i++)
if(a[i]%2)
sum^=a[i];
printf("%d\n",sum);
}
return 0;
}