import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; import static java.util.Comparator.*; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in)); int element = Integer.valueOf(inReader.readLine()); Map tagScore = new HashMap<>(); for (int i = 0; i < element; i++) { int imageNo = Integer.valueOf(inReader.readLine()); String[] inputs = inReader.readLine().split(" "); int tagCount = Integer.valueOf(inputs[0]); int score = Integer.valueOf(inputs[1]); String tags[] = inReader.readLine().split(" "); for (String tag : tags) { Integer now = tagScore.get(tag); if (now == null) now = 0; tagScore.put(tag, now + score); } } List> sorted = tagScore .entrySet() .stream() .sorted(comparing(Entry::getValue).reversed() .thenComparing(Entry::getKey)) .collect(Collectors.toList()); int count = 0; for (Entry entry : sorted) { System.out.println(entry.getKey() + " " + entry.getValue()); count++; if (10 <= count) break; } } }