QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 512 MB Total points: 100

#17794. Doughnut

Statistics

Today is another peaceful day — Kruskal-chan is in the library struggling with graph theory.

Suddenly, a copy of "A Guide to the Doughnut Planet" fell off the shelf, with a note tucked inside:

"Dear Kruskal-chan, we cordially invite you to the Doughnut Planet to help us calculate $n + m$! You remember all the regional adjacency relationships, right?"

Huh? What, what is this? Has my graph theory ability already spread across the universe?

The inhabitants of the Doughnut Planet live on a doughnut-shaped planet. For ease of management, the Doughnut King has drawn $n$ latitude circles (dotted lines in the figure) and $m$ longitude circles (dashed lines in the figure) on the planet, dividing the surface into $nm$ regions, numbered $1 \sim nm$.

Kruskal was invited to visit the Doughnut Planet. Since she had just learned graph theory, she remembered all the regional adjacency relationships (i.e., which regions are adjacent to each other). Can you help her calculate the value of $n + m$?

Input

The problem contains multiple test cases.

The first line contains an integer $T$ ($1 \le T \le 10^5$), representing the number of test cases.

For each test case, the first line contains an integer $k$ ($0 \le k \le 10^5$), representing the total number of regional adjacency relationships.

Following this are $k$ lines, each containing two integers $u, v$, indicating that the region numbered $u$ and the region numbered $v$ are adjacent.

Note: The adjacency relationships are guaranteed to be generated by some $n, m$, but may be given in any order.

It is guaranteed that $\sum k \le 10^5$.

Output

Output $T$ lines, each containing an integer representing the value of $n + m$. If the value of $n + m$ cannot be uniquely determined, output $-1$.

Examples

Input 1

2
1
1 2
9
1 2
2 3
3 1
4 5
5 6
6 4
1 6
2 5
3 4

Output 1

3
5

Note

In the first example, one of $n, m$ is $1$ and the other is $2$; it can be proven that no other possibilities exist.

In the second example, one of $n, m$ is $2$ and the other is $3$; it can be proven that no other possibilities exist.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.