High School Programming League 2008/2009

Recursive sequences

Problem code: HS09REC

Bernie wishes to impress his math teacher with a new theorem. He observes some sequences which satisfy a recursive relation

an+2=2an+1-an+2

Each sequence of his concern starts with number a1=1, but the second numbers differ. Bernie thinks he found a nice rule, which he wants to check. He thinks that no matter what the number a2 is and no matter which n he chooses, one always can find an element of the sequence which equals anan+1.

You can help him in his investigations by finding required elements.

Input

There is K (1 ≤ K ≤ 1 000) lines of standard input. Each consists of two integer numbers a2, n (2 ≤ a2 ≤ 1 000, 1 ≤ n ≤ 1 000 000 000) separated by spaces.

The line K+1 will contain two zeros, which shouldn't be processed.

Output

Write out K lines of output - one for each testcase. For each testcase the line should contain the smallest positive integer m such that am=anan+1 or the number 0 if such an m doesn't exist.

Example

Input:
2 1 2 2 2 4 3 5 0 0 Output: 2 4 14 26

Scoring

For solving this problem you will score 10 points.


Added by:Adam Dzedzej
Date:2010-01-30
Time limit:1s
Source limit:50000B
Languages:SED C99 strict C++ 4.0.0-8 C++ 4.3.2 TCL SCALA NICE NEM BASH PHP SCM guile LISP sbcl LISP clisp ERL TECS TEXT DOC PDF PS PERL 6 JS
Resource:High School Programming League (thanks to Talent Association)
SPOJ System © 2010 Sphere Research Labs. All Rights Reserved.