#!/usr/bin/env python3 import sys from collections import defaultdict rules = defaultdict(set) prints = [] in_rules = True for l in sys.stdin: l = l.strip() if l == "": in_rules = False continue if in_rules: (l, r) = l.split("|") rules[r].add(l) else: prints.append(l.split(",")) tot = 0 for pr in prints: passed = True all = set(pr) seen = set() for pg in pr: seen.add(pg) for dep in rules[pg]: if dep in all and dep not in seen: passed = False break if passed: tot += int(pr[int(len(pr)/2)]) print(tot)