#include <bits/stdc++.h>
using namespace std;
unsigned long long x, y, z, d, t;
int main()
{
freopen("noi_online2022_math.in","r",stdin);
freopen("noi_online2022_math.out","w",stdout);
scanf("%d", &t);
for(int i = 1; i <= t; ++i)
{
cin >> x >> z;
y = z / x / sqrt(__gcd(x * x, z / x));
if(z == x * y * __gcd(x , y)) printf("%d\n", y);
else printf("-1\n");
}
return 0;
}