T=int(input()) for _ in range(T): X,M=map(int,input().split()) lx=len(str(X)) q=[-1]*10**lx for i in range(1000): j=(M*i)%(10**lx) if q[j]==-1: q[j]=M*i ans=-1 Y=X*(10**15) Y+=(-Y)%M Y+=q[(X-Y)%(10**lx)] 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)