GCD2 - GCD2

Frank explained its friend Felman the algorithm of Euclides to calculate the GCD of two numbers. Then Felman implements it algorithm

int gcd(int a, int b)
{
	if (b==0)
		return a;
	else
		return gcd(b,a%b);
}
and it proposes to Frank that makes it but with a little integer and another integer that has up to 250 digits.

Your task is to help Frank programming an efficient code for the challenge of Felman.

Input

The first line of the input file contains a number representing the number of lines to follow. Each line consists of two number A and B (0 <= A <= 40000 and A <= B < 10^250).

Output

Print for each pair (A,B) in the input one integer representing the GCD of A and B.

Example

Input:
2
2 6
10 11

Output:
2
1

Source limit is 1,000 Bytes.


Added by:Frank Rafael Arteaga
Date:2008-08-04
Time limit:1s
Source limit:1000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 LISP sbcl LISP clisp ERL JAVA JS-RHINO NODEJS PERL PERL6 PHP PYTHON RUBY VB.NET
Resource:My own Resource

hide comments
2014-10-14 12:14:38 അഭി
Keep getting internal error. lang=pypy, id=12623106
2014-09-28 13:07:32 SHIVAM DIXIT
be careful of follwing test cases
0 0 ->0
0 10 ->10
10 0 ->10

Last edit: 2014-09-28 13:07:49
2014-09-18 16:58:02 ISHANI
after doing this,see this problem http://www.spoj.com/problems/GCD3/
2014-08-17 22:58:26 Rajat (1307086)
half of code is already solved in ques.....
thing is how to reduce code to that level
2014-07-25 18:35:02 rahul nagurtha
please enable python 2.7
2014-06-29 07:27:14 `Ak
1
0 0
output 0
costed me 1 runtime error(SIGFPE)
2014-06-06 20:20:12 Sumit Mishra
getting WA
<snip>

Last edit: 2022-09-16 17:22:02
2014-06-06 17:15:10 agaurav77
Here, interpreted languages do better than compiled ones. Euclid's algo works in these languages.
2014-05-20 19:52:25 pvkcse
fractions.gcd() got TLE but euclid works fine and got AC with 0.10s in python...
2014-03-25 12:08:03 zai zai
@Narendra yadav: thank you
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.