Gossip Between Villages

easy graphs shortest-paths dp 2800 ms · 256 MB

Gossip Between Villages

The valley has n villages, numbered from 1 to n, and m footpaths. The i-th footpath joins villages u_i and v_i and takes w_i minutes to walk, in either direction. Two villages may be joined by several footpaths, possibly with different walking times, and a footpath may even start and end at the same village (a loop around the village pond).

News travels only on foot. When something happens in a village, a messenger sets off at once and may walk along any sequence of footpaths, passing through as many villages as needed; nobody ever waits along the way, and passing through a village costs no time. So the time for news to get from village a to village b is the smallest total walking time over all ways to walk from a to b. News that happens in a village is known there immediately, so the time from a village to itself is 0.

The village elders have q questions. Each question names two villages a and b and asks how many minutes it takes for news from village a to reach village b. If no sequence of footpaths leads from a to b, the news never arrives.

Input

The first line contains three integers n, m and q (1 ≤ n ≤ 300, 0 ≤ m ≤ 10^5, 1 ≤ q ≤ 10^5): the number of villages, footpaths and questions.

Each of the next m lines contains three integers u_i, v_i and w_i (1 ≤ u_i, v_i ≤ n, 1 ≤ w_i ≤ 10^9): a footpath between villages u_i and v_i that takes w_i minutes to walk. u_i may equal v_i.

Each of the next q lines contains two integers a and b (1 ≤ a, b ≤ n): a question. a may equal b.

Output

Print q lines. The j-th line must contain the answer to the j-th question: the smallest number of minutes for news from village a to reach village b, or -1 if it can never get there.

Notes

In the first example, villages 1 and 2 are joined by two footpaths taking 4 and 3 minutes. News from village 1 reaches village 4 fastest by walking 1 → 2 (3 minutes), 2 → 3 (1 minute) and 3 → 4 (2 minutes), 6 minutes in total; the direct footpath from 1 to 3 (7 minutes) is slower than going through village 2 (4 minutes). Village 5 has no footpaths at all, so news from it never reaches village 2.

In the second example, the loops around villages 1 and 3 never help: news is known in its own village at time 0.

Answers can exceed 2^31: a single walk may use many footpaths that each take up to 10^9 minutes.

Example 1

Input

5 5 5
1 2 4
2 3 1
1 3 7
3 4 2
2 1 3
1 4
4 1
1 3
5 2
3 3

Output

6
6
4
-1
0

Example 2

Input

3 4 4
1 1 5
1 2 10
2 3 10
3 3 1
1 1
1 3
3 1
2 2

Output

0
20
20
0

Hints · reveal one at a time if you're stuck

Hint 1

There are only 300 villages but up to 10^5 questions, so it pays to compute the answer for every pair of villages once. Which all-pairs shortest-path method is simple enough for a graph this small?

Hint 2

Use the Floyd–Warshall algorithm with the lightest footpath between each pair of villages, and ignore loops. Distances can exceed 32 bits, so pick an infinity that cannot overflow when two of them are added, and keep the intermediate village as the outermost loop.

report an issue Prefer your own editor? sooks prep 34 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.