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