gets.to_i.times do s = gets.chomp b = 10 if s[0,2] == "0b" s.slice!(0,2) b = 2 elsif s[0,2] == "0o" s.slice!(0,2) b = 8 elsif s[0,2] == "0x" s.slice!(0,2) b = 16 end n = 0 s.chars.each do |c| if c >= "0" && c <= "9" n = n * b + (c.ord - "0".ord) elsif c >= "a" && c <= "z" n = n * b + (c.ord - "a".ord + 10) end end puts n end