def to_int(c) return c.to_i if c =~ /[0-9]/ 'abcdefghijklmnopqrstuvwxyz'.index(c.downcase) + 10 end def calc(ns) 2.upto(36) do |max_n| if ns.all? { |n| n < max_n } r = 0 ns.each_with_index do |n, i| if i == 0 r += n else r += (max_n ** i) * n end end return r end end end ans = [] gets.to_i.times do ns = gets.strip.chars.reverse.map { |c| to_int(c) } ans << calc(ns) end puts ans.min