#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll x, ll y) {
ll ans = x % y;
while (ans != 0) {
x = y;
y = ans;
ans = x % y;
}
return y;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
ll a, b;
cin >> a >> b;
ll ans = gcd(a * a, b / a);
ll t = (ll)(sqrt(ans)) + 0.5;
if (b % a != 0) {
cout << -1 << "\n";
continue;
}
if (t * t != ans) {
cout << -1 << "\n";
continue;
}
cout << (b / a) / t << "\n";
}
return 0;
}
调了好久没调出来