Piping Up
Adelaide has a vast and complex plumbing network.
There are houses, with
pipes flowing between them. Each pipe has some capacity, in liters.
Consider a path of pipes between two houses. The "flow" of that path is the minimum capacity along that path (or, it's "bottleneck").
Now, consider all paths between two houses. The "widest flow" is equal to the path with the biggest flow.
For example, consider this network of pipes:
The "widest flow" between house and house
is
. This path goes from house
, and has a flow of
, since its smallest pipe and hold 3 liters.
There are several other paths between house and house
, but they don't have the widest flow. For example:
, with a flow of
.
, with a flow of
.
Today, you have gotten phone calls, asking you what the widest flow is between two houses. Answer them!
Input
The first line consists of an integer
, the number of houses in the plumbing network.
The second line consists of an integer
, the number of pipes in the plumbing network.
The next lines are in the form
, signifying that there is a pipe of capacity
between houses
and
.
The next line consists of an integer
, the number of queries for you to answer.
The next lines are in the form
, meaning you need to output the "widest flow" between houses
and
.
Note: Each pair of houses may have at most 1 pipe between them.
Output
Output the maximum "widest flow" for each query.
Note: The graph is not guaranteed to be connected. If two houses are completely disconnected, their "widest flow" is -1.
Example
Input
5
8
1 2 3
1 4 1
1 5 3
2 3 2
2 4 3
2 5 3
3 4 4
3 5 2
3
1 3
2 3
3 4
Output
3
3
4
This is the above example.
- We have established that the widest flow from house
to house
is
.
- The widest flow from house
to house
is
. The optimal path is
.
- The widest flow from house
to house
is
. The optimal path is
.
Input
5
4
1 2 1
2 3 1
3 4 1
4 5 1
4
1 3
2 3
3 4
1 4
Output
1
1
1
1
Comments