«Exclusive or» Strikes Back
Time limit2sMemory limit512 MB
For each query with a and n up to 1e18, find the smallest non-negative b such that a xor b is divisible by n.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Number theory, Greedy, Math
- Solved
- No attempts yet
Problem
Two non-negative integers and are given. Find the smallest non-negative integer such that is divisible by .
Here denotes the bitwise «exclusive or» operation and corresponds to the «xor» operation in Pascal or to «\char 94» in other languages. To compute the bitwise «exclusive or» of two numbers and , write each of them in binary, padding with leading zeros on the left if needed. The result in each position is 1 if exactly one of the numbers has a 1 in the corresponding position. For example, for and the result is 22.

Input
The first line of input contains the number , the number of test cases (). The next lines contain the descriptions of the test cases. Each description consists of two numbers and separated by a space ().
Output
For each test case, output the required as a single number.