import java.util.*; class family_tree { private static final Map> children = new HashMap>(); private static final Map> parents = new HashMap>(); private static Set descendants(String person) { Set descendants = new HashSet(); List l = children.get(person); if (l != null) { descendants.addAll(l); for (String child : l) descendants.addAll(descendants(child)); } return descendants; } private static Set ancestors(String person) { Set ancestors = new HashSet(); List l = parents.get(person); if (l != null) { ancestors.addAll(l); for (String parent : l) ancestors.addAll(ancestors(parent)); } return ancestors; } public static void main(String[] arguments) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { Scanner line = new Scanner(in.nextLine()); if (! line.hasNext()) break; String father = line.next(); String mother = line.next(); String child = line.next(); List l; l = children.get(father); if (l == null) l = new LinkedList(); l.add(child); children.put(father, l); l = children.get(mother); if (l == null) l = new LinkedList(); l.add(child); children.put(mother, l); l = new LinkedList(); l.add(father); l.add(mother); parents.put(child, l); } while (in.hasNext()) { String person = in.next(); System.out.printf("%s has %d descendants and %d ancestors%n", person, descendants(person).size(), ancestors(person).size()); } } }