Ghost leg is a method of establishing random pairings between any two groups of equal size. For example, it might be used to assign $$n$$ chores randomly to a group of $$n$$ people. In Japanese the method is known as amidakuji.

At first, a diagram is drawn consisting of $$n$$ vertical lines. Such a vertical line is called a stem. Then a random number of "legs" are added at random positions in the diagram. A leg is a horizontal line that connects two adjacent stems. It is not allowed for a stem to both have a leg to the left and a leg to the right at the same height.

amidakuji with visible legs
Sample diagram of an amidakuji that pairs the items in two groups of $$n=6$$ items. The $$n=6$$ stems (vertical lines) are labelled from left to right with the letters af. There are 17 legs in total, each connecting two adjacent stems.

Then the middle part of the diagram is covered up to conceal the legs. Only the top and bottom segments of each stem are visible, so one can't tell which top segment leads to which bottom segment. Each task is assigned to the bottom segment of a stem, and everyone chooses a top segment of a stem as their starting position (no two people at the same stem).

amidakuji with hidden legs
The middle part of the diagram is covered up to conceal the legs. Only the top and bottom segments of each stem are visible, so one can't tell which top segment leads to which bottom segment.

Now the ghost legs are revealed and each person can follow a path from the top of the diagram to the bottom of the diagram. In doing so, they must follow each leg they encounter, jump to the adjacent stem and continue downward. When a person reaches the bottom of the diagram, it establishes a link between the person and the chore found there.

path through amidakuji
A path through an amidakuji starts at a stem at the top of the diagram and runs to the bottom of the diagram. The path takes turn at each leg encountered, jumps to the adjacent stem and continues downward. When it reaches the bottom of the diagram, it establishes a link between the top segment of a stem and the bottom segment of a stem.

The benefit of this method is that it works for groups of any size, reliably establishing a 1:1 correspondence between their items. And it works no matter how many legs are added.

Assignment

The diagram of an amidakuji is described in a text file. Each stem is labelled with a unique letter. The top of the diagram is at distance 0 and distances increase as we move further down the diagram. Each line of the text file describes a leg of the amidakuji using three space-separated fields. The first two fields contain the labels of the adjacent stems connected by the leg. The third field contains the distance from the leg to the top of the diagram. Distances are always expressed as natural numbers. If these are the distances of the legs in the amidakuji we used as an example

leg distances
Sample diagram of an amidakuji where each stem is labelled with a unique letter. Distances are indicated from all legs to the top of the diagram. The top of the diagram is at distance 0 and distances increase as we move further down the diagram.

then the diagram can be described by this text file (amidakuji.txt1)

a b 5
c d 5
e f 7
b c 10
d e 10
e f 15
b c 20
d e 20
c d 23
c d 26
e f 26
a b 35
d e 35
a b 40
c d 40
b c 42
e f 42

Your task:

Example

In the following interactive session we assume the current directory contains the text file amidakuji.txt2. It describes the diagram of the amidakuji we used as an example in this assignment.

>>> branches = read_branches('amidakuji.txt3')
>>> branches['a']
{5: 'b', 35: 'b', 40: 'b'}
>>> branches['b']
{5: 'a', 10: 'c', 20: 'c', 35: 'a', 40: 'a', 42: 'c'}
>>> branches['c']
{5: 'd', 10: 'b', 20: 'b', 23: 'd', 26: 'd', 40: 'd', 42: 'b'}
>>> branches['d']
{5: 'c', 10: 'e', 20: 'e', 23: 'c', 26: 'c', 35: 'e', 40: 'c'}
>>> branches['e']
{7: 'f', 10: 'd', 15: 'f', 20: 'd', 26: 'f', 35: 'd', 42: 'f'}
>>> branches['f']
{7: 'e', 15: 'e', 26: 'e', 42: 'e'}

>>> path('a', branches)
[(0, 'a'), (5, 'b'), (10, 'c'), (20, 'b'), (35, 'a'), (40, 'b'), (42, 'c')]
>>> path('b', branches)
[(0, 'b'), (5, 'a'), (35, 'b'), (40, 'a')]
>>> path('c', branches)
[(0, 'c'), (5, 'd'), (10, 'e'), (15, 'f'), (26, 'e'), (35, 'd'), (40, 'c'), (42, 'b')]
>>> path('d', branches)
[(0, 'd'), (5, 'c'), (10, 'b'), (20, 'c'), (23, 'd'), (26, 'c'), (40, 'd')]
>>> path('e', branches)
[(0, 'e'), (7, 'f'), (15, 'e'), (20, 'd'), (23, 'c'), (26, 'd'), (35, 'e'), (42, 'f')]
>>> path('f', branches)
[(0, 'f'), (7, 'e'), (10, 'd'), (20, 'e'), (26, 'f'), (42, 'e')]

>>> amidakuji(branches)
{'a': 'c', 'b': 'a', 'c': 'b', 'd': 'd', 'e': 'f', 'f': 'e'}

These are the paths through the amidakuji that start from the different stems at the top of the diagram.

path from stem a
Path through the amidakuji starting from stem a and ending at stem c.
path from stem b
Path through the amidakuji starting from stem b and ending at stem a.
path from stem c
Path through the amidakuji starting from stem c and ending at stem b.
path from stem d
Path through the amidakuji starting from stem d and ending at stem d.
path from stem e
Path through the amidakuji starting from stem e and ending at stem f.
path from stem f
Path through the amidakuji starting from stem f and ending at stem e.