#include <bits/stdc++.h> using namespace std; long long eazymod(long long a,long long m){a %= m; if(a < 0) a += m; return a;} pair<long long,long long> invgcd(long long a,long long b){ //return {gcd(a,b),x} (xa≡g(mod b)) a = eazymod(a,b); if(a == 0) return {b,0}; long long x = 0,y = 1,memob = b; while(a){ long long q = b/a; b -= a*q; swap(x,y); y -= q*x; swap(a,b); } if(x < 0) x += memob/b; return {b,x}; } pair<long long,long long> CRT(vector<long long> A,vector<long long> M){ int n = A.size(); //0 <= Ai < Mi,|A| = |M| long long x = 0,lcm = 1; for(int i=0; i<n; i++){ long long a = A.at(i)%M.at(i),m = M.at(i); if(lcm < m) swap(lcm,m),swap(x,a); if(lcm%m == 0){ if(x%m != a) return {-1,-1}; continue; } auto[g,in] = invgcd(lcm,m); if((a-x)%g) return {-1,-1}; long long u1 = m/g; long long y = (a-x)/g %u1 * in %u1; x += y*lcm; lcm *= u1; if(x < 0) x += lcm; } return {x,lcm}; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; long long Mod = 1'000'000'000; while(T--){ long long N,M; cin >> N >> M; N %= Mod; M %= Mod; if(N == 0){ if(M == 0) cout << 1 << "\n"; else cout << "-1\n"; continue; } long long g = gcd(Mod,N); if(M%g){cout << "-1\n"; continue;} if(M == 0){cout << Mod/g << "\n"; continue;} int time2 = 9,time5 = 9; while(g%2 == 0) time2--,g /= 2,N /= 2,M /= 2; while(g%5 == 0) time5--,g /= 5,N /= 5,M /= 5; long long two = 0,five = 0,mod2 = 1,mod5 = 1; { vector<long long> OK = {0,1}; for(int i=0; i<time2; i++){ mod2 *= 2; long long n = N%mod2,m = M%mod2; for(int k=0; k<2; k++){ long long v = OK.at(k); if((n*v+m)%mod2 == 0){ OK.clear(); for(int l=0; l<2; l++) OK.push_back(v+mod2*l); break; } } } two = OK.at(0); } { vector<long long> OK = {0,1,2,3,4}; for(int i=0; i<time5; i++){ mod5 *= 5; long long n = N%mod5,m = M%mod5; for(int k=0; k<5; k++){ long long v = OK.at(k); if((n*v+m)%mod5 == 0){ OK.clear(); for(int l=0; l<5; l++) OK.push_back(v+mod5*l); break; } } } five = OK.at(0); } auto [ans,ign] = CRT({two,five},{mod2,mod5}); cout << ans << "\n"; } }