LogiQuest

434. Relative Sort Array

EasyCounting Sort

Order arr1 so its elements follow the order given in arr2 (all distinct, all present in arr1). Elements not in arr2 go at the end in ascending order.

Input format

Line 1: n Line 2: arr1 Line 3: m Line 4: arr2

Output format

The sorted arr1.

Example 1

Input

11
2 3 1 3 2 4 6 7 9 2 19
6
2 1 4 3 9 6

Output

2 2 2 1 4 3 3 9 6 7 19

Example 2

Input

6
28 6 22 8 44 17
4
22 28 8 6

Output

22 28 8 6 17 44

Constraints

1 ≤ m ≤ n ≤ 1000

Read input from standard input and print the answer to standard output. Your code is judged against 3 test cases, most of them hidden.

Loading the MyLoginn IDE…