#P7551. [COCI 2020/2021 #6] Alias
[COCI 2020/2021 #6] Alias
Description
Novak and Rafael are playing a word guessing game.
Rafael has a database in his mind that contains words. The database also has links of the form , meaning that if he remembers or hears the word , he will remember the word after milliseconds.
The game will be played for rounds, and each round is independent. At the start of each round, Novak will say the initial word . He wants to know: after how many milliseconds will Rafael remember the target word ?
Input Format
The first line contains two integers .
The next lines each contain two strings and an integer , representing a link.
The next line contains an integer .
The next lines each contain two strings , representing the initial word and the target word in the -th round.
Output Format
Output lines, the answer for each round.
If Rafael can remember the target word, output an integer representing the required time. Otherwise, output Roger.
3 2
novak goat 1
goat simulator 3
2
novak simulator
simulator goat
4
Roger
3 3
kile legend 4
legend beer 5
beer kile 6
2
kile beer
legend kile
9
11
4 5
rafael me 5
me ow 6
ow ausopenfinal 2012
ausopenfinal me 2
rafael ausopenfinal 2
3
rafael me
me rafael
ow me
4
Roger
2014
Hint
Explanation of Sample 1
In the first round, Novak says the word novak. After millisecond, Rafael remembers the word goat. After another milliseconds, he remembers the target word simulator.
In the second round, Novak says the word simulator, but Rafael will not remember any other word.
Constraints
For the testdata worth points, .
For the testdata worth points, .
For of the testdata, , , , . It is guaranteed that each word has length at most and contains only lowercase letters.
Notes
The score of this problem follows the original COCI settings, with a full score of ..
Translated from COCI2020-2021 CONTEST #6 T2 Alias.
Translated by ChatGPT 5
京公网安备 11011102002149号