def factorization(n): arr = [] temp = n if temp%2 == 0: cnt = 0 while temp%2 == 0: cnt += 1 temp//=2 arr.append(2) for i in range(3, int(-(-n**0.5//1))+1,2): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append(i) if temp!=1: arr.append(temp) return arr def Euler_totient_function(n): L = factorization(n) ret = n for x in L: ret = ret*(x-1)//x return ret def make_divisors(n): lower_divisors , upper_divisors = [], [] i = 1 while i*i <= n: if n % i == 0: lower_divisors.append(i) if i != n // i: upper_divisors.append(n//i) i += 1 return lower_divisors + upper_divisors[::-1] Case = int(input()) for _ in range(Case): N = int(input()) while N%2 == 0: N //= 2 while N%5 == 0: N //= 5 #print(N) if N == 1: print(1) continue M = Euler_totient_function(N) #print(M) L = make_divisors(M) #print(L) for x in L: #print(x,pow(10,x-1) - 1%N,N) a = pow(10,x,N) - 1 #print("A",a) if a == 0: print(x) break