The first line contains a single integer
(
) — the number of test cases.
Each test case consists of one line containing two integers
(
) and
(
), denoting half the total number of cards in the deck and the modulus respectively.
It is guaranteed that the sum of
over all test cases does not exceed
.
For each test case, print one integer — the total number of cards Nana can draw modulo
.
For example, if
and the values of cards from the top to the bottom are
, Nana can draw all cards by using the skill only once. The process is as follows:
Draw the card of value .
For , she guesses that the next card has a higher value and draws the card of value
.
Her guess is correct. For , she guesses that the next card has a higher value and draws the card of value
.
Her guess is correct. For , she guesses that the next card has a lower value and draws the card of value
.
Note that if the values of cards from the top to the bottom are , although her last guess is wrong, she can draw all cards too according to the skill.