LearnPy Hub
← Arena
AdvancedRating 1600

Distances Between Any Two Cities

Roads are two-way with lengths. Answer q questions: the shortest distance between a and b, or -1 if there is no route.

Input

n (≤ 100) and m; m lines 'u v w'; q; q lines 'a b'.

Output

q lines.

Sample 1

Input
4 3
1 2 5
2 3 2
1 3 9
3
1 3
3 1
1 4
Output
7
7
-1

Sample 2

Input
29 29
4 7 9
25 20 7
1 20 12
17 18 30
3 2 22
1 9 38
27 18 12
27 2 48
29 19 33
8 7 9
5 14 43
13 10 26
4 5 47
17 14 41
19 11 44
21 7 50
15 25 30
23 22 37
12 19 9
14 22 47
15 26 37
12 1 28
22 20 12
19 22 7
2 14 35
11 16 34
20 22 38
14 26 37
18 17 21
15
18 17
18 1
7 16
21 2
16 24
24 1
6 28
23 20
1 13
11 2
27 20
22 26
10 1
4 22
8 23
Output
21
133
231
184
-1
-1
-1
49
-1
133
133
84
-1
137
192
solution.py
Output appears here
Distances Between Any Two Cities · Python Arena problem · LearnPy Hub