import sys sys.setrecursionlimit(3*10**5) t = int(input()) b = {} for _ in range(t): n = int(input()) dic = {} now = 1 p = n for i in range(2,int(p**0.5)+1): if n == 1 or i > n: break while n%i == 0: n //= i if n == 1: print(1) continue if n in b: print(b[n]) continue while now < n: now *= 10 c = 0 ans = 1 print(now,n) while now: x,now = divmod(now,n) if now in dic: ans = c-dic[now] break dic[now] = c now *= 10 c += 1 if c >= 2*10**5: ans = p-1 break b[n] = ans print(ans)