T=int(input()) for _ in range(T): N,M=map(int, input().split()) p=10**9 try: t=(p-M)*pow(N,-1,p)%p print(t) except: print(-1)