program zht;
var
i,n,t,s,bh,q,w:longint;
x,y,p:array[0..110000] of longint;
h,f:array[0..2100000] of longint;
procedure hash(x:longint);
begin
s:=x mod 2000007;
while (h[s]<>0) and (h[s]<>x) do
inc(s);
h[s]:=x;
end;
procedure find(x:longint);
begin
if f[x]=x then begin s:=x; exit; end
else begin find(f[x]); f[x]:=s; end;
end;
begin
assign(input,'prog.in');
assign(output,'prog.out');
reset(input);
rewrite(output);
readln(t);
for t:=1 to t do
begin
fillchar(h,sizeof(h),0);
readln(n);
for i:=1 to n do
begin
readln(x[i],y[i],p[i]);
hash(x[i]);
x[i]:=s;
hash(y[i]);
y[i]:=s;
end;
for i:=1 to 2000010 do
f[i]:=i;
for i:=1 to n do
begin
if p[i]=0 then continue;
find(x[i]);
q:=s;
find(y[i]);
w:=s;
f[q]:=w;
end;
bh:=0;
for i:=1 to n do
begin
if p[i]=1 then continue;
find(x[i]);
q:=s;
find(y[i]);
w:=s;
if q=w then begin bh:=1; writeln('NO'); break; end;
end;
if bh=0 then writeln('YES');
end;
close(input);
close(output);
end.