Under construction

Still being built. Things may change

Amazing Tree

Time limit1sMemory limit1024 MB

Summary
Given a tree, choose the starting vertex and neighbour order so the DFS post-order is lexicographically smallest, then print that order for each test case.
Level

Medium6 of 10

Topics
Tree, DFS, Greedy
Solved
No attempts yet

Problem

Consider an undirected tree. The following algorithm constructs a post-order traversal of the tree:

fun dfs(v):
    mark v as used
    for u in neighbours(v):
        if u is not used:
            dfs(u)
    append v to order

The post-order traversal will be in the list order. You may choose the order of neighbours for each vertex, and you may choose the starting vertex. Find the lexicographically minimal order that you can obtain.

Input

The first line contains one integer TT (1≤T≤1051 \le T \le 10^{5}), the number of test cases. Each test case begins with a line containing a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^{5}), the number of vertices. The next n−1n-1 lines each contain two integers ui,viu_i, v_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), meaning that there is an undirected edge between uiu_i and viv_i. The given graph is guaranteed to be a tree. The sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^{5}.

Output

For each test case, print the lexicographically minimal order on a separate line.

Hint

The first test looks as follows:

Starting from vertex 1, the only possible order is 2 3 1. Starting from vertex 2, the only possible order is 1 3 2. Starting from vertex 3, there are two possible orders: 1 2 3 and 2 1 3. The lexicographically minimal of the four orders is 1 2 3.

The second test looks as follows:

Starting from vertex 1, there are two possible orders: 2 3 1 and 3 2 1. Starting from vertex 2, the only possible order is 3 1 2. Starting from vertex 3, the only possible order is 2 1 3. The lexicographically minimal of the four orders is 2 1 3.

The third test looks as follows:

The lexicographically minimal order is 4 5 2 1 6 3 7, and it can be obtained by starting from vertex 7.

Examples1

  1. Example 1

    Input
    3
    3
    1 3
    3 2
    3
    2 1
    1 3
    7
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    
    Expected output
    1 2 3
    2 1 3
    4 5 2 1 6 3 7