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