Under construction

Still being built. Things may change

Farmer John Actually Farms

Time limit2sMemory limit1024 MB

Summary
Find the minimum day D so that each plant's height h_i + a_i*D has exactly t_i taller plants, or report -1 if no such day exists.
Level

Hard8 of 10

Topics
Binary search, Sorting, Greedy
Solved
No attempts yet

Problem

Farmer John is growing NN (1≤N≤2⋅1051 \leq N \leq 2\cdot 10^5) asparagus plants on his farm. Some plants have genetic differences, so some grow faster than others. The initial height of the iith plant is hih_i inches, and each day the iith plant grows by aia_i inches.

FJ likes some of his plants more than others, so he wants certain plants to be taller than others. He gives you an array of distinct values t1,…,tNt_1,\dots,t_N that contains every integer from 00 to N−1N-1. He wants the iith plant to have exactly tit_i other plants that are taller than it. Find the minimum number of days needed to satisfy his request, or determine that it is impossible.

Input

The first line contains an integer TT (1≤T≤101 \leq T \leq 10), the number of test cases.

Each test case starts with a line containing an integer NN.

The second line contains NN integers hih_i (1≤hi≤1091 \leq h_i \leq 10^9), the initial height of the iith plant in inches.

The third line contains NN integers aia_i (1≤ai≤1091 \leq a_i \leq 10^9), the number of inches the iith plant grows each day.

The fourth line contains NN distinct integers tit_i.

The sum of NN over all test cases does not exceed 2⋅1052\cdot 10^5.

Output

Output TT lines, one per test case, each containing the answer. Output −1-1 if the request is impossible.

The values can be large, so use a 64-bit integer type such as long long in C/C++.

Examples2

  1. Example 1

    Input
    6
    1
    10
    1
    0
    2
    7 3
    8 10
    1 0
    2
    3 6
    10 8
    0 1
    2
    7 3
    8 9
    1 0
    2
    7 7
    8 8
    0 1
    2
    7 3
    8 8
    1 0
    
    Expected output
    0
    3
    2
    5
    -1
    -1
    
  2. Example 2

    Input
    2
    5
    7 4 1 10 12
    3 4 5 2 1
    2 1 0 3 4
    5
    4 10 12 7 1
    3 1 1 4 5
    2 4 3 1 0
    
    Expected output
    4
    7