Under construction

Still being built. Things may change

Ninja Delivery

Time limit1sMemory limit1024 MB

Summary
Pick hubs x and y on a tree to minimize the total weighted cost of Q requests, where each request goes from a to x, x to y, then y to b.
Level

Hard8 of 10

Topics
Tree, Divide and conquer, Dynamic programming, DFS
Solved
No attempts yet

Problem

Byeongwoo is the village head of Leaf village. Leaf village has NN markets and N−1N-1 bidirectional roads of length 11 that connect pairs of markets. Any two different markets can reach each other using only roads. In other words, Leaf village is shaped like a tree. For convenience, the markets are numbered from 11 to NN.

Recently, the economy of Leaf village has grown, so goods need to be moved between markets more often. There are QQ such requests. Each request is written as three integers (a,b,w)(a, b, w), meaning that ww kg of goods must be moved from market aa to market bb. Byeongwoo hired ninjas to handle the requests. To use the ninja parcel service efficiently, he must choose two markets xx and yy to serve as logistics hubs. xx and yy do not need to be different.

For two markets aa and bb, let d(a,b)d(a, b) be the shortest distance from aa to bb when traveling only along roads.

  • All goods must first be gathered at market xx. Moving goods from market aa to market xx costs sas_{a} per kilogram per unit distance.
  • Then the ninjas use a mysterious technique to move all goods at market xx to market yy. This costs uu per kilogram per unit distance.
  • Finally, goods are carried from market yy to the destination of each request. Moving goods to market bb costs tbt_{b} per kilogram per unit distance.

In other words, moving ww kg of goods from market aa to market bb costs w(sad(a,x)+ud(x,y)+tbd(y,b))w(s_{a}d(a, x) + u d(x, y) + t_{b}d(y, b)). Choose the markets xx and yy so that the total cost of all QQ requests is minimized, and write a program that finds this minimum.

Input

The first line contains the number of markets NN.

Each of the next N−1N-1 lines contains two integers uu and vv, separated by a space. This means that the i+1i+1-th road connects market uu and market vv. It is guaranteed that 1≤u≠v≤N1 \le u \neq v \le N and that the roads form a tree.

Each of the next NN lines contains two integers sis_{i} and tit_{i}, separated by a space. sis_{i} is the cost per unit distance per kilogram of moving goods from market ii to market xx. tit_{i} is the cost per unit distance per kilogram of moving goods from market yy to market ii.

The line after that contains the number of requests QQ.

Each of the next QQ lines contains three integers aa, bb, and ww, separated by spaces. This means that ww kg of goods must be moved from market aa to market bb. aa and bb may be equal, and the same (a,b)(a, b) pair may appear in more than one request.

The last line contains uu, the cost per unit distance per kilogram of moving goods from market xx to market yy.

Output

Print the minimum possible total cost of all QQ requests on the first line.

Constraints

  • 1≤N≤100,0001 \le N \le 100,000
  • 1≤Q≤100,0001 \le Q \le 100,000
  • 0≤si,ti≤1060 \le s_{i}, t_{i} \le 10^{6} (1≤i≤N)(1 \le i \le N)
  • 0≤u≤1060 \le u \le 10^{6}
  • For all QQ requests (a,b,w)(a, b, w), 1≤w≤101 \le w \le 10.

Examples1

  1. Example 1

    Input
    6
    4 2
    2 1
    2 5
    1 6
    2 3
    9 7
    10 1
    2 2
    3 6
    10 9
    4 10
    8
    6 6 6
    5 4 5
    6 2 1
    4 5 4
    4 5 4
    5 2 9
    2 2 6
    3 3 10
    4
    
    Expected output
    482