#include using namespace std; using LL=long long; using ULL=unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) ULL loop(){ int N; scanf("%d",&N); while(N%2==0) N/=2; while(N%5==0) N/=5; if(N==1) return 1; unordered_map X(30000); ULL p=1; rep(i,30000){ X[p]=i; p=p*10%N; if(p==1) return i+1; } p=1; ULL inv=0; rep(i,10) if((N/10*i+1)*10%N==1) inv=N/10*i+1; ULL q=1; rep(i,30000) q=q*inv%N; rep(i,40000){ p=p*q%N; if(X.count(p)) return (i+1)*30000+X[p]; } return ~0ull; } int main(){ int T; scanf("%d",&T); while(T--) printf("%llu\n",loop()); return 0; }