## TOUR - Fake tournament

no tags

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.

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```

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define MOD (ll)1000000007
#define pb   push_back
#define EPS 1e-9
#define FOR(i,n)  for(int i = 0;i < n; i++)
#define FORE(i,a,b) for(int i = a;i <= b; i++)
#define pi(a)   printf("%d\n", a)
#define all(c)  c.begin(), c.end()
#define tr(container, it)   for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define gc getchar_unlocked
#define sdi(a, b)   si(a);si(b)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define F first
#define S second
#define FILL(arr, val)  memset(arr, val, sizeof(arr))
#define read(arr, n)    for(int i = 0;i < n; i++)cin>>arr[i];
#define sp ' '

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

void si(int &x){
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 || c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}

const int MAXN = 1e3+5;
int components[MAXN], in_degree[MAXN];
int n;
bool vis[MAXN];
stack<int> stk;
int numberOfComponents;

void dfs(int src){
vis[src] = 1;
}
}
stk.push(src);
}

void dfsF(int src){
vis[src] = 1;
components[src] = numberOfComponents;
}
}
}

void scc(){
while(!stk.empty()){
int ele = stk.top();
stk.pop();
if(!vis[ele]){
numberOfComponents++;
dfsF(ele);
}
}
}

int main(){
io;
int t;
cin >> t;
while(t--){
cin >> n;
for(int v = 1;v <= n; v++){
int m;
cin >> m;
//u -> v means u can defeat v.
for(int j = 0;j < m; j++){
int u;
cin >> u;
}
}
for(int i = 1;i <= n; i++){
if(!vis[i]){
dfs(i);
}
}
FORE(i,1,n) vis[i] = false;
scc();
//DAG of SCC is formed calculate indegree of components
for(int i = 1;i <= n; i++){
for(int j = 0;j < adj[i].size(); j++){
}
}
}
int countZeroIndegree = 0;
int num = -1;
FORE(i,1,numberOfComponents){
if(in_degree[i] == 0){
countZeroIndegree++;
num = i;
}
}
if(countZeroIndegree > 1 || numberOfComponents == 0 || countZeroIndegree == 0){
cout << 0 << endl;
}else{
int ans = 0;
FORE(i,1,n){
if(components[i] == num){
ans++;
}
}
cout << ans << endl;
}
}
return 0;
}``````
Copy The Code &

Input

cmd
1
3
2 3 2
1 3
0

Output

cmd
1