str = STDIN.read lines = str.split(/\n/) lines.shift output = "" while true do if lines.length == 0 then break end lines.sort! output += lines[0][0] new_str = lines[0][1..lines[0].length] if new_str == "" then lines.delete_at(0) else lines[0] = new_str end end puts(output)