#include<bits/stdc++.h>
using namespace std;
long long T,x,z,y,gcd,t;
bool b;
int main(){
scanf("%lld",&T);
while(T){
T--;
scanf("%lld%lld",&x,&z);
if(z%x!=0){
printf("-1\n");
continue;
}
t=z/x;
if(t==1){
printf("1\n");
continue;
}
gcd=__gcd(x*x,t);
double hh=sqrt(gcd)*1.000000;
if(hh*hh==gcd){
y=t/hh;
printf("%lld\n",y);
}
else printf("-1\n");
}
return 0;
}