Square Graph

아직 제출이 없습니다시간 제한5초메모리 제한256 MB

문제

Prof. Elephant has a sequence a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n. He has used the sequence to generate an undirected graph GG with nn vertices labeled by 1,2,,n1, 2, \ldots, n.

For each even-length contiguous subsequence a_l,a_l+1,,a_l+2k1a\_l, a\_{l + 1}, \ldots, a\_{l + 2 k - 1}, if a_l+i1=a_l+k+i1a\_{l + i - 1} = a\_{l + k + i - 1} always holds for i=1,2,,ki = 1, 2, \ldots, k, Prof. Elephant would add kk edges to GG, where the endpoints of the ii-th edge are vertices labeled by (l+i1)(l + i - 1) and (l+k+i1)(l + k + i - 1), and its weight is w_kw\_k.

Prof. Elephant would like to know the total weight of the minimum spanning forest of GG.

입력

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (2n3×1052 \leq n \leq 3 \times 10^5).

The second line contains nn integers a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n (1a_in1 \leq a\_i \leq n).

The third line contains n2\lfloor\frac{n}{2}\rfloor integers w_1,w_2,,w_n2w\_1, w\_2, \ldots, w\_{\lfloor\frac{n}{2}\rfloor} (1w_i1091 \leq w\_i \leq 10^9).

It is guaranteed that the sum of nn in all test cases will not exceed 3×1053 \times 10^5.

출력

For each test case, output an integer denoting the total weight of the minimum spanning forest of GG.