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