Tree Product

아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

Given nn rooted trees T_1,T_2,,T_nT\_1,T\_2,\ldots,T\_n, find two permutations p_1,p_2,,p_np\_1,p\_2,\dots,p\_n and q_1,q_2,,q_nq\_1,q\_2,\ldots,q\_n such that the diameter of T_p_1×T_p_2××T_p_nT\_{p\_1} \times T\_{p\_2} \times \ldots \times T\_{p\_n} is maximum and the diameter of T_q_1×T_q_2××T_q_nT\_{q\_1} \times T\_{q\_2} \times \ldots \times T\_{q\_n} is minimum.

For two rooted trees AA and BB, their tree product T=A×BT = A \times B is defined as follows: copy tree AA, and then for each vertex xx in it, make a copy of BB and merge its root with vertex xx. See the table below for an example:

It can be shown that tree product is associative: (A×B)×C=A×(B×C)(A \times B) \times C = A \times (B \times C). So the parentheses in a product of three or more trees can be omitted.

Recall that:

  • A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex vv is the last vertex different from vv on the path from the root to vv.
  • The diameter of a rooted tree is the length of the longest simple path in the tree, where the length of a path is the number of edges in the path.

입력

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1n106)(1 \le n \le 10^6), indicating the number of rooted trees.

Each of the next nn lines starts from an integer m_im\_i (1m_i1051 \le m\_i \le 10^5), indicating the number of vertices in the ii-th rooted tree. It is followed by m_im\_i integers p_i,1,p_i,2,,p_i,m_ip\_{i,1},p\_{i,2},\ldots,p\_{i,m\_i} (0p_i,jm_i0 \le p\_{i,j} \le m\_i) on the same line, where the jj-th of them denotes the parent of the jj-th vertex. The root of the tree has 00 as parent.

It is guaranteed that the sum of m_im\_i over all test cases does not exceed 10610^6.

출력

For each test case, output two integers: the maximum and the minimum diameter, in that order.

힌트

For the first sample test case, T_1×T_2×T_3T\_1 \times T\_2 \times T\_3 will provide the maximum diameter, while T_3×T_2×T_1T\_3 \times T\_2 \times T\_1 will provide the minimum diameter.