Under construction

Still being built. Things may change

Turing's Challenge

Time limit1sMemory limit1024 MB

Summary
Given X and N, pick indices i from 1 to N+1 so the product of the terms C(N, i-1) X^(i-1) is 2 mod 4, and maximize the index sum.
Level

Medium7 of 10

Topics
Number theory, Bit manipulation, Greedy
Solved
No attempts yet

Problem

Knuth was reading through some of Turing's memoirs and found an interesting challenge that Turing had left for one of his successors. Knuth asked you, his best student, to write a program that solves it, but he plans to take the credit for the work. Co-authoring a paper with Knuth is to computer scientists what co-authoring a paper with Erdos is to mathematicians, so you accept the task. Help Knuth solve Turing's problem.

Given positive integers XX and NN, define the set TT as follows: T={T_i∣1≤i≤N+1}T = \left\{ T\_i | 1 \le i \le N + 1\right\}, where T_i=(Ni−1)Xi−1T\_i = \binom{N}{i-1}X^{i-1}.

The goal is to pick a set SS with S⊆{i∣1≤i≤N+1}S \subseteq \{i|1 \le i \le N + 1\} such that ∏_i∈ST_i≡2(mod4)\prod\_{i\in S}{T\_i} \equiv 2 \pmod{4}, and the sum of the elements of SS is as large as possible. In other words, you choose terms from the binomial expansion of (1+X)N(1 + X)^N so that their product leaves remainder 2 when divided by 4, and the sum of their indices is maximal.

Find that maximal sum.

Input

The first line contains a positive integer qq (1≤q≤5001 \le q \le 500), the number of queries. Each of the next qq lines contains two space-separated integers XX and NN (1≤X<2311 \le X < 2^{31}, 1≤N<2311 \le N < 2^{31}).

Output

For each query, print the maximal sum of indices on its own line. If no such subset exists, print 00.

Examples1

  1. Example 1

    Input
    3
    3 5
    1 4
    4 6
    
    Expected output
    18
    9
    0