Expansion Order

Time limit1sMemory limit128 MB

Summary
Given sets of stations and a network that starts as station 1, output the lexicographically smallest build order where each set touches the network when built, or Impossible.
Level

Medium6 of 10

Topics
Graph, Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

After choosing which set of routes to add to the transit system, the next question is the order in which to build these expansions. A new line is only useful if, at the moment it is built, it already touches the existing network. For example, extending a line to Monrovia is pointless if Monrovia is not yet connected to the rest of the system.

You are given the current network as a single starting point (station 11, which stands for the entire existing network) together with all proposed expansions. Each expansion is a new route described by the set of stations it would connect. A route may be built only if at least one of its stations already belongs to the network; once it is built, all of its stations become part of the network.

Determine an order in which to build the routes so that every route connects to the network at the time it is built, or report that no such order exists.

Input

The first line contains the number of data sets KK. Each data set has the following form:

  • The first line contains two integers nn and mm (1≤n≤5001 \le n \le 500, 1≤m≤501 \le m \le 50): nn is the total number of stations, where station 11 represents the entire current network, and mm is the number of proposed new routes.
  • The next mm lines each describe one route. Line ii lists the stations in the set Si⊆{1,…,n}S_i \subseteq \{1, \dots, n\} that route ii would connect, separated by spaces.

Output

For each data set, first print a line Data Set x:, where xx is the data set's number (starting from 11).

Then print the routes in the order in which they should be built, one route number per line (routes are numbered 11 to mm in the order they appear in the input). Each route must connect to the network at the time it is built.

If several valid orders exist, print the lexicographically smallest one: compare two orders at the first position where they differ, and prefer the order with the smaller route number at that position.

If no valid order exists, print Impossible instead of an ordering.

Separate consecutive data sets with a blank line.

Examples3

  1. Example 1

    Input
    2
    6 2
    1 2 3
    4 6 5
    8 4
    4 2
    5 4 6 3
    1 5 3 7
    7 6 8
    
    Expected output
    Data Set 1:
    Impossible
    
    Data Set 2:
    3
    2
    1
    4
    
  2. Example 2

    Input
    1
    3 1
    1 2 3
    
    Expected output
    Data Set 1:
    1
    
  3. Example 3

    Input
    1
    3 1
    2 3
    
    Expected output
    Data Set 1:
    Impossible