#pragma comment(linker, "/STACK:1677721600") #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define sf scanf #define pf printf #define INF 0x3f3f3f3f #define mem0(a) memset(a,0,sizeof(a)) #define mem_1(a) memset(a,-1,sizeof(a)) #define FIN freopen("in.txt", "r", stdin) #define FOUT freopen("out.txt", "w", stdout) template inline bool in(T &ret) { char c; int sgn; if (c=getchar(),c==EOF) return 0; while (c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } template inline void out(T x) { if(x<0){ putchar('-'); x=-x; } if(x>9) out(x/10); putchar(x%10+'0'); } template T MAX(T a, T b) { return a > b ? a : b; } template T MIN(T a, T b) { return a < b ? a : b; } template T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } template T LCM(T a, T b) { return a / GCD(a,b) * b; } typedef long long LL; typedef unsigned long long ULL; const int MAXN = 50 + 10; const int MAXM = 50 + 10; const double eps = 1e-8; LL MOD = 1000000007; int f[100005],a[100005]; int finnd(int x) { if(x!=f[x]) f[x]=finnd(f[x]); return f[x]; } int main(){ int T,n,x,b,t; cin>>T; while(T--){ in(n); mem0(a); for(int i=1;i<=n;i++) f[i]=i; for(int i=1;i<=n-1;i++){ in(x);in(b);in(t); if(t==0) if(finnd(x)!=finnd(b)){ f[b]=finnd(x); } } for(int i=1;i<=n;i++){ if(f[i]!=i){ a[f[i]]++; } else a[i]=1; } //for(int i=1;i<=n;i++) cout<<"i "<