Max Heat

 Kiteretsu goes to his Chemistry Lab to perform the perfect reaction. In his lab, he found 

N reagents numbered 1 to N. The ith reagent has two properties - Ai and Bi.

A reaction can be performed between the reagents i and j only if Aij.

If a reaction is performed between reagents i and jij amount of heat is produced. Out of this, D(BiBj) is absorbed and the rest heat is released. Here,  denotes the bitwise XOR operation.

More formally, in a reaction between reagents i and j, the heat released is given by:

  • H(i,j)=ijD(BiBj)

Find the maximum possible heat which can be released in a reaction.

Note: The input of this problem is large, so use fast input/output methods.

Input Format

  • The first line will contain T - the number of test cases. Then the test cases follow.
  • First line of each test case contains two integers N,D.
  • Second line of each test case contains N integers A1,A2,,AN.
  • Third line of each test case contains N integers B1,B2,,BN

Output Format

For each test case, print a single line, a single integer denoting the maximum possible heat that can be released in a reaction.

Constraints

  • 1T10
  • 2N106
  • 1D2000
  • (i+1)AiN  (1i<N)
  • AN=N+1
  • 1BiN
  • Sum of N over all test cases does not exceed 106.

Sample Input 1 

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

Sample Output 1 

6
-7

Explanation

Test Case 1: Let us choose i=2 and j=4. Since A24, a reaction can take place.
The heat released in this reaction is 242(32)=821=6. It can be proven that for no other reaction, the heat released is greater than 6.
Some other reactions can take place between (2,5)(3,5). Note that no reaction is possible between (3,4) as A34.

Test Case 2: The only possible reaction is between reagents 1 and 2. The heat released in this reaction is 123(12)=233=7.

Previous
Next Post »