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 sorted(set(LIST)) def calc(N): while N%2==0: N//=2 while N%5==0: N//=5 if N==1: return 1 F=fac(N) ANS=1 for f in F: for i in faclist(f-1): if pow(10,i,f)==1: xx=i break while True: if pow(10,xx,f**F[f])==1: ANS*=xx break else: xx*=f for i in faclist(ANS): if pow(10,i,N)==1: return i T=int(input()) for tests in range(T): N=int(input()) print(calc(N))