import sys input = sys.stdin.readline import math def fac(x): L=int(math.sqrt(x)) FACT=dict() for i in range(2,L+2): while x%i==0: FACT[i]=FACT.get(i,0)+1 x=x//i if x!=1: FACT[x]=FACT.get(x,0)+1 return FACT def faclist(x): xr=math.ceil(math.sqrt(x)) LIST=[] for i in range(1,xr+1): if x%i==0: LIST.append(i) LIST.append(x//i) return LIST T=int(input()) for tests in range(T): N=int(input()) while N%2==0: N//=2 while N%5==0: N//=5 if N==1: print(1) continue F=fac(N) A=[1] for f in F: B=[] for i in faclist(f-1): for a in A: B.append(a*(i**F[f])) A=list(set(B)) A.sort() for a in A: if pow(10,a,N)==1: print(a) break