#K79372. Merge Multiple Lists

    ID: 35294 Type: Default 1000ms 256MiB

Merge Multiple Lists

Merge Multiple Lists

You are given several lists of integers and a set of merge operations. In each operation, you are required to merge two specified lists. Specifically, for each operation given by two indices \(x\) and \(y\) (with 1-indexing), append all elements of list \(L_y\) to the end of list \(L_x\), and then clear list \(L_y\).

After processing all operations, output the final merged list by concatenating all non-empty lists in their original order.

Note: The merge operations are performed sequentially, and each list is 1-indexed.

inputFormat

The input is read from standard input (stdin) and has the following format:

m
L1
L2
...
Lm
n
x1 y1
x2 y2
...
xn yn

Where:

  • m is an integer representing the number of lists.
  • Each of the next m lines contains a list of integers separated by spaces (each list \(L_i\)).
  • n is the number of merge operations.
  • Each of the next n lines contains two integers \(x\) and \(y\), meaning that list \(L_y\) is merged into list \(L_x\) (appended to it), and then list \(L_y\) is cleared.

All indices are 1-indexed.

outputFormat

Output to standard output (stdout) a single line containing the final merged list. The elements must be printed in order and separated by a single space.

## sample
3
1 2 3 4
5 6 7
8 9
2
1 2
1 3
1 2 3 4 5 6 7 8 9