#include using namespace std; // 型定義 typedef long long ll; typedef pair P; // forループ #define REP(i,n) for(ll i=0; i<(ll)(n); ++i) // 定数宣言 const int INF = 1e9; const int MOD = 1e9+7; const ll LINF = 1e18; // グラフ表現 using Graph = vector>; // グラフの辺表現 using Edge = map,int>; // n次元配列の初期化。第2引数の型のサイズごとに初期化していく。 template void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } // 最大公約数 ll gcd(ll a,ll b){ if (a%b == 0) return(b); else return(gcd(b, a%b)); } // 最小公倍数 ll lcm(ll a, ll b){ return a/gcd(a, b) * b; } map prime_factor(ll n) { map ret; for(ll i = 2; i * i <= n; i++) { while(n % i == 0) { ret[i]++; n /= i; } } if(n != 1) ret[n] = 1; return ret; } ll Euler(ll n){ ll res = n; for(auto p : prime_factor(n)) { res *= (p.first - 1); res /= p.first; } return res; } // 約数の全列挙 vector divisor(ll n) { vector ret; for(ll i = 1; i * i <= n; i++) { if(n % i == 0) { ret.push_back(i); if(i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } ll modpow(ll a, ll n, ll mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { cout << fixed << setprecision(15); ll T; cin >> T; REP(t, T){ ll N; cin >> N; while(N%2 == 0){ N /= 2; } while(N%5 == 0){ N /= 5; } ll K = Euler(N); // cout << "K:" << K << endl; vector D = divisor(K); if(N == 1 || N == 3){ cout << 1 << endl; continue; } for(auto d: D){ if(d==1) continue; // cout << d << endl; // cout << "mod:" << modpow(10, d+1 , N)<< endl; if(modpow(10, d, N) % N == 1){ cout << d << endl; break; } } } return 0; }