Scientists discovered a new kind of microscopic lifeform and named them synnergs. Only a few facts about synnergs are known:
An example set of rules:
| # | Target | Source #1 | Source #2 | Amplification factor |
|---|---|---|---|---|
| 1 | aa | a | a | 1 |
| 2 | AA | a | a | 17 |
| 3 | bb | b | b | 3 |
| 4 | x | aa | bb | 2 |
| 5 | x | x | a | 2 |
| 6 | c | c | a | 3 |
When two or more newborn synnergs are placed in a sequence, each synnerg may unify with its left or right neighbour, making the sequence shorter. Unification repeats recursively, and there can be many different ways to unify the same sequence, yielding different lifetimes.
Example unification steps:
| Step | Sequence | Lifetime | Rules | Remarks |
|---|---|---|---|---|
| 1 | a a b b | 1 1 1 1 | - | Newborn sequence |
| 2 | aa bb | 2 6 | 1, 3 | |
| 3 | x | 16 | 4 | Completely unified |
| 1 | a a b b | 1 1 1 1 | - | Newborn sequence |
| 2 | AA bb | 34 6 | 2, 3 | Final but not completely unified |
| 1 | a c a c | 1 1 1 1 | - | Newborn |
| 2 | c c | 6 6 | 6, 6 | Final but not completely unified |
| 1 | a c a c | 1 1 1 1 | - | Newborn |
| 2 | a c c | 1 6 1 | -, 6, - | |
| 3 | c c | Final but not completely unified |
In the first example the newborn sequence a a b b (lifetimes 1 1 1 1) unifies into aa[a a] bb[b b] using rules 1 and 3, where aa has lifetime $2 = (1 + 1) \times 1$ and bb has lifetime $6 = (1 + 1) \times 3$. Then aa bb unifies into x[aa bb] using rule 4, with lifetime $16 = (2 + 6) \times 2$.
In the second example the same newborn sequence instead unifies into AA[a a] bb[b b] using rules 2 and 3, with lifetimes $34 = (1 + 1) \times 17$ and $6 = (1 + 1) \times 3$. This sequence is final (no further unification is possible) even though it is not fully unified into one synnerg, and its AA has a longer lifetime than the fully unified x above.
Given a newborn sequence, find every synnerg with the maximum possible lifetime that can be produced by fully unifying some contiguous part of the sequence. The part may be the whole sequence or only a sub-sequence, and the result need not be completely unified. A synnerg is creatable when some contiguous block of the input can be unified, step by step, into that single synnerg; its lifetime depends on the unification order, so for each (type, block) take the largest achievable lifetime. Report all synnergs whose lifetime equals the overall maximum.
The input has two parts separated by a blank line.
Part 1 — unification rules. Each line is one rule with four space-separated fields: the target type, source type #1, source type #2, and the amplification factor. Each type is a string of at most 20 alphanumeric characters. The amplification factor is a positive integer at most 100.
Part 2 — sequences. Each line is one newborn sequence: synnerg types separated by spaces. Every synnerg in a sequence starts as a newborn with lifetime 1.
A blank line (or the end of input) terminates the list of sequences.
For each input sequence print two parts:
The example uses the 6 rules above and 5 sequences.
a a b b, even though it can be fully unified into x[aa[a a] bb[b b]] at offsets 1 to 4 with lifetime $16 = ((1+1)\times 1 + (1+1)\times 3) \times 2$, that is smaller than the 34 of AA[a a] at offsets 1 to 2, so the answer is AA 1 2.a a b b a there are two solutions: AA at offsets 1 to 2 and x at offsets 1 to 5, both with lifetime 34.a a b b a a there is one solution: x at offsets 1 to 6 with lifetime 70.c, remember that rule 6 (target c from sources c and a) is order-insensitive, so it applies whenever an adjacent c and a occur in either order.