Queue Rearrangement

Time limit1sMemory limit128 MB

Problem

A queue holds several named items, and you are given a list of queue operations. Perform every operation and then report the items remaining in the queue.

Positions are numbered starting from 1: the first item is at position 1, the second at position 2, and so on. Each operation is written as

start positiondestination position

and it moves the item currently at the start position to the destination position.

For example, suppose the queue contains

Item1 Item2 Item3 Item4 Item5

Applying the operation 5 → 2 moves Item5 into position 2, and the queue becomes

Item1 Item5 Item2 Item3 Item4

Several operations may also be applied at the same time. For instance, if the queue is

Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8

and the operations

2 → 6, 6 → 3, 4 → 5, 5 → 2, 7 → 4, 8 → 1

are applied together, the queue becomes

Item8 Item5 Item6 Item7 Item4 Item2 Item1 Item3

Every operation refers to positions in the original queue. An item that is not the start of any operation keeps its relative order and slides into one of the positions left empty (the destination positions that no operation targets). It is guaranteed that no two operations share the same start position, and that no two operations share the same destination position.

Input

The first line contains the number of test cases.

For each test case, the first line contains two integers $m$ and $n$: the number of items and the number of queue operations ($1 \le m, n \le 20$). The second line lists the $m$ item names in their current queue order; each name is made of letters and digits, is at most 8 characters long, and no two items in the same test case have the same name. Each of the next $n$ lines describes one operation as two integers, the start position and the destination position.

Output

For each test case, print the items remaining in the queue after all of its operations have been performed, in order and separated by single spaces, on one line.