def calc(xs) ys = xs.sort {|a, b| x = b[0] <=> a[0] x == 0 ? a[1] <=> b[1] : x } ys.each {|y| puts (y[2] + ?A.ord).chr } end xs = (0..2).map {|i| gets.split.map(&:to_i) + [i] } calc(xs)