D. Reachability and Tree

Problem Statement

Let uu and vv be two distinct vertices in a directed graph. Let’s call the ordered pair (u,v)(u,v) good if there exists a path from vertex uu to vertex vv along the edges of the graph.

You are given an undirected tree with nn vertices and n1n-1 edges. Determine whether it is possible to assign a direction to each edge of this tree so that the number of good pairs in it is exactly nn. If it is possible, print any way to direct the edges resulting in exactly nn good pairs.

Input

The first line contains one integer tt (1t104)(1 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains one integer nn (2n2105)(2 \leq n \leq 2 \cdot 10^5) — the number of vertices in the tree.

The next n1n-1 lines describe the edges. The ii-th line contains two integers uiu_i and viv_i (1ui,vin;uivi)(1 \leq u_i, v_i \leq n; u_i \neq v_i) — the vertices connected by the ii-th edge.

It is guaranteed that the edges in each test case form an undirected tree and that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, print “NO” (case-insensitive) if it is impossible to direct all edges of the tree and obtain exactly nn good pairs of vertices.

Otherwise, print “YES” (case-insensitive) and then print n1n-1 pairs of integers uiu_i and viv_i separated by spaces — the edges directed from uiu_i to viv_i.

The edges can be printed in any order. If there are multiple answers, output any.

Solution

The idea is to find a node with degree 2. Suppose we have vv with degree 2, then we can have uvwu \rightarrow v \rightarrow w. Then make all the other edges have length 1, and we will have (n1)+1=n(n-1) + 1 = n good pairs. The n1n-1 good pairs are provided by the edges and 1 more is provided by uwu \rightarrow w.

submission link