spoj#RLTOUR. Robert Langdon & Florence

Robert Langdon & Florence

 

As a token of thanks for his help, Elizabeth Sinskey has gifted Robert Langdon an amazing set of Russian dolls. The dolls have the property that if doll A has height HA and doll B has height HB, then B can fit inside A if HA>=HB.
Robert noted the heights of the dolls (two dolls can have same height), and then arranged the dolls in a line beautifully. For each doll he noted down the number of dolls that are before this doll in the line and can contain this doll. He wrote this down on another piece of paper.
Accidentally, the dolls fell out of line, and Robert wants to arrange them back beautifully. Given the two pieces of information (heights of the dolls and number of dolls before this in line that can contain this doll), can you rearrange the dolls beautifully?
INPUT:
First line contains T, number of test cases
Each test case consists of 3 lines
First line of each test case has a single number N, number of dolls.
Second line contains array H, space separated array of size N containing the heights of dolls.
Third line contains array C, space separated array of size N, C[i] indicating number of dolls before this in the beautiful arrangement that can contain doll i.
OUTPUT:
For each test case, output a single line containing the heights of dolls in order as they were when Robert arranged them beautifully.
Assume that a valid solution always exists.
EXAMPLE INPUT:
2
3
12 13 14
0 0 2
3
12 14 13
0 1 0
EXAMPLE OUTPUT:
13 14 12
13 12 14
EXPLAINATION:
For the first case, (arrangement 13,14,12) 13 and 14 cannot be contained by any doll prior to them, but 12 can be put in doll 13 as well as doll 14, hence the array 0 0 2
For the second case, (arrangement 13,12,14) 12 can be contained in 13, hence the array 0 1 0
CONSTRAINTS:
1<=T<=500
1<=N<=500
1<=H[i]<=10^9

As a token of thanks for his help, Elizabeth Sinskey has gifted Robert Langdon an amazing set of Russian dolls. The dolls have the property that if doll A has height HA and doll B has height HB, then B can fit inside A if HA>=HB.

Robert noted the heights of the dolls (two dolls can have same height), and then arranged the dolls in a line beautifully. For each doll he noted down the number of dolls that are before this doll in the line and can contain this doll. He wrote this down on another piece of paper.

Accidentally, the dolls fell out of line, and Robert wants to arrange them back beautifully. Given the two pieces of information (heights of the dolls and number of dolls before this in line that can contain this doll), can you rearrange the dolls beautifully?

 

INPUT:

First line contains T, number of test cases

Each test case consists of 3 lines

First line of each test case has a single number N, number of dolls.

Second line contains array H, space separated array of size N containing the heights of dolls.

Third line contains array C, space separated array of size N, C[i] indicating number of dolls before this in the beautiful arrangement that can contain doll i.

 

OUTPUT:

For each test case, output a single line containing the heights of dolls in order as they were when Robert arranged them beautifully.

Assume that a valid solution always exists.

 

EXAMPLE INPUT:

2

3

12 13 14

0 0 2

3

12 14 13

0 1 0

 

EXAMPLE OUTPUT:

13 14 12

13 12 14

 

EXPLAINATION:

For the first case, (arrangement 13,14,12) 13 and 14 cannot be contained by any doll prior to them, but 12 can be put in doll 13 as well as doll 14, hence the array 0 0 2

For the second case, (arrangement 13,12,14) 12 can be contained in 13, hence the array 0 1 0

 

CONSTRAINTS:

1<=T<=500

1<=N<=500

1<=H[i]<=109