def f(i,C):
</>if i in A and i not in C:
</></>C.append(i)
</></>f(A[i],C)
</>else:
</></>return len(C)
n=int(input())
for _ in range(n):
</>a=int(input())
</>A={}
</>for _ in range(a):
</></>b,c=map(int,input().split())
</></>A[b]=c
</>B=
</> for i in range(1,a+1):
</></> z=f(i,)
</></>B.append(z)
</>print(B)