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.


hide comments
Davit Safrastyan: 2016-12-10 18:08:52

Find the solution of this problem in xoptutorials.com/spoj.php

flyingduchman_: 2016-11-20 17:19:29

CLRS textbook and Theorem 31.9 states that,
gcd(b,a) = gcd(a , b%a) , so you reduce the big string by (b%a) using pen-paper division method. Then call gcd(a, reduced b) and get AC.
pen-paper division method: find 934/7 by hand on paper and see how you find remainder.

rmaurya: 2016-10-27 19:33:04

no JAVA..:(

sandeep_4141: 2016-10-26 19:55:49

really take care when a==0,cost 1 WA ..but finally AC!

sonali9696: 2016-07-08 22:18:19

simple high school mathematics :)

suraj: 2016-06-28 09:27:43

take care when a=0

gohanssj9: 2016-05-11 09:49:49

Be careful of a=0 value. And also remove unnecessary details from your code,because I got an error when I tried to submit a code with unnecessary details. After removing those things, it ran successfully.

hareesh: 2016-04-29 13:26:57

Good one.... Do it in C/C++

try2catch: 2016-03-25 10:28:54

You know! I was compromising time with Java for many questions.
Now it was my turn to take some advantage, but you ruined it. (If u know wht I mean )

manas0008: 2016-01-26 05:44:10

Be careful when a==0.
Got a SIGFPE due to that.


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