Knuth was looking through some of Turing's memoirs and found a rather interesting challenge that Turing had left for one of his successors. Naturally, Knuth has slyly decided to ask you, his best student, to write a computer program to solve the challenge, but plans on taking credit for the work. Since you know that co-authoring a paper with Knuth is to computer scientists what co-authoring a paper with Erdos is to mathematicians, you've decided to take the bait. Help Knuth solve Turing's problem!
The challenge is as follows:
Given positive integer values for X and N, define the set T as follows:
T = \left\\{ T\_i | 1 \le i \le N + 1\right\\}, where T_i=(i−1N)Xi−1
The goal of the challenge is to pick a set S of maximal sum with S ⊆ \left\\{i|1 ≤ i ≤ N + 1\right\\}, such that ∏_i∈ST_i≡2(mod4).
In other words, we seek a subset of terms in the binomial expansion of (1+X)N such that the product of the terms leaves a remainder of 2 when divided by 4 and the sum of the indices of those terms is maximal.
The goal of Turing's challenge is to determine this maximal sum.
As an example, consider X=3 and N=5. The corresponding terms are T_1=1, T_2=15, T_3=90, T_4=270, T_5=405, and T_6=243.
The product, T_1T_2T_4T_5T_6=1×15×270×405×243=398580750≡2(mod4), thus the solution to this specific challenge is 1+2+4+5+6=18, since no other product of terms with a higher sum of indices is congruent to 2(mod4).
The first input line contains a positive integer, q (1≤q≤500), indicating the number of queries. Each of the next q lines will contain a pair of space-separated integers, where the first integer is X (1≤X<231), and the second integer is N (1≤N<231), for that query.
For each query, output on a line by itself, the desired maximal sum of indices. If no such subset of terms exists, output 0 instead.