#include #define rep(i,n) for(int i=0;i<(int)(n);i++) #define chmin(x,y) x = min((x),(y)); #define chmax(x,y) x = max((x),(y)); using namespace std; using ll = long long ; using P = pair ; using pll = pair; const int INF = 1e9; const long long LINF = 1e17; const int MOD = 1000000007; //const int MOD = 998244353; const double PI = 3.14159265358979323846; map memo; ll gcd(ll i,ll j){ if(j == 0) return i; return gcd(j,i%j); } ll lcm(ll i,ll j){ return i / gcd(i,j) * j; } bool finite(ll n){ while(n%2==0) n /= 2; while(n%5==0) n/= 5; return (n==1); } long long modpow(long long x,long long n) { long long ret = 1; ll t = 10; while (x > 0) { if (x & 1) ret = (ret * t) % n; t = (t * t) % n; x >>= 1; } return ret; } ll len(ll p){ if(memo.count(p) > 0) return memo[p]; if(p==2 || p==5){ memo[p] = 1; return 1; } ll q = p-1; ll res = p-1; for(ll i=1;i*i<=q;i++){ if(q%i==0){ if(modpow(i,p) == 1) chmin(res,i); if(modpow(q/i,p) == 1) chmin(res,q/i); } } memo[p] = res; return res; } ll length(ll m){ ll res = 1; for(ll i=2;i*i<=m;i++){ if(m%i==0){ ll now = len(i); m /= i; while(m%i==0){ m /= i; if(now!=2&&now!=5)now *= i; } res = lcm(res,now); } } if(m > 1){ res = lcm(res,len(m)); } return res; } void solve(){ ll n; cin >> n; if(finite(n)) cout << 1 << endl; else cout << length(n) << endl; return; } int main(){ int t; cin >> t; while(t--){ solve(); } return 0; }