Novak and Rafael are playing a simplified version of the game Alias. Novak needs to make Rafael guess a word without saying it. Rafael has a database of
words in his head, and there are
connections between some words. The connection between words
and
, with time
, means that if Rafael remembers the word
or hears it, after
milliseconds he will remember the word
.
Novak and Rafael will play
rounds. In each round, Novak wants to know: if he says the word
, after how many milliseconds will Rafael remember the word
for the first time? The rounds are independent.
Input Specification
The first line contains integers
and
, the number of words and the number of connections.
Each of the following
lines contains two different words
and
, and an integer
, that describe a connection. The words consist of at most
lowercase letters. All words from Rafael's database will appear at least once. It is possible that there are multiple connections between some pairs of words.
The following line contains an integer
, the number of rounds.
Each of the following
lines contains two different words
and
, the word that Novak will say and the word that Rafael needs to remember in the
round. Both words appear in Rafael's database.
Output Specification
Output
lines. In the
line output the time for the
round in milliseconds, or Roger
if Rafael will never remember the word.
Constraints
Subtask | Points | Constraints |
1 | 20 |  |
2 | 20 |  |
3 | 30 | No additional constraints. |
Sample Input 1
Copy
3 2
novak goat 1
goat simulator 3
2
novak simulator
simulator goat
Sample Output 1
Copy
4
Roger
Explanation for Sample Output 1
In the first round, Novak will say the word novak
. After
millisecond, Rafael will remember the word goat
, and after
more milliseconds the required word simulator
. In the second round, Novak will say the word simulator
, but Rafael won't remember any other words.
Sample Input 2
Copy
3 3
kile legend 4
legend beer 5
beer kile 6
2
kile beer
legend kile
Sample Output 2
Copy
9
11
Sample Input 3
Copy
4 5
rafael me 5
me ow 6
ow ausopenfinal 2012
ausopenfinal me 2
rafael ausopenfinal 2
3
rafael me
me rafael
ow me
Sample Output 3
Copy
4
Roger
2014
Comments