public static void main(String[] args) { Scanner sc = new Scanner(); while (true) { int p = sc.nextInt(); if (p == 0) return; for (int i = 0; i < p; i++) { String nombre = sc.nextLine(); int numero = sc.nextInt(); actual = new Producto(nombre, numero); for (int j = 0; j < numero; j++) { String[] pedazos = sc.nextLine().split("\\s+"); String nActual = pedazos[0]; String posible = pedazos.length == 1 ? null : pedazos[1]; if (posible == null) actual.ingredientes[j] = new Ingrediente(nActual, i); else actual.ingredientes[j] = new Ingrediente( nActual, i, Integer.parseInt(posible.substring(0, posible.length() - 1))); } productos[i] = actual; for (int a = 0; a < 110; a++) for (int j = 0; j < 110; j++) for (int k = 0; k < 110; k++) dp[a][j][k] = null; posible(0, 100, 0); for (int j = 0; j < numero; j++) actual.mapa.put(actual.ingredientes[j].nombre, actual.ingredientes[j]); } int q = sc.nextInt(); TreeSet<Integer> resultado = new TreeSet<Integer>(); for (int query = 0; query < q; query++) { String cual = sc.next(); boolean most = cual.equals("most"); String ingrediente = sc.next(); ArrayList<Ingrediente> enOrden = new ArrayList<Ingrediente>(); for (int i = 0; i < p; i++) if (productos[i].mapa.containsKey(ingrediente)) enOrden.add(productos[i].mapa.get(ingrediente)); else enOrden.add(new Ingrediente(ingrediente, i, 0)); if (most) { Collections.sort(enOrden, new Menor()); Ingrediente ultimo = enOrden.get(enOrden.size() - 1); resultado.clear(); for (int i = ultimo.minimo; i <= ultimo.maximo; i++) { for (Ingrediente in : enOrden) if (in.cumple(i)) resultado.add(in.producto); } System.out.print(productos[resultado.pollFirst()].nombre); while (!resultado.isEmpty()) System.out.print(" " + productos[resultado.pollFirst()].nombre); System.out.println(); } else { Collections.sort(enOrden, new Mayor()); Ingrediente ultimo = enOrden.get(0); resultado.clear(); for (int i = ultimo.minimo; i <= ultimo.maximo; i++) { for (Ingrediente in : enOrden) if (in.cumple(i)) resultado.add(in.producto); } System.out.print(productos[resultado.pollFirst()].nombre); while (!resultado.isEmpty()) System.out.print(" " + productos[resultado.pollFirst()].nombre); System.out.println(); } } } }