#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) vector divisors(ll n) { vector res; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ll j = n / i; res.emplace_back(i); if (i != j) { res.emplace_back(j); } } } return res; } const ll INF = 1e18; void solve() { ll a, b; cin >> a >> b; ll c = b - a; ll ans = INF; for (const ll d : divisors(c)) { if (a <= d) ans = min(ans, d - a); } if (ans == INF) ans = -1; cout << ans << '\n'; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); int T = 1; cin >> T; for (int t = 0; t < T; t++) { solve(); } return 0; }