class Team
  include Comparable
  attr_reader :id
  def initialize(name, win, lose, drow, i)
    @name = name
    @win = win.to_i
    @drow = drow.to_i
    @id = i
  end
  def to_i
    return 3 * @win + @drow
  end
  def <=>(other)
    if (other.to_i == to_i)
      return @id <=> other.id
    else
      return other.to_i <=> to_i
    end
  end
  def to_s
    return "#{@name},#{to_i}"
  end
end
while (i = gets.to_i) != 0
  arr = Array.new(i){|id|Team.new(*gets.split(" "), id)}
  puts arr.sort.map(&:to_s).join("\n")
end