#include using namespace std; using ll = long long; //10^xをnで割った余り int f(int x, int n){ int ret = 1; int p = 10%n; while(x>0){ if(x&1>0) ret = (ret*p)%n; p = (p*p)%n; x >>= 1; } return ret%n; } vector p; //nの素因数をベクトルpに格納 void prime(int n){ int num = n; for(int i = 2; i*i <=n ;i++){ int x = 0; while(num%i ==0){ num /= i; x++; } if(x >=1) p.push_back(i); } if(num!=1) p.push_back(num); } //オイラー関数(xと互いに素なx以下の自然数の個数) int Euler(int x){ prime(x); int euler = x; for(int i = 0; i < p.size(); i++) euler = euler/p[i]*(p[i]-1); return euler; } int main(){ int t; cin >> t; for(int i = 0; i < t; i++){ int n; cin >> n; while(n%2==0) n /= 2; while(n%5==0) n /= 5; if(n==1){ cout << 1 << endl; continue; } p.clear(); int m = Euler(n); int ans = m; for(int i = 1; i*i <= m; i++){ if(m%i==0){ if(f(i, n)==1) ans = min(ans, i); if(f(m/i, n)==1) ans = min(ans, m/i); } } cout << ans << endl; } return 0; }