Universal Cup Judging System

Universal Cup

時間限制: 3.0 s 記憶體限制: 2048 MB 總分: 100 难度: [顯示] 可 Hack ✓
统计

Piggy (who, in his spare time, manages the Interstellar Connectivity Protocol Commission) is facing a delicate hardware conundrum. The latest communication grid, consisting of $n$ relay stations connected by $n-1$ fiber-optic links to form a tree structure, is finally ready for deployment. However, the system must be powered on with extreme caution.

The activation process involves firing laser pulses along specific paths. A path is defined as the unique simple route between two stations. Due to the catastrophic risk of “Quantum Backflow Interference” — a phenomenon that keeps the commission’s lawyers awake at night — the following protocol must be strictly observed:

  • Transmission paths must be established one by one.
  • To prevent power surges, each newly established path may share at most one station with the set of stations already activated by any previous path.

Formally, let $S$ be the set of currently activated stations. For any new path $P$ consisting of a set of stations $V(P)$, the condition $|V(P) \cap S| \le 1$ must hold. Once a path is successfully fired, all stations along that path are considered part of the activated set $S$.

Your mission, should you choose to accept it, is to determine the minimum number of laser pulses required to activate every station in the entire network and provide one such sequence of paths to satisfy the legal department.

The illustration of the first two sample test cases. Note that in Case 2, the second pulse shares only station 2 with the first, satisfying the non-interference rule.

Input

There are multiple test cases. The first line of the input contains an integer $T$ ($1 \le T \le 10^4$) indicating the number of test cases. For each test case:

The first line contains an integer $n$ ($2 \le n \le 3 \times 10^5$), the number of stations in the grid.

For the following $n-1$ lines, the $i$-th line contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n, u_i \neq v_i$), describing a bidirectional fiber-optic link between station $u_i$ and station $v_i$.

It is guaranteed that the links form a tree and that the sum of $n$ over all test cases does not exceed $3 \times 10^5$.

Output

For each test case, output the minimum number of paths $k$ on the first line. Following this, output $k$ lines, each containing two integers $a_i$ and $b_i$, representing the endpoints of the $i$-th path in the sequence. Note that $a_i$ may equal $b_i$ if the path consists of a single station.

If multiple optimal sequences exist, any one that strictly follows the non-interference protocol will be accepted.

Examples

Input 1

3
3
1 2
2 3
5
1 2
2 3
2 4
2 5
7
1 2
1 3
2 4
2 5
3 6
3 7

Output 1

1
1 3
2
4 5
1 3
3
7 7
1 6
4 5

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.