#include using namespace std; #define rep(i,a,b) for(int i=a;i E[101010]; int U[101010]; //----------------------------------------------------------------- int sum[101010]; void bfs() { queue que; sum[0] = U[0]; que.push(0); while (!que.empty()) { int i = que.front(); que.pop(); for (int j : E[i]) if (sum[j] == 0) { sum[j] = sum[i] + U[j]; que.push(j); } } } //----------------------------------------------------------------- class LCA { public: int NV; int logNV; vector D; vector L; vector > P; LCA(int N) { NV = N; logNV = 0; while (NV > (1LL << logNV)) logNV++; D = vector(NV); L = vector(NV); P = vector >(logNV, vector(NV)); } void init() { dfs(0, -1, 0, 0); build(); } void dfs(int v, int par, int d, long long l) { D[v] = d; P[0][v] = par; L[v] = l; for (int i = 0; i < (int)E[v].size(); i++) { int w = E[v][i]; int lc = 1; if (w == par) continue; dfs(w, v, d + 1, lc + l); } } void build() { for (int k = 0; k + 1 < logNV; k++) { for (int v = 0; v < NV; v++) { if (P[k][v] < 0) P[k + 1][v] = -1; else P[k + 1][v] = P[k][P[k][v]]; } } } int query(int u, int v) { if (D[u] > D[v]) swap(u, v); for (int k = 0; k < logNV; k++) { if ((D[v] - D[u]) >> k & 1) v = P[k][v]; } if (u == v) return u; for (int k = logNV - 1; k >= 0; k--) { if (P[k][u] != P[k][v]) { u = P[k][u]; v = P[k][v]; } } return P[0][u]; } }; //----------------------------------------------------------------- int main() { scanf("%d", &N); rep(i, 0, N - 1) { int a, b; scanf("%d %d", &a, &b); E[a].push_back(b); E[b].push_back(a); } rep(i, 0, N) scanf("%d", &U[i]); bfs(); LCA lca(N); lca.init(); int M; scanf("%d", &M); ll ans = 0; rep(i, 0, M) { int a, b, c; scanf("%d %d %d", &a, &b, &c); int p = lca.query(a, b); ans += (sum[a] + sum[b] - sum[p]) * c; } printf("%lld\n", ans); }