#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; map factorize(ll x){ map ret; ll d, q; while(x>=4 && x%2==0){ret[2]++; x/=2;} d = 3; q = x/d; while(q>=d){ if(x%d==0){ ret[d]++; x=q; }else{ d+=2; } q=x/d; } ret[x]++; return ret; } vector divisor(ll n){ vector ret; for(ll i=1; i*i<=n; i++){ if(n%i==0){ ret.push_back(i); if(i!=n/i) ret.push_back(n/i); } } sort(ret.begin(), ret.end()); return ret; } ll mod_pow(ll x, ll n, ll mod){ ll res = 1; while(n>0){ if(n&1) res = res*x%mod; x = x*x%mod; n >>= 1; } return res; } ll calc_for_prime(ll p){ vector div = divisor(p-1); rep(i,div.size()){ ll m = div[i]; ll x = mod_pow(10, m, p); if(x == 1) return m; } return -1; } ll gcd(ll a, ll b){return (b==0?a:gcd(b,a%b));} ll lcm(ll a, ll b){return a/gcd(a,b)*b;} void solve(){ ll N; cin >> N; map res = factorize(N); if(res.size() == 2 && res[2] > 0 && res[5] > 0){ cout << 1 << endl; return; } if(res.size() == 1 && (res[2] > 0 || res[5] > 0)){ cout << 1 << endl; return; } ll ret = 1; FOR(it,res){ ll x = it->first; ret = lcm(ret, calc_for_prime(x)); } cout << ret << endl; } int main(){ int t; cin >> t; rep(i,t){ solve(); } return 0; }