#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; 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; } int phi(int n) { int res = n; for(int i = 2; i*i <= n; i++) { if (n % i == 0) { res -= res / i; while(n % i == 0) n /= i; } } if (n > 1) // n is prime res -= res / n; return res; } 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; } void solve(){ ll N; cin >> N; while(N%2==0) N/=2; while(N%5==0) N/=5; vector div = divisor(phi(N)); rep(i,div.size()){ ll x = div[i]; if(mod_pow(10, x, N) == 1){ cout << x << endl; return; } } } int main(){ int t; cin >> t; rep(i,t){ solve(); } return 0; }