This page is still under construction.

Parts of this page are still being built. What you see may change.

«Exclusive or» Strikes Back

Time limit2sMemory limit512 MB

Summary
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 aa and nn are given. Find the smallest non-negative integer bb such that a⊕ba \oplus b is divisible by nn.

Here ⊕\oplus 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 xx and yy, 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 x=12x=12 and y=26y=26 the result is 22.

Input

The first line of input contains the number tt, the number of test cases (1≤t≤1041 \le t \le 10^4). The next tt lines contain the descriptions of the test cases. Each description consists of two numbers aa and nn separated by a space (1≤a,n≤10181 \le a, n \le 10^{18}).

Output

For each test case, output the required bb as a single number.

Examples1

  1. Example 1

    Input
    3
    10 5
    3 2
    98 100
    
    Expected output
    0
    1
    6