3631. Delivery Service

单测试点时限: 2.5 秒

内存限制: 512 MB

EOJ Delivery Service Company handles a massive amount of orders in our nation. As is well known, our nation has cities, with bi-directional paths connecting them, forming a tree. The length of the -th path is , meaning that the time we have to spend walking through this path is .

Now, you, as the headquarter of EOJ, has somehow learned a magic spell. Each time you cast a spell on two paths, the lengths of these two paths magically swap with each other. You can do this magic as many times as you want (possibly none).

We have orders waiting to be handled. The -th order demands sending a package from city to city . We kindly ask you to do your magic before all the deliveries start, such that the total time we have to spend on delivering these packages is minimized.

输入

The first line of the input contains one single integer ().

The next lines tell about the path information from path to path , the -th of which contains three space-separated integers , and (, , ).

The next line contains one integer ().

The next lines each contains two space-separated integers and ().

输出

Output one integer, the minimum total time you can achieve.

样例

Input
5
1 2 2
1 3 3
3 4 3
3 5 4
2
1 5
4 2
Output
14

提示

In the example, we swap the weights between edge and , so that the first order takes time units to complete; the second order takes time units. Thus the total time is .

43 人解决,51 已尝试。

58 份提交通过,共有 185 份提交。

7.7 EMB 奖励。

创建: 4 月,1 周前.

修改: 4 月,1 周前.

最后提交: 3 周,4 天前.

来源: EOJ Monthly 2018.8

标签