#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

int main() {
    int T;
    cin >> T;

    while (T--) {
        ll A, B;
        cin >> A >> B;

        vector<ll> divisors;
        for (ll i = 1; i * i <= B - A; i++) {
            if ((B - A) % i == 0) {
                divisors.push_back(i);
                if (i * i != B - A) divisors.push_back((B - A) / i);
            }
        }

        if (B == A) {
            cout << 0 << endl;
            continue;
        }

        ll ans = INFL;
        for (ll d : divisors) {
            if (d >= A) {
                ans = min(ans, d - A);
            }
        }

        if (ans == INFL) {
            cout << -1 << endl;
        } else {
            cout << ans << endl;
        }
    }
}