from math import gcd for _ in range(int(input())): N, M = map(int, input().split()) N %= 10 ** 9 M %= 10 ** 9 if N == 0: if M == 0: print(0) else: print(-1) else: g = gcd(N, M) if gcd(N, 10 ** 9) > gcd(M, 10 ** 9): print(-1) else: print(-M * pow(N, -1, 10 ** 9) % (10 ** 9))