#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; class LCA{ public: LCA(){ } LCA(const vector> &G, int root = 0):V((int)G.size()), depth(V), parent(V){ rep(i, LG)ancestor[i] = vector(V); dfs(root, 0, 0, G); rep(i, V) ancestor[0][i] = parent[i]; rep(i,LG-1)rep(v,V) ancestor[i + 1][v] = ancestor[i][ancestor[i][v]]; } int get(int u, int v){ if(depth[u] < depth[v]) swap(u, v); int dist = depth[u] - depth[v]; for(int i = 0; i < LG; ++i) if((dist >> i) & 1) u = ancestor[i][u]; if(u == v) return u; for(int i = LG - 1; i >= 0; --i){ if(ancestor[i][u] != ancestor[i][v]){ u = ancestor[i][u]; v = ancestor[i][v]; } } return ancestor[0][u]; } int getDepth(int u){ return depth[u]; } int goUp(int u, int len){ for(int i = 0; i < LG; ++i) if(len >> i & 1)u = ancestor[i][u]; return u; } int distance(int u, int v){ return depth[u] + depth[v] - 2 * depth[get(u, v)]; } int getParent(int u){ return parent[u]; } private: static const int LG = 18; int V; vector depth, parent, ancestor[LG]; void dfs(int v, int p, int d, const vector> &G){ depth[v] = d; parent[v] = p; for(int to : G[v]){ if(to != p){ dfs(to, v, d + 1, G); } } } }; const int MAX = (int)100000; int N, M, U[MAX]; ll sum[MAX]; vector G; void dfs(int u, int pre){ sum[u] = U[u]; if(pre != -1)sum[u] += sum[pre]; each(v, G[u])if(v != pre)dfs(v, u); } int main(){ cin >> N; G.resize(N); rep(i, N - 1){ int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } rep(i, N)scanf("%d", U + i); auto lca = LCA(G); dfs(0, -1); cin >> M; ll ans = 0; while(M--){ int A, B, C; scanf("%d%d%d", &A, &B, &C); ll l = lca.get(A, B), val = sum[A] + sum[B]; val -= sum[l]*2; val += U[l]; ans += val*C; } cout << ans << endl; }