#P7747. [2019年杭电多校]Rikka with Stable Marriage

[2019年杭电多校]Rikka with Stable Marriage

Rikka with Stable Marriage

Problem Description

People in love always feel humble. Sometimes, Rikka is worried about whether she deserves love from Yuta. Stable marriage problem is an interesting theoretical model which has a strong connection with the real world. Given nn men and nn women, where each person has ranked all members of the opposite sex in order of preference. We use a permutation pp of length nn to represent a match that the iith man gets married with the pip_ith woman. A match is stable if and only if there are no two people of opposite sexes who would both rather have each other than their current partners, i.e., $\forall i \neq j, \neg (r_i(p_j,p_i) \wedge r_{p_j}(i,j))$ where ra(b,c)r_a(b,c) represents whether person aa loves bb more than cc. Rikka wants to resolve the confusion in her mind by considering a special case of the stable marriage problem. Rikka uses a feature integer to represents a person's character, and for two persons with feature integers equal to xx and yy, Rikka defines the suitable index of them as xyx \oplus y, where \oplus represents binary exclusive-or. Given nn men with feature integers a1,,ana_1, \dots, a_n and nn women with feature integers b1,,bnb_1, \dots, b_n. For the iith man, he loves the jjth woman more than the kkth woman if and only if aibj>aibka_i \oplus b_j > a_i \oplus b_k; for the iith woman, she loves the jjth man more than the kkth man if and only if biaj>biakb_i \oplus a_j > b_i \oplus a_k. Rikka wants to calculate the best stable match for this problem, i.e., let P\mathbb P be the set of all stable match, she wants to calculate $\max_{p \in \mathbb P} \left(\sum_{i=1}^n \left(a_i \oplus b_{p_i} \right) \right)$. Since nn is quite large, this problem is too difficult for Rikka, could you please help her find the answer?

Input

The first line of the input contains a single integer T(1T50)T(1 \leq T \leq 50), the number of test cases. For each test case, the fisrt line contains a sigle integer n(1n105)n(1 \leq n \leq 10^5). The second line contains nn integers a1,,an(1ai109)a_1, \dots, a_n(1 \leq a_i \leq 10^9) which represents the feature number of each man. The third line contains nn integers b1,,bn(1bi109)b_1, \dots, b_n(1 \leq b_i \leq 10^9) which represents the feature number of each woman. The input guarantees that there are no more than 55 test cases with n>104n > 10^4, and for any i,j[1,n],iji,j \in [1,n], i \neq j, aiaja_i \neq a_j and bibjb_i \neq b_j.

Output

For each test case, output a single line with a single integer, the value of the best stable match. If there is no stable match, output 1-1. Hint In the first test case, one of the best matches is (2,1,4,3)(2,1,4,3). Therefore the answer is $(1 \oplus 2) + (2 \oplus 1) + (3 \oplus 4) + (4 \oplus 3) = 20$.

Sample Input

2

4

1 2 3 4

1 2 3 4

5

10 20 30 40 50

15 25 35 45 55

Sample Output

20

289

Source

2019 Multi-University Training Contest 9