Problem list
Log in
#coci00586 · COCI 2020/2021 Contest 6

Alias

Gold IV 1.0 s 256 MB

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 $n$ words in his head, and there are $m$ connections between some words. The connection between words $x$ and $y$, with time $t$, means that if Rafael remembers the word $x$ or hears it, after $t$ milliseconds he will remember the word $y$. Novak and Rafael will play $q$ rounds. In each round, Novak wants to know: if he says the word $a$, after how many milliseconds will Rafael remember the word $b$ for the first time? The rounds are independent.

Input

The first line contains integers $n$ ($2 \le n \le 1000$) and $m$ ($1 \le m \le 1000$), the number of words and the number of connections. Each of the following $m$ lines contains two different words $x_{i}$ and $y_{i}$, and an integer $t_{i}$ ($1 \le t_{i} \le 10^{9}$), that describe a connection. The words consist of at most 20 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 $q$ ($1 \le q \le 1000$), the number of rounds. Each of the following $q$ lines contains two different words $a_{i}$ and $b_{i}$, the word that Novak will say and the word that Rafael needs to remember in the $i$-th round. Both words appear in Rafael’s database.

Output

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

Constraints

In test cases worth 20 points, it holds $1 \le n \le 10$. In test cases worth additional 20 points, it holds $1 \le n \le 100$.

Example 1

Input
3 2
novak goat 1
goat simulator 3
2
novak simulator
simulator goat
Output
4
Roger

Example 2

Input
3 3
kile legend 4
legend beer 5
beer kile 6
2
kile beer
legend kile
Output
9
11

Example 3

Input
4 5
rafael me 5
me ow 6
ow ausopenfinal 2012
ausopenfinal me 2
rafael ausopenfinal 2
3
rafael me
me rafael
ow me
Output
4
Roger
2014
Source
COCI 2020/2021 Contest 6
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code