#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #include "dump.hpp" #else #define dump(...) #endif #define int long long #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(c) begin(c),end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = 1'000'000'007; template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } using Weight = int; struct Edge { int s, d; Weight w; Edge() {}; Edge(int s, int d, Weight w) : s(s), d(d), w(w) {}; }; bool operator<(const Edge &e1, const Edge &e2) { return e1.w == e2.w ? (e1.s == e2.s ? e1.d < e2.d : e1.s < e2.s) : e1.w < e2.w; } bool operator>(const Edge &e1, const Edge &e2) { return e2 < e1; } inline ostream &operator<<(ostream &os, const Edge &e) { return (os << '(' << e.s << ", " << e.d << ", " << e.w << ')'); } using Edges = vector; using Graph = vector; using Array = vector; using Matrix = vector; void addArc(Graph &g, int s, int d, Weight w = 1) { g[s].emplace_back(s, d, w); } void addEdge(Graph &g, int a, int b, Weight w = 1) { addArc(g, a, b, w); addArc(g, b, a, w); } auto getEdges = [](const Graph &g) { Edges ret; for (auto &es : g) for (auto &e : es) ret.emplace_back(e); return ret; }; struct UnionFind { vector parent; int size; UnionFind(int n) :parent(n, -1), size(n) {} bool unite(int x, int y) { x = root(x); y = root(y); if (x == y)return false; if (sizeOf(x) < sizeOf(y))swap(x, y); parent[x] += parent[y]; parent[y] = x; size--; return true; } bool same(int x, int y) { return root(x) == root(y); } int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); } int sizeOf(int x) { return -parent[root(x)]; } }; struct Query { int u, v; Query(int u, int v) :u(u), v(v) {} }; struct LowestCommonAncestor { vector>> query_set; Graph g; vector color; vector ancestor; vector res; UnionFind uf; LowestCommonAncestor(const Graph &g, vector &query) :g(g), color(g.size()), ancestor(g.size()), uf(g.size()), res(query.size()), query_set(g.size()) { int n = query.size(); for (int i = 0; i < n; i++) { query_set[query[i].u].emplace_back(i, query[i]); query_set[query[i].v].emplace_back(i, query[i]); } } void visit(int s, int prev) { ancestor[uf.root(s)] = s; for (auto &e : g[s]) { if (e.d == prev)continue; visit(e.d, s); uf.unite(e.s, e.d); ancestor[uf.root(s)] = s; } color[s] = 1; for (auto &p : query_set[s]) { Query q = p.second; int w = (q.v == s ? q.u : q.u == s ? q.v : -1); if (w == -1 || !color[w])continue; res[p.first] = ancestor[uf.root(w)]; } } vector solve(int root) { int n = g.size(); UnionFind uf(n); vector color(n), ancestor(n); visit(root, -1); return res; } }; auto bfs = [&](const Graph &g, int s, Array &dist) { int n = g.size(); vector vis(n); vector prev(n, -1); dist.assign(n, INF); dist[s] = 0; using State = tuple; queue q; q.emplace(0, s, -1); while (q.size()) { Weight d; int v, p; tie(d, v, p) = q.front(); q.pop(); vis[v] = true; prev[v] = p; for (auto &e : g[v]) { if (vis[e.d])continue; if (dist[e.d] > dist[v] + e.w) { dist[e.d] = dist[v] + e.w; q.emplace(dist[e.d], e.d, v); } } } return prev; }; // 無向木を anti-arborescence に変換 // 連結無向グラフから変換することも可能 // (親, weight) の配列を返す vector> antiArborescence(const Graph &g, int root = 0) { int n = g.size(); vector vis(n); queue q; q.emplace(root); vector> ret(n); ret[root] = make_pair(-1, 0); while (q.size()) { int u = q.front(); q.pop(); if (vis[u])continue; vis[u] = true; for (auto &e : g[u]) { if (vis[e.d])continue; ret[e.d] = make_pair(u, e.w); q.emplace(e.d); } } return ret; } signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; Graph g(N); vector A(N - 1), B(N - 1); rep(i, 0, N - 1) { cin >> A[i] >> B[i]; addEdge(g, A[i], B[i]); } vector U(N); rep(i, 0, N) { cin >> U[i]; } int M; cin >> M; vector q; vector a(M), b(M), c(M); rep(i, 0, M) { cin >> a[i] >> b[i] >> c[i]; q.emplace_back(a[i], b[i]); } LowestCommonAncestor lca(g, q); vector res = lca.solve(0); auto t = antiArborescence(g, 0); Graph g2(N); rep(i, 1, N) { int p = t[i].first; addEdge(g2, i, p, U[i]); } Array dist; bfs(g2, 0, dist); int ans = 0; rep(i, 0, M) { int anc = res[i]; ans += (dist[a[i]] + dist[b[i]] - 2 * dist[anc] + U[anc]) * c[i]; } cout << ans << endl; return 0; }