Doraemon and His Pocket of wonder!

3.5

6 votes
Problem

Doraemon is planning to clean his pocket and arrange all his gadgets according to a particular order so that he can find the gadgets easily whenever Nobita cries for help!He wasn't able to find his "Sort_Everything" gadget so he asks for your help. He will give you points in return to help you secure rank in the contest.

Doraemon

He will give you two arrays as input. First array contains the ID number of all his gadgets.The second array contains the order in which he wants you to arrange some of his favourite gadgets in the beginning. Then add the remaining gadgets in the sorted order of their ID.  

It is possible that gadget ID present in second array might not be present in first array. First array can contain duplicate gadet IDs. no duplicates in the second array is allowed. 

Do this for T testcases.

Input Format: 

First line contains number of testcases.

Each test case is of 4 lines.

- First line contains the size of the array N

- Second line contains the first array A1 of size N .

- Third line contains the size of the array M

- Fourth line contains the second array, A2 of size M.

Output Format:

It consists of T lines. One line represents the sorted order of the pocket for that test case.

Constraints:

0<T<5

0<N,M<=10^5

0<A1[i], A2[i]<=10^9

Sample Input
2
6
5 3 2 15 10 7
2
7 2
10
3 3 1 1 8 9 4 5 10 2
4
3 6 9 1
Sample Output
7 2 3 5 10 15
3 3 9 1 1 2 4 5 8 10
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Explanation: 

Test Case 1:

Here, 7,2 should be placed in this same order at the beginning then remaining in the sorted order

Test Case 2:

Here, only thing to note is that ID 6 is not currently present in Doraemon's pocket. Else all is same.

Editor Image

?