def ascan; gets.split.map(&:to_i);end sc = {} gets.to_i.times do no = gets.to_i m,s = ascan tags = gets.chomp.split tags.each do |tag| sc[tag] ||= 0 sc[tag] -= s end end limit = 10 sc.to_a.map(&:reverse).sort.each do |s, tag| break if limit == 0 limit -= 1 puts "#{tag} #{-s}" end