Bubble XOR Sort#12413

Bubble XOR Sort

3 seconds
512 megabytes
Hard

You are given two arrays and consisting of positive integers.

In one operation, you can choose two adjacent elements a_i and a_{i+1} (1 \le i < n) of and swap them at the cost of a_i \oplus a_{i+1} where is the bitwise XOR operation.

You must find the minimum possible cost to make the array match . It is guaranteed that the input will be constructed in a way so it is always possible to make match by applying the mentioned operation a finite number of times.

You have to answer independent test cases.

Input

The first line contains one integer (1 \le t \le 10^{5}) — the number of test cases. Then test cases follow.

The first line of the test case contains one integer (1 \le n \le 2 \cdot 10^{5}) — the number of elements in and .

The second line of the test case contains integers a_1, a_2, \dots, a_n (1 \le a_i \le n) — the elements of array .

The third line of the test case contains integers b_1, b_2, \dots, b_n (1 \le b_i \le n) — the elements of array .

It is guaranteed that the sum of over all test cases does not exceed 2 \cdot 10^5.

Output

For each test case, print one integer — the minimum cost required to transform array into array .

Examples

Example 1

Loading
Loading

Note

In the first test case of the sample initially, a = [1,1,2] and we need to obtain b = [1,2,1].

We can swap the last two elements of at the cost of 1 \oplus 2 = 3 to make a = [1,2,1]. So, the minimum cost is .

Scoring

1. Subtask ( points): (\sum n \le 9)

2. Subtask ( points): (\sum n \le 2000)

3. Subtask ( points): (a_i \le 2000 for all 1 \le i \le n)

4. Subtask ( points): (\sum n \le 5 \cdot 10^{4})

5. Subtask ( points): (elements of are pairwise distinct)

6. Subtask ( points): No additional constraints