#C10627. Earliest Bus Finder

    ID: 39853 Type: Default 1000ms 256MiB

Earliest Bus Finder

Earliest Bus Finder

You are given a set of bus schedules. Each bus schedule lists the arrival times of that bus at consecutive stops. Note that not every bus has the same number of stops.

For each test case, determine the bus that arrives earliest at each stop. If there is a tie in arrival times, choose the bus with the smaller identifier. The bus identifiers are 1-indexed following the order of the input.

The answer for each test case should be a single line listing pairs of bus identifier and arrival time for each stop in order, with each pair separated by a space.

Mathematical formulation:

For each stop \(i\), find \(b, t\) such that \(t = \min\{ t_{j,i} : bus~j \text{ has a time at stop } i\}\) and if there exist multiple such \(j\) then choose the smallest \(j\).

inputFormat

The first line contains an integer \(T\) representing the number of test cases. For each test case, the first line contains an integer \(N\) representing the number of buses. This is followed by \(N\) lines, one per bus. Each of these lines begins with an integer \(K\) indicating the number of stops for that bus, followed by \(K\) space-separated integers representing the arrival times at consecutive stops.

Input format:

T
N
K arrival_time[0] arrival_time[1] ... arrival_time[K-1]
... (repeat for each bus)
(repeat for each test case)

outputFormat

For each test case, output a single line containing the pairs of bus identifier and arrival time (separated by a space) for each stop in the order of stops (starting from the first stop). The pairs are concatenated in one line with a space between each pair.

## sample
2
3
2 10 20
2 15 25
3 5 10 20
2
2 30 45
3 25 50 75
3 5 3 10 3 20

2 25 1 45 2 75

</p>