Alice and Bob

Time limit1sMemory limit128 MB

Summary
Given all polygon sides and non-crossing diagonals as an unordered edge list, reconstruct the convex polygon's cyclic vertex order.
Level

Medium7 of 10

Topics
Graph, DFS, Geometry
Solved
No attempts yet

Problem

Alice and Bob play a puzzle. Alice draws a convex polygon with nn vertices and labels the vertices with the integers 1,2,…,n1, 2, \dots, n in an arbitrary order. She then draws some diagonals so that no two of them cross (sharing an endpoint is not considered a crossing).

Alice lists, in a single mixed collection, every side of the polygon together with the diagonals she drew, each described only by its two endpoints. She does not reveal which pairs are sides and which are diagonals. From this list alone Bob must recover the cyclic order of the vertices around the border of the polygon.

Because the polygon is convex and the diagonals never cross, the border order is uniquely determined up to rotation and reflection. Write a program that, for each puzzle, reconstructs that border order.

Input

The first line contains one integer dd, the number of puzzles (1≤d≤201 \le d \le 20).

Each puzzle is given on two lines.

  • The first line contains two integers nn and mm (3≤n≤10 0003 \le n \le 10\,000, 0≤m≤n−30 \le m \le n - 3): the number of vertices and the number of diagonals.
  • The second line contains 2(m+n)2(m + n) integers describing the nn sides and the mm diagonals. For each jj with 1≤j≤m+n1 \le j \le m + n, the integers at positions 2j−12j-1 and 2j2j are the endpoints aja_j and bjb_j of one side or diagonal, with 1≤aj,bj≤n1 \le a_j, b_j \le n and aj≠bja_j \ne b_j.

The sides and diagonals appear in an arbitrary order and there are no duplicates. Every puzzle is guaranteed to have a solution.

Output

Print exactly dd lines, one per puzzle. For the ii-th puzzle, print a permutation of 1,2,…,n1, 2, \dots, n: the vertices in the order they appear around the border of the polygon. The sequence must start at vertex 11, and its second element must be the smaller of the two border neighbours of vertex 11.

Examples5

  1. Example 1

    Input
    1
    4 1
    1 3 4 2 1 2 4 1 2 3
    
    Expected output
    1 3 2 4
    
  2. Example 2

    Input
    1
    3 0
    1 2 1 3 2 3
    
    Expected output
    1 2 3
    
  3. Example 3

    Input
    1
    4 0
    1 4 2 3 4 2 3 1
    
    Expected output
    1 3 2 4
    
  4. Example 4

    Input
    1
    5 0
    4 5 5 1 2 3 1 2 4 3
    
    Expected output
    1 2 3 4 5
    
  5. Example 5

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