eolymp
bolt
Try our new interface for solving problems
Problems

The road in ByteLandia – 2

The road in ByteLandia – 2

\includegraphics{https://static.e-olymp.com/content/a1/a162d5fecf99b0ef6379b4f51dbbb86b03c9bd90.jpg} By visiting islands of ByteLandia the president Bityk understood that the inhabitants of islands also want to travel the country on their SonaL. That is why Bityk needs to allocate a new budget for building two way bridges. The architect gave the president the projects of future bridges. The president need to choose which bridges will be build to connect all islands. It is obvious that Bityk wants to spend as small as possible. After that the inhabitants of the islands are interested in which is the length of the smallest route between to islands? \InputFile In first line will be given two positive integer: \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{10^5}) -- number of islands in country, and \textbf{M }(\textbf{1} ≤ \textbf{M} ≤ \textbf{10^5}) --number of bridge projects. In next \textbf{M} lines will be \textbf{3} positive integers: \textbf{u}, \textbf{v}, \textbf{w} (\textbf{1} ≤ \textbf{u}, \textbf{v} ≤ \textbf{N}, \textbf{u} ≠ \textbf{v}, \textbf{1} ≤ \textbf{w} ≤ \textbf{10^4}), \textbf{u}, \textbf{v} -- numbers of islands, which can be connect by bridge with length \textbf{w}. In next line positive integer \textbf{q} (\textbf{1} ≤ \textbf{q} ≤ \textbf{10^7}) -- numbers of queries. In next \textbf{q} lines will be given numbers of islands, between which is requesting to find a smallest distance. \OutputFile In first line cost of all new bridges, which was built, or "\textbf{Impossible}" (without quotes), if we can’t build a bridge system, that there will be path between any islands. If we can build a bridges, in next \textbf{q} lines output for all queries.
Time limit 15 seconds
Memory limit 64 MiB
Input example #1
5 7
1 2 5
1 3 10
2 3 20
3 5 30
3 4 25
4 5 35
1 5 15
4
1 2
3 5
1 5
2 4
Output example #1
55
5
25
15
40
Author Ostap Stoliarchuk
Source Distance Summer Computer School - Summer 2013