Under construction

Still being built. Things may change

Heavy Cargo

Time limit2sMemory limit1024 MB

Summary
For each room p from 2 to n, find the fewest lifts and put-downs needed to carry the box from room 1 to room p, or report -1.
Level

Medium7 of 10

Topics
Graph, Shortest path
Solved
No attempts yet

Statement

Warehouse operators need to move a heavy box with a special forklift. The warehouse can be modeled as nn rooms connected by mm corridors. From any room, you can reach any other room by walking through corridors. The rooms are numbered from 1 to nn. Corridor ii directly connects rooms uiu_i and viv_i, and it can be traversed in both directions.

The forklift can lift and put down the box. When it is not holding the box, it can move through free rooms and corridors. At the start, the forklift is in room 1 and holds the box. The forklift supports these actions:

  1. If the forklift is in room aa and holds the box, it can put the box down in room bb without moving, provided rooms aa and bb are directly connected by a corridor. After this action, the forklift no longer holds the box and can move.
  2. If the forklift is in room aa and the box is in room bb, it can pick up the box without moving, provided rooms aa and bb are directly connected by a corridor. After this action, the forklift stays in room aa and holds the box. It cannot move until it puts the box down.
  3. If the forklift is not holding the box, it can move through corridors and rooms. It cannot pass through a room where the box lies.

An empty forklift moves much faster than it lifts or lowers the box. So action 1 or action 2 takes one unit of time, and action 3 takes no time. For each room pp (2≤p≤n2 \le p \le n), find the minimum time the forklift needs to go from the start state (room 1, holding the box) to room pp while holding the box. If that is impossible, report it.

Input

Each test consists of several input sets. The first line contains one integer tt (1≤t≤1000001 \le t \le 100000), the number of input sets. Then the input sets follow.

The first line of each input set contains two integers nn and mm (2≤n≤5000002 \le n \le 500000, 1≤m≤5000001 \le m \le 500000), the number of rooms and corridors.

The next mm lines each contain two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), the numbers of the rooms connected by the ii-th corridor. Each pair of rooms connected by a corridor is listed exactly once. It is guaranteed that if all rooms are empty, you can reach any room from any other room by walking through corridors.

Let the sum of nn over all input sets be at most 500000, and the sum of mm be at most 500000.

Output

For each input set, print n−1n - 1 numbers. The ii-th number is the minimum number of lifts and put-downs of the box needed for the forklift to reach room i+1i + 1 while holding the box. If it is impossible, the ii-th number is −1-1.

Hint

In the fourth input set, the forklift can reach room 4 holding the box in the fastest way as follows:

  • Put the box down in room 2. This takes one unit of time.
  • Move to room 3. This takes no time.
  • Pick up the box from room 2. This takes one unit of time.
  • Put the box down in room 9. This takes one unit of time.
  • Move to room 6. This takes no time.
  • Pick up the box from room 9. This takes one unit of time.
  • Put the box down in room 5. This takes one unit of time.
  • Move to room 4. This takes no time.
  • Pick up the box from room 5. This takes one unit of time.

In total, 6 units of time are spent.

Examples1

  1. Example 1

    Input
    4
    4 4
    1 2
    2 3
    3 4
    4 1
    5 5
    1 2
    2 3
    3 4
    4 5
    5 1
    5 6
    1 2
    3 2
    1 3
    3 5
    5 4
    3 4
    9 12
    1 2
    2 3
    3 1
    4 5
    5 6
    6 4
    7 8
    8 9
    9 7
    3 6
    6 9
    9 3
    
    Expected output
    -1 2 -1
    4 2 2 4
    2 2 4 4
    2 2 6 6 4 6 6 4