J=lambda:map(int,input().split()) T,*_=J() for _ in[0]*T: N,M=J();m,s=10**9,1 for p in[2,5]: while m%p<1: if N%p:break elif M%p:s=0;break else:N//=p;M//=p;m//=p n=-pow(N,-1,m)*M%m if s else-1 print([n,m][n<1and s])