#include using namespace std; long long totient(long long n) { long long ret = n; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { ret = ret / i * (i - 1); while (n % i == 0) n /= i; } } if (n != 1) ret = ret / n * (n - 1); return ret; } long long modpow(long long x, long long p, long long mod) { long long ret = 1; while (p) { if (p & 1) ret = ret * x % mod; x = x * x % mod; p >>= 1; } return ret; } int solve(int n) { while (n % 2 == 0) n /= 2; while (n % 5 == 0) n /= 5; if (n == 1) return 1; int t = totient(n); int ret = t; for (int x = 1; x * x <= t; x++) { if (t % x == 0) { if (modpow(10, x, n) == 1) ret = min(ret, x); if (modpow(10, t/x, n) == 1) ret = min(ret, t/x); } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while (T--) { int n; cin >> n; cout << solve(n) << endl; } return 0; }