CF BUDDY
← Problems·

1567B · MEXor Mixup

1000 · bitmasks, greedy

Problem: Alice gave Bob two integers aa and bb (a>0a > 0 and b0b \ge 0). Being a curious boy, Bob wrote down an array of non-negative integers with MEX\operatorname{MEX} value of all elements equal to aa and XOR\operatorname{XOR} value of all elements equal to bb.

What is the shortest possible length of the array Bob wrote?

Recall that the MEX\operatorname{MEX} (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the XOR\operatorname{XOR} of an array is the bitwise XOR of all the elements of the array.

Input Format: The input consists of multiple test cases. The first line contains an integer tt (1t51041 \leq t \leq 5 \cdot 10^4) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers aa and bb (1a31051 \leq a \leq 3 \cdot 10^5; 0b31050 \leq b \leq 3 \cdot 10^5) — the MEX\operatorname{MEX} and XOR\operatorname{XOR} of the array, respectively.

Output Format: For each test case, output one (positive) integer — the length of the shortest array with MEX\operatorname{MEX} aa and XOR\operatorname{XOR} bb. We can show that such an array always exists.

Note: In the first test case, one of the shortest arrays with MEX\operatorname{MEX} 11 and XOR\operatorname{XOR} 11 is [0,2020,2021][0, 2020, 2021].

In the second test case, one of the shortest arrays with MEX\operatorname{MEX} 22 and XOR\operatorname{XOR} 11 is [0,1][0, 1].

It can be shown that these arrays are the shortest arrays possible.

Sample Cases

Case 1

Input

5
1 1
2 1
2 0
1 10000
2 10000

Output

3
2
3
2
3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback