Hide

Problem D
Mixing Colours

Frank lives in London and he really likes games and maths. Lately, he has been playing a game on his mobile phone. It is simple, a sequence of coloured tokens is provided and each turn the player has to combine a pair of adjacent tokens to obtain a new token of certain colour. This process is repeated until the whole sequence results in only one final token. The problem is that not every pair of colours can be merged. There is a set of rules describing the allowed combinations. For example, given the following rules

\begin{equation*} \begin{matrix} \text {blue} & + & \text {yellow} & \to & \text {green} \\ \text {yellow} & + & \text {red} & \to & \text {orange} \\ \text {blue} & + & \text {orange} & \to & \text {brown} \\ \end{matrix}\end{equation*}

and the sequence (blue, yellow, red), the game could be finished obtaining a brown token after two steps: $(\mathtt{blue}, \mathtt{yellow}, \mathtt{red}) \to (\mathtt{blue}, \mathtt{orange}) \to (\mathtt{brown})$.

Frank is now in Valencia attending a famous programming contest and he is waiting for the tram to go to the university. Meanwhile, he is playing this game to fill time but the sun is shining so bright that he can not properly see the screen of his phone. He has some certainty about the possible colour of each token and he is wondering what will be the resulting colour following the most likely play of the game according to his estimation of the input sequence. Given Frank’s estimation of two colours A and B, and the combination $\texttt{A} + \texttt{B} \to \texttt{C}$, the certainty of the obtained colour C is computed as $\mathsf{cer}(\mathtt{C}) = \mathsf{cer}(\mathtt{A}) \cdot \mathsf{cer}(\mathtt{B})$.

Input

The first line contains $R$ ($0 < R \leq 100$) the number of rules describing the allowed combinations of colours. Each of the following $R$ lines contains three strings $s_1$, $s_2$, $s_3$ representing the combination $s_1 + s_2 \to s_3$. Next line shows the number of test cases $T$. For each test case an integer $C$ indicates the length of the input sequence of tokens ($0 < C \leq 500$). Each of the next $C$ lines describes a token, it contains a list of pairs $(k, \mathsf{cer}(k))$ providing a colour $k$ and its corresponding certainty ($0 < \textsf{cer}(k) \leq 1.0$). The list ends with the word END. The sum of the certainties for a certain token always is $1.0$. Every colour in a test case has appeared first in the rules describing the game.

Output

For each test case, print the resulting colour of the most likely play of the game. If there is no possible combination to finish the game then print GAMEOVER.

Sample Input 1 Sample Output 1
7
Blue Yellow Green
Yellow Red Orange
Green Red Yellow
White Red Pink
Pink Black Red
Orange Red Red
Yellow Orange Yellow
3
2
Red 0.7 Orange 0.3 END
Yellow 1.0 END
4
Blue 0.6 Green 0.4 END
Red 0.2 Orange 0.6 Yellow 0.2 END
White 0.9 Yellow 0.1 END
Red 0.5 Black 0.5 END
2
Blue 1.0 END
Orange 1.0 END
Orange
Yellow
GAMEOVER

Please log in to submit a solution to this problem

Log in