import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.HashMap; import java.util.Comparator; import java.util.Collections; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, Scanner in, PrintWriter out) { HashMap tags = new HashMap<>(); int n = in.nextInt(); for (int i = 0; i < n; i++) { in.next(); int m = in.nextInt(); int s = in.nextInt(); for (int j = 0; j < m; j++) { String t = in.next(); tags.put(t, s + tags.getOrDefault(t, 0)); } } ArrayList> entries = new ArrayList>(tags.entrySet()); Collections.sort(entries, new Comparator>() { public int compare(Map.Entry o1, Map.Entry o2) { Integer val1 = o1.getValue(); Integer val2 = o2.getValue(); if (val1.equals(val2)) return o1.getKey().compareTo(o2.getKey()); else return val2.compareTo(val1); } }); int count = Math.min(tags.size(), 10); for (int i = 0; i < count; i++) { out.println(entries.get(i).getKey() + " " + entries.get(i).getValue()); } } } }