Problem A
Ghost Leg

A Ghost Leg is a method for representing permutations.
A Ghost Leg can be represented as a set of vertical lines, each line corresponding to one element of the set being permuted. Horizontal lines (like the rungs of a ladder) connect adjacent vertical lines in such a way that no two rungs share an endpoint. An example is shown in the illustration to the right.
To determine the resulting position of any element under a Ghost Leg permutation, begin at the top of the vertical line corresponding to that element. Trace a path down the line until encountering the first rung touching that line. Follow that rung to the adjacent vertical line. It does not matter whether the rung goes to the left or to the right, just follow it. Continue downward, following rungs, until reaching the bottom. This gives the final position of that element under the permutation.
In the example, element
Given a description of a Ghost Leg, determine the result of the permutation. In other words, apply the specified permutation, and output the elements in their permuted order.
Input
The first line of input contains two positive integers
Each of the next
Output
Output
Sample Input 1 | Sample Output 1 |
---|---|
4 5 1 2 1 3 2 |
3 4 2 1 |
Sample Input 2 | Sample Output 2 |
---|---|
7 6 6 4 2 1 3 5 |
3 1 5 2 7 4 6 |