import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random def extended_euclid(a, b): if b == 0: return (1, 0) else: xd, yd = extended_euclid(b, a % b) return (yd, xd - a // b * yd) input = sys.stdin.readline #sys.setrecursionlimit(10**9) t = int(input()) #alist = list(map(int,input().split())) #alist = [] #s = input() #n,m = map(int,input().split()) #for i in range(n): # alist.append(list(map(int,input().split()))) for i in range(t): n,m = map(int,input().split()) n %= 1000000000 m %= 1000000000 if n % 2 == 0 or n % 5 == 0: print(-1) continue x = extended_euclid(n,1000000000)[0] print(x*(-m) % 1000000000)