class SPFPrime def initialize(max) @spf = (0..max).to_a sqmax = Math.sqrt(max).ceil 4.step(sqmax, 2) { |i| @spf[i] = 2 } 3.step(sqmax, 2) { |i| (i*i).step(max, i) { |j| @spf[j] = i if @spf[j] == j } if @spf[i] == i } end def prime?(n); @spf[n] == n; end def each; (2..max).each { |i| yield i if @spf[i] == i }; end def factorize(n); fs = []; while n > 1; f = @spf[n]; fs << f; n /= f; end; fs; end end T = gets.to_i testcases = Array.new(T) { n = gets.to_i n /= 2 while n % 2 == 0 n /= 5 while n % 5 == 0 n } prime = SPFPrime.new testcases.max testcases.each do |n| len = 1 prime.factorize(n).tally.each do |(p, e)| count = 1 repnov = 9 % p until repnov == 0 repnov = (repnov * 10 + 9) % p count += 1 end count *= p ** (e - 1) len = len.lcm(count) end puts len end