#yuki-1339 T = int(input()) candidates = [2*i+1 for i in range(1, 15811)] primelist = [2] p = 3 while p**2 <= 31623: primelist.append(p) candidates = [c for c in candidates if c%p] p = candidates[0] primelist += candidates l = len(primelist) def ediv(n): """ nが奇数の時 """ t = 1 from collections import defaultdict as ddt d = ddt(int) i = 1 while i < l and primelist[i]**2 <= n: p = primelist[i] if not n%p: n //= p t *= p-1 while not n%p: n //= p d[p] += 1 i += 1 if n > 1: t *= n-1 j = 0 while primelist[j]**2 <= t: q = primelist[j] while not t%q: d[q] += 1 t //= q j += 1 if t > 1: d[t] += 1 ans = {1} for k in d.keys(): plus = set() for x in ans: for ex in range(d[k]): plus.add(x*k**(ex+1)) ans |= plus return sorted(ans) for _ in range(T): N = int(input()) #10**9以下 while not N%2: N //= 2 while not N%5: N //= 5 if N == 1: print(1) for d in ediv(N): if pow(10, d, N) == 1: print(d) break