import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
while(T>0){
int m = input.nextInt();//长
int n = input.nextInt();//宽
int sum = 0;
while(m%n!=0) {
sum += (m / n) * 4 * n;
int t = m;
m = n;
n = t % n;
}
sum +=(m/n)*4*n;
System.out.println(sum);
T--;
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
while(T>0){
int m = input.nextInt();//长
int n = input.nextInt();//宽
System.out.println(digui(m,n));
T--;
}
}
public static int digui(int m,int n){
if(m%n==0){
return (m/n)*n*4;
}
return (m/n)*n*4+digui(n,m%n);
}
}