#include<stdio.h>
int gcd(int m,int n)
{
int t;
while(n)
{
t=m;
m=n;
n=t%n;
}
return m;
}
int main()
{
int n,a,b,c;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
c=2*b;
while(gcd(a,c)!=b)
c+=b;
printf("%d\n",c);
}
return 0;
}