Balada lu' State

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

Cool dude from Paris
By enemies undefeated
A name spoken with respect
Of a smart guy.

Task

Let AA and BB be two arrays of length NN. An interval [l,r][l, r] is considered good if min(Bl,,Br)max(Al,,Ar)max(Bl,,Br)\min(B_l, \dots, B_r) \leq \max(A_l, \dots, A_r) \leq \max(B_l, \dots, B_r).

If SS is the set of good intervals, compute [l,r]Smax(Bl,Bl+1,,Br)\displaystyle \sum_{[l,r] \in S} \max(B_l, B_{l+1}, \dots, B_r).

Input

The first line contains tt, the number of test cases. The first line of each test case contains NN, the number of elements in the two arrays. The next line contains the elements of array AA, followed on the last line by the elements of array BB.

Output

Print tt numbers, the required sums.

Constraints and notes

  • 1t1001 \leq t \leq 100
  • 1N1051 \leq N \leq 10^5
  • 1Ai,Bi1091 \leq A_i, B_i \leq 10^9
  • It is guaranteed that the sum of all NN values across all test cases is 105\leq 10^5.

Example

stdin

1
11
2 3 1 6 2 1 6 2 4 2 2
4 3 1 5 2 1 8 5 2 1 2

stdout

311

Explanation

Two of the good intervals are [1,7][1, 7] and [8,11][8, 11].

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