A prime is an integer greater than whose only positive divisors are and itself. A composite is an integer greater than that is not prime. The integer is neither prime nor composite.
Define the sequences and as follows.
The first five terms of are , and the first five terms of are .
Append the first terms of to the first terms of to form a sequence of length . In other words,
Find the -th smallest number in . Since the answer may be large, output its remainder when divided by .
The input is given in the following format.
For each test case, output the remainder when the -th smallest number in is divided by , on a separate line. Output the answers in the order the test cases appear in the input.
In the first test case, , so the smallest number is .
In the second test case, . Sorting it in ascending order gives , so the third smallest number is .
The first line contains the number of test cases . Each test case is given in the following format.
A single line contains two integers and , separated by a space.
In the third test case, . Sorting it in ascending order gives , so the second smallest number is .