There are cities in
the Magical Island, numbered from to . These cities are connected by
bi-directional roads
such that there is a unique path between every pair of cities.
Each road has a congestion factor. The
traffic condition on the Magical Island is very complicated. To
transport one magical crystal from city to city , one must pay a cost that equals
the maximum congestion factor among all the roads on the path
from city to city
.
Demand and supply of magical crystals vary every year. In
the -th year, there are
cities that either
generate or request magical crystals. The Magical Island is
very aware of energy efficiency, so that every year the demand
of magical crystals equals the supply. Each generated magical
crystal must be transported to a city that requests magical
crystals. The Magical Island needs to arrange the
transportation to minimize the transportation cost in each
year.
Input
The first line contains a single integer . Each of
the next lines
contains three integers that describe a road connecting city
and city with a congestion factor of
. In the next line,
there is a single integer , the
number of years to consider. The input then describes the
demand and supply of the years in order. Each year starts
with a positive integer on the first line. The next lines each have two integers
() and (). A positive means city generates magical crystals. A negative
means city
requests magical crystals. The sum of
in each year is
guaranteed to be zero. The total number of magical crystals
generated across all
years is no larger than
Output
For each year, output the minimum total cost to transport
the magical crystals on a single line.
Sample Input 1 |
Sample Output 1 |
3
1 2 3
2 3 2
3
2
1 1
2 -1
2
2 1
3 -1
2
1 -2
3 2
|
3
2
6
|
Sample Input 2 |
Sample Output 2 |
6
1 2 6
1 3 8
1 4 4
2 5 12
2 6 2
4
6
1 -5
2 1
3 1
4 1
5 1
6 1
4
3 2
5 -1
6 -2
4 1
2
2 1
6 -1
6
1 1
2 1
3 1
4 -1
5 -1
6 -1
|
36
26
2
18
|