This page is still under construction.

Parts of this page are still being built. What you see may change.

Mixing Colours

Time limit5sMemory limit128 MB

Summary
The player picks one colour per token and merges adjacent tokens under the rules to maximize the product of picked certainties, breaking ties in ASCII order.
Level

Hard8 of 10

Topics
Dynamic programming, Probability
Solved
No attempts yet

Problem

Frank lives in London and he likes games and maths. Lately he has been playing a simple game on his phone. A row of coloured tokens is given, and on every turn the player merges a pair of adjacent tokens into one token of a certain colour. The turns repeat until a single token is left.

Not every pair of colours can be merged. A set of rules says which combinations are allowed. With the rules

  • Blue + Yellow → Green
  • Yellow + Red → Orange
  • Blue + Orange → Brown

and the row (Blue, Yellow, Red), the game can end with a Brown token after two turns. (Blue, Yellow, Red) → (Blue, Orange) → (Brown)

A rule ignores the order of its two colours. If the rule s1+s2→s3s_1 + s_2 \to s_3 exists, two adjacent tokens merge into one s3s_3 token whether they sit as (s1,s2)(s_1, s_2) or as (s2,s1)(s_2, s_1).

Frank is now in Valencia for a programming contest. He is waiting for the tram to the university and playing this game to fill time, but the sun shines so bright that he cannot see the screen properly. He has only some certainty about the colour of each token, so he wonders which colour is left at the end of the most likely play according to his estimate. Given his estimate of two colours AA and BB and the rule A+B→CA + B \to C, the certainty of the obtained colour CC is cer(C)=cer(A)×cer(B)\mathrm{cer}(C) = \mathrm{cer}(A) \times \mathrm{cer}(B).

One play picks a single colour for every token and merges the whole row into one token under the rules. The certainty of that play is the product of the certainties of the picked colours.

Input

The first line contains RR, the number of rules describing the allowed combinations of colours. (0<R≤1000 < R \le 100)

Each of the following RR lines contains three strings s1s_1, s2s_2, s3s_3 representing the rule s1+s2→s3s_1 + s_2 \to s_3.

The next line contains the number of test cases TT.

The first line of each test case contains the length CC of the row of tokens. (0<C≤5000 < C \le 500)

Each of the next CC lines describes one token. It lists pairs of a colour kk and its certainty cer(k)\mathrm{cer}(k), and ends with the word END. (0<cer(k)≤1.00 < \mathrm{cer}(k) \le 1.0)

The certainties of one token always sum to 1.01.0. Every colour in a test case has appeared first in the rules.

Output

Print one line for each test case.

If some choice of one colour per token merges the whole row into a single token under the rules, print the colour left at the end of the play whose product of picked certainties is largest. When several colours reach that largest certainty, print the one that comes first in lexicographic (ASCII) order.

For C=1C = 1 there is nothing to merge, so the answer is the colour with the largest certainty in that token.

If no play finishes the game, print GAMEOVER.

Hint

The first case of the first example has only two tokens. Frank is sure that the second token is Yellow, but the first token could be either Red or Orange. The game can be finished in two ways.

  • (Red, Yellow) → (Orange), certainty 0.70.7. The rule Yellow + Red → Orange applies with its two colours swapped.
  • (Orange, Yellow) → (Yellow), certainty 0.30.3.

The first play is more likely, so the final colour is Orange.

The second case has more tokens and estimates. Two possible plays are

  • (Blue, Yellow, Yellow, Red) → (Blue, Yellow, Orange) → (Blue, Yellow) → (Green), certainty 0.0060.006
  • (Green, Red, White, Black) → (Green, Pink, Black) → (Green, Red) → (Yellow), certainty 0.0360.036

The second play is more likely, so the answer is Yellow.

In the third case Frank is sure that the tokens are Blue and Orange. No rule merges those two colours, so the game cannot be finished.

The first case of the second example ends in two plays of equal certainty. In (Red, Red, Yellow), merging the first pair gives Red + Red → Blue and then Blue + Yellow → Green, which leaves Green. Merging the second pair gives Red + Yellow → Purple and then Red + Purple → Cyan, which leaves Cyan. Both plays have certainty 1.01.0, so the answer is Cyan, the colour that comes first in lexicographic order.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    Orange
    Yellow
    GAMEOVER
    
  2. Example 2

    Input
    4
    Red Red Blue
    Red Yellow Purple
    Blue Yellow Green
    Red Purple Cyan
    2
    3
    Red 1.0 END
    Red 1.0 END
    Yellow 1.0 END
    2
    Yellow 0.9 Red 0.1 END
    Yellow 0.9 Purple 0.1 END
    
    Expected output
    Cyan
    Purple