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