#CF2230D. D. Good Schedule

D. Good Schedule

D. Good Schedule

Alice and Bob decided to watch a TV series consisting of nn episodes, numbered from 11 to nn. The series will be shown on television over the next nn days. Unfortunately, they live in different cities, and the episode schedules may differ. On the ii-th day, the aia_i-th episode will be shown in Alice's city, and the bib_i-th episode in Bob's city.

They plan to select a segment of days [L,R][L, R] (1LRn1 \le L \le R \le n) to watch the series. Initially, neither of them has seen any episodes. Each day ii in this segment, the following happens:

  • if Alice has already watched episodes 1,2,,ai11, 2, \dots, a_i-1, but not aia_i, then she watches aia_i on day ii; otherwise, she watches nothing on this day;

  • if Bob has already watched episodes 1,2,,bi11, 2, \dots, b_i-1, but not bib_i, then he watches bib_i on day ii; otherwise, he watches nothing on this day. To avoid spoilers in their conversations, Alice and Bob want to choose a segment [L,R][L, R] such that on every day in this segment, one of the following holds:

  • either they both watch the same episode on that day;

  • or neither of them watches anything on that day. Help Alice and Bob calculate the number of suitable segments [L,R][L, R].

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

Each test case consists of three lines:

  • the first line contains a single integer nn (1n51051 \le n \le 5 \cdot 10^5);
  • the second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n);
  • the third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (1bin1 \le b_i \le n). Additional constraint on the input: the sum of nn over all test cases does not exceed 51055 \cdot 10^5.

For each test case, print a single integer — the number of suitable segments [L,R][L, R].

ExampleNoteIn the first example, the suitable segments are [1,1][1, 1], [1,2][1, 2], [1,3][1, 3], and [2,2][2, 2].

Input

InputThe first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

Each test case consists of three lines:

  • the first line contains a single integer nn (1n51051 \le n \le 5 \cdot 10^5);
  • the second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n);
  • the third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (1bin1 \le b_i \le n). Additional constraint on the input: the sum of nn over all test cases does not exceed 51055 \cdot 10^5.

Output

OutputFor each test case, print a single integer — the number of suitable segments [L,R][L, R].

Samples

3
3
1 2 1
1 2 1
2
2 1
2 2
4
1 2 3 1
1 2 3 4
6
1
6

Note

NoteIn the first example, the suitable segments are [1,1][1, 1], [1,2][1, 2], [1,3][1, 3], and [2,2][2, 2].