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 houses along the road, numbered from to . For each house we are given the following information: .
is the position of the -th house along the road. If , then in the -th house there is an elf with a single gift of value . If , then in the -th house there is a single child waiting for a single gift of minimum value .
Task
There will be scenarios. In the -th scenario Santa enters the city at coordinate , carrying an empty gift bag. He first moves to the right until he reaches the -th house (which is located at coordinate ) and then he moves to the left until he reaches some other position . 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 specified by the child.
In the -th scenario, Santa travels distance . For each scenario it's your job to find the minimum distance 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 exists, take . 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 - the number of tests to solve.
The description of tests follows. Each test consists of four lines:
The first line contains one integer - the number of houses in the city.
The second line contains integers: .
The third line contains integers: .
The fourth line contains the integers: .
Output data
For each of the tests output a single line containing integers: .
Constraints and clarifications
- The sum of the values over all the tests does not exceed .
# | Points | Restrictions |
---|---|---|
1 | 10 | |
2 | 10 | |
3 | 10 | |
4 | 10 | |
5 | 10 | |
6 | 10 | |
7 | 10 | |
8 | 10 | |
9 | 10 | |
10 | 10 |
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 houses.
Inside houses and there are elves with presents of values and respectively.
Inside the -th house there is a child expecting a present of value . Since Santa can't get such a present from any of the elves, that child will receive no gift.
In scenarios and , Santa doesn't visit all the elves, thus .
In scenarios and , Santa does not visit enough children that would accept his gifts, thus .
In scenario , Santa needs to return to the first house to distribute all the gifts, thus .
In scenario , Santa does not need to return at all to distribute all the gifts, thus .
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