import java.io.*; import java.util.*; public class Maarten { public static void main(String[] args) throws IOException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { new Maarten().run(reader); } } public void run(BufferedReader reader) throws IOException { int n = Integer.parseInt(reader.readLine()); Recipe[] recipes = new Recipe[n]; for (int i = 0; i < n; i++) { recipes[i] = new Recipe(reader); } Arrays.sort(recipes); for (Recipe recipe : recipes) { System.out.println(recipe.name); } } static class Recipe implements Comparable { String name; int s; double accessibility; public Recipe(BufferedReader reader) throws IOException { String[] firstLine = reader.readLine().split(" "); name = firstLine[0]; s = Integer.parseInt(firstLine[1]); HashMap end = new HashMap<>(); int sumT = 0; for (int i = 0; i < s; i++) { String[] line = reader.readLine().split(" "); String step = line[0]; int t = Integer.parseInt(line[1]); sumT += t; int d = Integer.parseInt(line[2]); int depEnd = 0; for (int j = 3; j < 3 + d; j++) { depEnd = Integer.max(depEnd, end.get(line[j])); } end.put(step, t + depEnd); } accessibility = (double) sumT / (double) end.values().stream().mapToInt(i -> i).max().getAsInt(); } @Override public int compareTo(Recipe other) { return Double.compare(this.accessibility, other.accessibility); } } }