#P7551. [COCI 2020/2021 #6] Alias

[COCI 2020/2021 #6] Alias

Problem Description

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 nn words in his head, and there are mm connections between some words. The connection between words xx and yy, with time tt, means that if Rafael remembers the word xx or hears it, after tt milliseconds he will remember the word yy.

Novak and Rafael will play qq rounds. In each round, Novak wants to know: if he says the word aa, after how many milliseconds will Rafael remember the word bb for the first time? The rounds are independent.

Input Format

The first line contains integers nn (2n10002 \leq n \leq 1000) and mm (1m10001 \leq m \leq 1000), the number of words and the number of connections.

Each of the following mm lines contains two different words xix_i and yiy_i, and an integer tit_i (1ti1091 \leq t_i \leq 10^9), that describe a connection. The words consist of at most 2020 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 qq (1q10001 \leq q \leq 1000), the number of rounds.

Each of the following qq lines contains two different words aia_i and bib_i, the word that Novak will say and the word that Rafael needs to remember in the ii-th round. Both words appear in Rafael's database.

Output Format

Output qq lines. In the ii-th line output the time for the ii-th round in milliseconds, or Roger if Rafael will never remember the word.

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

Clarification of the first example:

In the first round, Novak will say the word novak\tt{novak}. After 11 millisecond, Rafael will remember the word goat\tt{goat}, and after 33 more milliseconds the required word simulator\tt{simulator}. In the second round, Novak will say the word simulator\tt{simulator}, but Rafael won't remember any other words.


Scoring

In test cases worth 20 points, it holds 1n101 \leq n \leq 10.
In test cases worth additional 20 points, it holds 1m1001 \leq m \leq 100.


Notes

The score of this problem follows the original COCI settings, with a full score of 7070..

From COCI2020-2021 CONTEST #6 T2 Alias.