#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int MOD=998244353;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
void solve();
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int test_case;
  cin>>test_case;
  while (test_case--){
    solve();
  }
}
void solve(){
  int A,B;
  cin>>A>>B;
  if(B%A==0){
    cout<<"0\n";
    return;
  }
  int C=B-A;
  int X=C+7;
  for(int i=1;i*i<=C;i++){
    if(C%i==0){
      if(i>=A)X=min(X,i-A);
      if(C/i>=A)X=min(X,C/i-A);
    }
  }
  if(X>C)X=-1;
  cout<<X<<'\n';
}