# 参考:http://www2r.biglobe.ne.jp/~kosanhp/math/junkan.pdf from math import gcd lcm = lambda a, b: a * b // gcd(a, b) def make_divisors(n): for i in range(1, int(n ** 0.5) + 1): if n % i == 0: yield i if i != n // i: yield n // i def factorization(n): arr = dict() temp = n for i in range(2, int(-(-n ** 0.5 // 1)) + 1): if temp % i == 0: cnt = 0 while temp % i == 0: cnt += 1 temp //= i arr[i] = cnt if temp != 1: arr[temp] = 1 if not arr and n != 1: arr[n] = 1 return arr def solve(N): while not N % 2: N //= 2 while not N % 5: N //= 5 if N == 1: return 1 res = 1 for p, idx in factorization(N).items(): l = N for d in make_divisors(p - 1): if pow(10, d, p) == 1: l = min(l, d) e = (2 if p in (3, 487) else 1) if idx > e: l *= p ** (idx - e) res = lcm(res, l) return res T = int(input()) for _ in range(T): print(solve(int(input())))