#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector< int > vi; typedef vector< vi > vvi; typedef vector< ll > vl; typedef vector< vl > vvl; typedef pair< int, int > pii; typedef vector< pii > vp; typedef vector< double > vd; typedef vector< vd > vvd; typedef vector< string > vs; template< class T1, class T2 > int upmin( T1 &x, T2 v ){ if( x > v ){ x = v; return 1; } return 0; } template< class T1, class T2 > int upmax( T1 &x, T2 v ){ if( x < v ){ x = v; return 1; } return 0; } const int INF = 0x3f3f3f3f; int N; vvi G; vi U; void init(){ cin >> N; G = vvi( N ); for( int i = 0; i < N - 1; ++i ){ int A, B; cin >> A >> B; G[ A ].emplace_back( B ); G[ B ].emplace_back( A ); } U = vi( N ); for( int i = 0; i < N; ++i ) cin >> U[ i ]; } vi cost2root; vvi par; vi depth; void dfs( int u, int fa, int cost, int dpt ){ par[ 0 ][ u ] = fa; cost2root[ u ] = cost + U[ u ]; depth[ u ] = dpt; for( int v : G[ u ] ){ if( v == fa ) continue; dfs( v, u, cost2root[ u ], dpt + 1 ); } } void preprocess(){ cost2root = vi( N ); par = vvi( 20, vi( N, -1 ) ); depth = vi( N ); dfs( 0, -1, 0, 0 ); for( int i = 0; i + 1 < 20; ++i ) for( int j = 0; j < N; ++j ){ int p = par[ i ][ j ]; if( p == -1 ) continue; par[ i + 1 ][ j ] = par[ i ][ p ]; } } int get_anc( int u, int v ){ if( not ( depth[ u ] >= depth[ v ] ) ) swap( u, v ); int diff = depth[ u ] - depth[ v ]; for( int i = 0; i < 20; ++i ) if( diff & 1 << i ) u = par[ i ][ u ], diff -= 1 << i; if( u == v ) return u; for( int i = 20 - 1; i >= 0; --i ){ int p = par[ i ][ u ]; int q = par[ i ][ v ]; if( p != q ) u = p, v = q; } return par[ 0 ][ u ]; } void solve(){ int M; cin >> M; ull ans = 0; for( int i = 0; i < M; ++i ){ int A, B, C; cin >> A >> B >> C; int anc = get_anc( A, B ); ull cost = cost2root[ A ] + cost2root[ B ] - 2 * cost2root[ anc ] + U[ anc ]; ans += cost * C; } cout << ans << endl; } signed main(){ ios::sync_with_stdio( 0 ); init(); preprocess(); solve(); return 0; }