#include <iostream>
#include <cmath>
#include <algorithm>
#define int unsigned long long
using namespace std;
int t,x,z;
signed main(){
cin>>t;
while(t--){
cin>>x>>z;
if(z%x) cout<<"-1\n";
long double tmp1=__gcd(z/x,x*x);
int tmp=tmp1;
int t=sqrt(tmp)+0.5;
if(t*t!=tmp) cout<<"-1\n";
else cout<<(z/x)/t<<endl;
}
return 0;
}