8664: Apples

内存限制:128 MB 时间限制:11 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

The people in city A want to share their apples. There are nn people in city A, and they are numbered from 1 to n. The ii-th person has bi apples initially, and this person will be happy if and only if he/she has exactly ei apples after sharing.It is guaranteed that the total number of apples will not changed, which is 

i=1nbi=i=1nei.

The city has n undirected roads. The i-th road connects the i-th and the (modn+1)-th person’s house. the apples can be transported by these roads. Each road has a value li, denoting the cost of transporting a single apple from person i to (modn+1) or from person (modn+1) to ii. Each apple can be transported by any road any number of times(including zero).

You need to find out a way to make all the people become happy and minimize the total cost of all the apples. And the cost of an apple is the total cost of all the roads it passed. Noted that an apple can pass the same road more than one time, and the cost will be counted repeatedly.

The cost of some roads may be changed, and you need to find out all the answers.

输入格式

The first line contains a single integer T(T100) - the number of test cases.

For each test case:

The first line contains a single integer n(2n5×105) - the number of people.

From the second line to the (n+1)-th line, each line contains three integers bi,ei,li(0bi,ei109,0li104). It is guaranteed thati=1nbi=i=1nei109.

The (n+2)-th line contains a single integer q(0q5×105).

Then the next q lines, each line contains two integers x,y(1xn,0y104), means that l_x is changed to y. Please note that the change in l_x has aftereffects.

It is guaranteed that the sum of n and the sum of q do not exceed 2\times 2×106.

输出格式

For each test case, output (q+1) lines:

Each line contains a single integer, The first integer denotes the answer without any changing, and the (i+1)-th integer (1in) denotes the answer after the i-th change.

输入样例 复制

2
4
4 1 4
5 1 4
1 9 1
9 8 1
0
2
1 2 1
2 1 2
3
1 3
1 2
2 1

输出样例 复制

23
1
2
2
1