use std::io::*; fn phi(n: usize) -> usize { let mut res = n; let mut i = 2; let mut m = n; while i * i <= m { if m % i == 0 { res -= res / i; while m % i == 0 { m /= i; } } i += 1; } if m != 1 { res -= res / m; } res } fn pow(x: usize, mut e: usize, m: usize) -> usize { let mut res = 1; let mut mul = x; while e > 0 { if e % 2 == 1 { res = res * mul % m; } mul = mul * mul % m; e >>= 1; } res } fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let t: usize = itr.next().unwrap().parse().unwrap(); for _ in 0..t { let mut n: usize = itr.next().unwrap().parse().unwrap(); while n % 2 == 0 { n /= 2; } while n % 5 == 0 { n /= 5; } if n == 1 { println!("1"); continue; } let p = phi(n); let mut i = 1; let mut ans = 1usize << 60; while i * i <= p { if p % i == 0 { if pow(10, i, n) == 1 { ans = std::cmp::min(ans, i); } if pow(10, p / i, n) == 1 { ans = std::cmp::min(ans, p / i); } } i += 1; } println!("{}", ans); } }