The module of GCD.
No recursion, or stack over.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
while (n-- > 0) {
s = br.readLine().split(" ");
long a = Long.parseLong(s[0]);
long b = Long.parseLong(s[1]);
pw.println(getGcd(a, b));
}
pw.flush();
br.close();
}
public static long getGcd(long a, long b) {
while (a % b != 0) {
long temp = a % b;
a = b;
b = temp;
}
return b;
}
}