Under construction

Still being built. Things may change

If I Could Turn Back Time

Time limit2sMemory limit2048 MB

Summary
Given current heights h and an earlier record p, find the fewest erosion years that turn p into h, or -1 if the model cannot do it.
Level

Medium6 of 10

Topics
Array, Implementation
Solved
No attempts yet

Problem

Inna is an avid hiker. She is visiting a range of nn mountains with heights h1,h2,…,hnh_1, h_2, \ldots, h_n.

At a nearby shop, Inna found a book stating that at some point in the past, the mountains had heights p1,p2,…,pnp_1, p_2, \ldots, p_n, in the same order. The book does not say how old it is.

The book also describes an erosion model that makes the mountains shorter year after year. Each year, the weather sets a height threshold xx. Every mountain whose current height is at least xx decreases by exactly 11. Different years can use different values of xx.

Inna wants to know how old the book is and whether the model is sound. Find the smallest number of years in which erosion could take the mountains from heights p1,…,pnp_1, \ldots, p_n to heights h1,…,hnh_1, \ldots, h_n, in the same order, or determine that the model cannot do this.

Input

The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). Each test case follows.

The first line of a test case contains nn (1≤n≤1051 \le n \le 10^5), the number of mountains.

The second line contains nn integers h1,h2,…,hnh_1, h_2, \ldots, h_n (1≤hi≤1061 \le h_i \le 10^6), the current heights.

The third line contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤1061 \le p_i \le 10^6), the heights in the same order at some point in the past.

The sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, print the smallest number of years in which erosion could take the mountains from p1,…,pnp_1, \ldots, p_n to h1,…,hnh_1, \ldots, h_n. If the model is unsound, print −1-1 instead.

Hint

In the first test case, the heights can go from (5,3,6,2)(5, 3, 6, 2) to (3,2,4,2)(3, 2, 4, 2) in just two years.

  • In the first year, let x=4x = 4. Afterward, the heights are (4,3,5,2)(4, 3, 5, 2).
  • In the second year, let x=3x = 3. Afterward, the heights are (3,2,4,2)(3, 2, 4, 2).

Examples1

  1. Example 1

    Input
    4
    4
    3 2 4 2
    5 3 6 2
    1
    10
    100000
    5
    1 2 3 4 5
    1 2 3 4 5
    3
    1 4 6
    4 1 8
    
    Expected output
    2
    99990
    0
    -1