Turing’s Challenge

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

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 XX and NN, define the set TT as follows:

T = \left\\{ T\_i | 1 \le i \le N + 1\right\\}, where T_i=(Ni1)Xi1T\_i = \binom{N}{i-1}X^{i-1}

The goal of the challenge is to pick a set SS of maximal sum with S ⊆ \left\\{i|1 ≤ i ≤ N + 1\right\\}, such that _iST_i2(mod4)∏\_{i∈S}{T\_i} ≡ 2 \pmod{4}.

In other words, we seek a subset of terms in the binomial expansion of (1+X)N(1 + X)^N such that the product of the terms leaves a remainder of 22 when divided by 44 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=3X = 3 and N=5N = 5. The corresponding terms are T_1=1T\_1 = 1, T_2=15T\_2 = 15, T_3=90T\_3 = 90, T_4=270T\_4 = 270, T_5=405T\_5 = 405, and T_6=243T\_6 = 243.

The product, T_1T_2T_4T_5T_6=1×15×270×405×243=3985807502(mod4)T\_1T\_2T\_4T\_5T\_6 = 1 × 15 × 270 × 405 × 243 = 398580750 ≡ 2 \pmod{4}, thus the solution to this specific challenge is 1+2+4+5+6=181 + 2 + 4 + 5 + 6 = 18, since no other product of terms with a higher sum of indices is congruent to 2(mod4)2 \pmod{4}.

입력

The first input line contains a positive integer, qq (1q5001 ≤ q ≤ 500), indicating the number of queries. Each of the next qq lines will contain a pair of space-separated integers, where the first integer is XX (1X<2311 ≤ X < 2^{31}), and the second integer is NN (1N<2311 ≤ N < 2^{31}), 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 00 instead.