import math #素因数分解 #collections.Counter(prime_factorize(N))で個数を取得 #最大公約数 def lcm(x, y): return (x * y) // math.gcd(x, y) def prime_factorize(n): a = [] while n % 2 == 0: a.append(2) n //= 2 f = 3 while f * f <= n: if n % f == 0: a.append(f) n //= f else: f += 2 if n != 1: a.append(n) return a T = int(input()) for i in range(T): N = int(input()) L=list(set(prime_factorize(N))) if 2 in L: L.remove(2) if 5 in L: L.remove(5) if len(L)==0: print(1) else: rep = [] for l in L: temp = 9 % l cnt = 1 while temp!=0: temp = (temp*10+9)%l cnt+=1 rep.append(cnt) lc = 1 for r in rep: lc = lcm(lc,r) print(lc)