#include // #include using namespace std; // using namespace atcoder; using ll = long long; using ull = unsigned long long; using P = pair; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define MOD 1000000007 ll totient(ll n){ ll res = n; for(ll i = 2;i*i <= n;i++){ if(n%i)continue; res /= i; res *= (i-1); while(n%i == 0)n /= i; } if(n != 1)res = res/n*(n-1); return res; } ll modpow(ll n,int r,int mod){ ll res = 1; while(r){ if(r & 1)res = res*n%mod; n = n*n%mod; r >>= 1; } return res; } int main(){ int T; cin >> T; while(T--){ ll n; cin >> n; while(n%2 == 0)n /= 2; while(n%5 == 0)n /= 5; ll res = 1ll*MOD*MOD; if(n == 1)res = 1; ll p = totient(n); for(ll i = 1;i*i <= p;i++){ if(p%i)continue; if(modpow(10,i,n) == 1)res = min(res,i); if(modpow(10,p/i,n) == 1)res = min(res,p/i); } cout << res << "\n"; } return 0; }