import sys input = sys.stdin.readline def p(x,m): ANS=0 while x%m==0: ANS+=1 x//=m return ANS T=int(input()) for tests in range(T): N,M=map(int,input().split()) N2=p(N,2) N5=p(N,5) M2=p(M,2) M5=p(M,5) if min(N2,9)<=M2 and min(N5,9)<=M5: pass else: print(-1) continue d=(2**min(N2,9))*(5**min(N5,9)) N//=d M//=d mod=10**9 mod//=d if mod==1: print(1) continue k=-M*pow(N,-1,mod)%mod if k==0: print(min(mod,M)//N) else: print(k)