from math import gcd def lcm(X): r=X[0] for i in range(len(X)): r=r*X[i]//gcd(r,X[i]) return r def f(x): r=[] for i in range(2,int(x**0.5)+1): if x%i==0: r.append([i,0]) while x%i==0: x//=i r[-1][1]+=1 if x>1: r.append([x,1]) return r def d(x): r=[] for i in range(1,int(x**0.5)+1): if x%i==0: r.append(i) if i*i!=x: r.append(x//i) r.sort() return r def l(x): if len(x)==1: if x[0][0]==2: if x[0][1]<=2: return x[0][1] else: return 2**(x[0][1]-2) else: return (x[0][0]**(x[0][1]-1))*(x[0][0]-1) return lcm([l([x[i]]) for i in range(len(x))]) def solve(): N=int(input()) while N%2==0: N//=2 while N%5==0: N//=5 if N==1: print(1) return 0 X=d(l(f(N))) for j in range(len(X)): if pow(10,X[j],N)==1: print(X[j]) return 0 for i in range(int(input())): solve()