[*?A..?C].map{|id| [id, gets.split.map(&:to_i)] }.sort{|a, b| a[1][0] == b[1][0] ? a[1][1] <=> b[1][1] : b[1][0] <=> a[1][0] }.each{|d| puts d[0]}