#include <stdio.h> #include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000000 #define Inf64 1000000000000000001LL int main(){ int _t; cin>>_t; rep(_,_t){ long long N,M; cin>>N>>M; long long mod = 1000000000; N %= mod; M %= mod; M *= -1; M %= mod; M += mod; M += mod; M %= mod; if(M==0){ cout<<mod / gcd(N,mod)<<endl; } else{ long long g = gcd(N,M); g = gcd(g,mod); N /= g; M /= g; mod /= g; if(gcd(N,mod)!=1)cout<<-1<<endl; else{ M *= inv_mod(N,mod); M %= mod; if(M==0)M = mod; cout<<M<<endl; } } } return 0; }