#include using namespace std; using ll = long long; class heavy_light_decomposition { const int n; vector> g; vector in, out, size, head, par, dep, inv; int it; void erase_par(int v, int prev) { par[v] = prev; for (auto& u : g[v]) { if (u == g[v].back()) break; if (u == prev) swap(u, g[v].back()); erase_par(u, v); } g[v].pop_back(); } void dfs1(int v) { for (auto& u : g[v]) { dfs1(u); size[v] += size[u]; if (size[u] > size[g[v][0]]) swap(u, g[v][0]); } } void dfs2(int v) { in[v] = it++; inv[in[v]] = v; for (auto u : g[v]) { head[u] = (u == g[v][0] ? head[v] : u); dep[u] = dep[v] + (u != g[v][0]); dfs2(u); } out[v] = it; } public: heavy_light_decomposition(int n_) : n(n_), g(n), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), dep(n), inv(n), it(0) {} heavy_light_decomposition(const vector>& G) : n(G.size()), g(G), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), dep(n), inv(n), it(0) {} void add_edge(int u, int v) { g[u].push_back(v); g[v].push_back(u); } void build(int rt = 0) { for (auto v : g[rt]) erase_par(v, rt); dfs1(rt); head[rt] = rt; dfs2(rt); } int get_id(int v) { return in[v]; } void path_query(int u, int v, function f) { while (true) { if (in[u] > in[v]) swap(u, v); f(max(in[head[v]], in[u]), in[v] + 1); if (head[u] == head[v]) return; v = par[head[v]]; } } void subtree_query(int v, function f) { f(in[v], out[v]); } int get_lca(int u, int v) { while (true) { if (in[u] > in[v]) swap(u, v); if (head[u] == head[v]) return u; v = par[head[v]]; } } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int N; cin >> N; heavy_light_decomposition hl(N); for (int i = 0; i < N - 1; i++) { int A, B; cin >> A >> B; hl.add_edge(A, B); } hl.build(); vector u(N), sum(N + 1); for (int i = 0; i < N; i++) { int U; cin >> U; u[hl.get_id(i)] = U; } for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + u[i]; } int M; cin >> M; ll res = 0; while (M--) { int A, B; ll C; cin >> A >> B >> C; ll all = 0; hl.path_query(A, B, [&](int l, int r) { all += sum[r] - sum[l]; }); res += all * C; } cout << res << endl; return 0; }