TOUR - Fake tournament

We consider only special type of tournaments. Each tournament consists of a series of matches. We have n competitors at the beginning of a competition and after each match the loser is moved out of the competition and the winner stays in (there are no draws). The tournament ends when there is only one participant left - the winner. It is a task of National Sports Federation to schedule the matches. Members of this committee can pick the contestants for the first match. Then, after they know the result, they say which of the remaining contestants meet in the second match, and so on until there is only one participant left.

It is easy to see that not only skill and training decides about the win, but also "luck" - i.e. the schedule. The members of NSF know it as well.

The committee used the training time to look carefully on the performance of each probable contestant. It is clear now, at the start of the season, that some of the results between the competitors are 100% predictable. Having this information NSF considers if it is possible to schedule the matches in such a way that the given contestant x wins. That is to plan the matches for x only with those who will lose with him (then he wins the whole tournament of course). If it is possible then w say that the tournament can be set for x.

Task

Your task is to write a program which determines the number of contestants of a given tournament for which it is possible to set it.

Input

t [number of tests to solve].
In the first line of each test: n (1<=n<=1000) - the number of participants of the tournament. We number the participants with numbers 1, 2 ... n. The following line contains a list of participants who will inevitably win with participant 1. This list begins with a number m (the number of contestants "better" than 1) and numbers n1, n2 ... nm delimited by single spaces.
Next n-1 lines contain analogous lists for participants 2, 3 ... n.

Remark 1. The fact that participant a would lose with b and b would lose with c doesn't necessarily mean that a would lose with c in a direct match.

Remark 2. It is not possible that a is on the list of contestants better than b and b is on the list of a at the same time.

Output

For each test your program should output a single integer - the number of participants, for which it is possible to set the tournament.

Example

Input:
1
3
2 3 2
1 3
0

Output:
1

Added by:Adam Dzedzej
Date:2004-06-08
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Internet Contest Pogromcy Algorytmów (Algorithm Tamers)
Round IV, 2001

hide comments
2016-06-14 14:44:11 Abhishek Jaisingh
Strongly Connected Components => O(n)
2016-03-21 17:47:15 Ved
@shady tournament can be set for participant 1, 2, 5
2016-03-20 17:09:05 shady
Can anybody tell why is the answer 3 for this test case below ->>
6
1 5
1 1
1 2
1 2
1 2
1 2
2016-02-10 21:08:49
can anyone explain third ,fourth ,fifth line of the input please
2016-02-03 13:41:52 Arjav Patel
@Deepak your comment helped!
2015-12-12 10:21:47 Deepak Singh Tomar
if we have more than one SCC with nobody defeating it, output should be 0.
2015-11-23 16:11:34
ankit kumar's comment saved me
no need of graphs think simple...
2015-09-08 18:55:01 7Bubble
but, Ankit kumar's comment helped me!
2015-08-22 13:36:44 (Tjandra Satria Gunawan)(曾毅昆)
Ankit Kumar's hint is wrong, thinking too simple will lead me to WA :p
2015-08-22 05:15:58
*Weak Testcases detected* :D, Add to the test cases:

1
5
2 2 4
1 3
1 1
0
1 1

ans: 1


1
6
2 2 3
1 3
1 4
2 1 6
1 2
0

ans: 1

my code outputs 0 in both and got AC.

Last edit: 2015-08-22 12:04:14
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.