Under construction

Still being built. Things may change

Santa Claus

Time limit1sMemory limit1024 MB

Summary
For each house i, find the minimum distance 2X_i - X_L Santa walks so that every elf gift can be handed to a child who accepts it.
Level

Hard9 of 10

Topics
Greedy, Segment tree, Binary search
Solved
No attempts yet

Problem

On Christmas Eve, Santa takes gifts from the elves and gives them to children, bringing joy to their hearts.

The city has NN houses along a road, numbered from 1 to NN (1≤N≤962261 ≤ N ≤ 96 226). House ii has the following data:

  • XiX_i (0≤X1≤X2≤...≤XN≤10000000000 ≤ X_1 ≤ X_2 ≤ ... ≤ X_N ≤ 1 000 000 000): the position of house ii on the road
  • HiH_i (0≤Hi≤10 ≤ H_i ≤ 1)
  • ViV_i (0≤Vi≤N0 ≤ V_i ≤ N)

If Hi=0H_i = 0, house ii has an elf holding one gift of value ViV_i. If Hi=1H_i = 1, house ii has a child waiting for one gift of minimum value ViV_i.

There are NN scenarios. In scenario ii, Santa enters the city at coordinate 0 with an empty gift bag. He first walks right to house ii at coordinate XiX_i, then walks left to some position XLi≤XiX_{L_i} ≤ X_i. Each time he passes an elf's house he has not visited before, he takes that gift and puts it in the bag. Each time he passes a child's house that has not yet received a gift, he can (but does not have to) pick one gift from the bag and hand it to the child. The chosen gift must have a value at least as large as VV, the child's minimum, and it is removed from the bag.

In scenario ii, Santa travels the distance Di=2Xi−XLiD_i = 2X_i - X_{L_i}. For each scenario, find the minimum distance DiD_i Santa must travel to give out all the elves' gifts. Some children may receive no gift, but all gifts must be distributed and no child may receive more than one. If no such DiD_i exists, set Di=−1D_i = -1. In particular, if Santa cannot reach all the elves' houses, the answer for that scenario is always −1-1.

Input

The first line contains one integer TT (1≤T≤101 ≤ T ≤ 10), the number of tests. Each test consists of four lines:

  • Line 1: the number of houses NN
  • Line 2: X1,X2,...,XNX_1, X_2, ..., X_N
  • Line 3: H1,H2,...,HNH_1, H_2, ..., H_N
  • Line 4: V1,V2,...,VNV_1, V_2, ..., V_N

The sum of NN over all tests does not exceed 500 000.

Output

For each test, output a single line with NN integers D1,D2,...,DND_1, D_2, ..., D_N.

Hint

In the first test of the first input, there are 8 houses. Houses 2, 3 and 4 each hold an elf with a gift of value 2, 3 and 3 respectively. House 5 holds a child who wants a gift of value 5. No elf has a gift that large, so that child receives nothing.

In scenarios 1, 2 and 3, Santa does not visit all the elves, so D1=D2=D3=−1D_1 = D_2 = D_3 = -1.

In scenarios 4, 5 and 6, Santa does not visit enough children who accept his 3 gifts, so D4=D5=D6=−1D_4 = D_5 = D_6 = -1.

In scenario 7, Santa must return to the first house (XL7=10X_{L_7} = 10) to give out all 3 gifts, so D7=40D_7 = 40.

In scenario 8, Santa does not need to return at all (XL8=X8=40X_{L_8} = X_8 = 40) to give out all 3 gifts, so D8=35D_8 = 35.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    -1 -1 -1 -1 -1 -1 40 35
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 36 24 31 33 37
    
  2. Example 2

    Input
    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
    
    Expected output
    -1 -1 -1 -1 -1 -1 -1 32 34
    -1 -1 -1 -1 -1 -1 -1 32 23