Santa Claus

Time limit: 0.75s Memory limit: 256MB Input: Output:

It's a well known fact that in the Christmas Eve it's Santa's job to take the gifts from the elves and give them to children, bringing joy and happiness to their hearts.

In The City there are NN houses along the road, numbered from 11 to NN. For each house ii we are given the following information: Xi,Hi,ViX_i, H_i, V_i.

XiX_i is the position of the ii-th house along the road. If Hi=0H_i = 0, then in the ii-th house there is an elf with a single gift of value ViV_i. If Hi=1H_i = 1, then in the ii-th house there is a single child waiting for a single gift of minimum value ViV_i.

Task

There will be NN scenarios. In the ii-th scenario Santa enters the city at coordinate 00, carrying an empty gift bag. He first moves to the right until he reaches the ii -th house (which is located at coordinate XiX_i) and then he moves to the left until he reaches some other position XLeftiXiXLeft_i ≤ X_i. Whenever Santa passes by an elf's house which he hasn't visited before, he takes their gift and puts it into the bag. Whenever Santa passes by a child's house which hasn't already received a gift, he can (but doesn't have to!) pick one of the gifts currently in the bag and hand it to the child, removing it from the bag. This can only be accomplished if the value of the chosen gift is at least as large as the minimum value VV specified by the child.

In the ii-th scenario, Santa travels distance Di=2XiXLeftiD_i = 2X_i - XLeft_i. For each scenario it's your job to find the minimum distance DiD_i Santa has to travel to distribute all the elves' gifts to children. Note that it may be the case that some children don't receive any gift - this is acceptable as long as all the gifts are distributed and no child receives more than one gift. If no such DiD_i exists, take Di=1D_i = -1. In particular, note that this will always be the case for scenarios where Santa can't reach all the elves' houses.

Input data

The first line of input contains one integer TT - the number of tests to solve.

The description of TT tests follows. Each test consists of four lines:

The first line contains one integer NN - the number of houses in the city.

The second line contains NN integers: X1,X2,,XNX_1, X_2, \dots, X_N.

The third line contains NN integers: H1,H2,,HNH_1, H_2, \dots, H_N.

The fourth line contains the NN integers: V1,V2,,VNV_1, V_2, \dots, V_N.

Output data

For each of the TT tests output a single line containing NN integers: D1,D2,,DND_1, D_2, \dots, D_N.

Constraints and clarifications

  • 1N96 0681 \leq N \leq 96 \ 068
  • 1T101 \leq T \leq 10
  • 0X1X2XN1 000 0000 \leq X_1 \leq X_2 \leq \dots \leq X_N \leq 1 \ 000 \ 000
  • 0Hi10 \leq H_i \leq 1
  • 0ViN0 \leq V_i \leq N
  • The sum of the NN values over all the TT tests does not exceed 500 000500 \ 000.
# Points Restrictions
1 10 1N841 \leq N \leq 84
2 10 1N1691 \leq N \leq 169
3 10 1N1 3791 \leq N \leq 1 \ 379
4 10 1N2 7091 \leq N \leq 2 \ 709
5 10 1N5 5621 \leq N \leq 5 \ 562
6 10 1N13 1231 \leq N \leq 13 \ 123
7 10 1N27 5991 \leq N \leq 27 \ 599
8 10 1N41 6461 \leq N \leq 41 \ 646
9 10 1N95 0451 \leq N \leq 95 \ 045
10 10 1N96 0681 \leq N \leq 96 \ 068

Example 1

stdin

2
8
10 11 12 13 14 16 25 35
1 0 0 0 1 1 1 1
2 2 3 3 5 1 1 1
16
10 11 12 13 14 15 16 17 18 19 20 23 24 31 33 37
1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1
2 1 7 3 1 10 10 6 5 5 1 6 1 10 8 2

stdout

-1 -1 -1 -1 -1 -1 40 35
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 36 24 31 33 37

Explanation

In the first test of the first input sample there are 88 houses.

Inside houses 2,32, 3 and 44 there are 33 elves with presents of values 2,32, 3 and 33 respectively.

Inside the 55-th house there is a child expecting a present of value 55. Since Santa can't get such a present from any of the elves, that child will receive no gift.

In scenarios 1,21, 2 and 33, Santa doesn't visit all the elves, thus D1=D2=D3=1D_1 = D_2 = D_3 = -1.

In scenarios 4,54, 5 and 66, Santa does not visit enough children that would accept his 33 gifts, thus D4=D5=D6=1D_4 = D_5 = D_6 = -1.

In scenario 77, Santa needs to return to the first house (XLeft7=10)(XLeft_7 = 10) to distribute all the 33 gifts, thus D7=40D_7 = 40.

In scenario 88 , Santa does not need to return at all (XLeft8=X8=40)(XLeft_8 = X_8 = 40) to distribute all the 33 gifts, thus D8=35D_8 = 35.

Example 2

stdin

2
9
1 2 3 4 15 16 17 18 19
0 0 1 1 1 0 0 1 1
5 7 4 1 2 3 1 6 2
9
1 2 3 4 15 16 17 18 19
0 0 1 1 1 0 0 1 1
5 7 4 1 2 3 1 6 1

stdout

-1 -1 -1 -1 -1 -1 -1 32 34
-1 -1 -1 -1 -1 -1 -1 32 23

Log in or sign up to be able to send submissions!