T=int(input()) for _ in range(T): X,M=map(int,input().split()) q=[-1]*1000 for i in range(1000): j=(M*i)%1000 if q[j]==-1: q[j]=M*i ans=-1 for i in range(20): Y=X*(10**i) Y+=(-Y)%M Y+=q[(X-Y)%1000] lx=len(str(X)) ly=len(str(Y)) if Y%M==0 and lx<=ly<=18 and str(Y)[:lx]==str(Y)[-lx:]==str(X): ans=Y for i in range(20): Y=(X-1)*(10**i) Y+=(-Y)%M Y+=q[(X-Y)%1000] lx=len(str(X)) ly=len(str(Y)) if Y%M==0 and lx<=ly<=18 and str(Y)[:lx]==str(Y)[-lx:]==str(X): ans=Y print(ans)