def primeFactor(N): i, n, ret, d, sq = 2, N, {}, 2, 99 while i <= sq: k = 0 while n % i == 0: n, k, ret[i] = n//i, k+1, k+1 if k > 0 or i == 97: sq = int(n**(1/2)+0.5) if i < 4: i = i * 2 - 1 else: i, d = i+d, d^6 if n > 1: ret[n] = 1 return ret def divisors(N): pf = primeFactor(N) ret = [1] for p in pf: ret_prev = ret ret = [] for i in range(pf[p]+1): for r in ret_prev: ret.append(r * (p ** i)) return sorted(ret) def calc(n): while n % 2 == 0: n //= 2 while n % 5 == 0: n //= 5 if n == 1: return 1 pf = primeFactor(n) a = 1 for p in pf: a *= (p - 1) * p ** (pf[p] - 1) for d in divisors(a): if pow(10, d, n) == 1: return d break T = int(input()) for _ in range(T): N = int(input()) print(calc(N))