#include using namespace std; using ll = long long; using vi = vector; using Graph1 = vector>; #define rep(i,n) for(ll i=0;i=0;i--) #define all(x) (x).begin(),(x).end() const ll INF = 1LL<<60; // LCA(最近共通祖先) struct LCA{ vector> parent; vector dist; LCA(const Graph1 &g, int root ) { init(g,root);} void init(const Graph1 &g,int root){ int V = g.size(); int K = 1; while((1<(V,-1)); dist.assign(V,-1); bfs(g,root); for(int k=0;k+1 todo; parent[0][v] = -1; dist[v] = 0; todo.push(v); while(!todo.empty()){ int pos = todo.front(); todo.pop(); for(int x:g[pos]){ if(dist[x]!=-1) continue; parent[0][x] = pos; dist[x] = dist[pos]+1; todo.push(x); } } } int query(int u,int v){ if(dist[u]> k&1){ u = parent[k][u]; } } if(u==v) return u; for(int k=K-1;k>=0;k--){ if(parent[k][u]!=parent[k][v]){ u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } int get_dist(int u,int v){ return dist[u]+dist[v]-2*dist[query(u,v)]; } bool is_on_path(int u,int v,int a){ return get_dist(u,a)+get_dist(a,v) == get_dist(u,v); } }; int main(){ int n; cin >> n; Graph1 gr(n); rep(i,n-1){ int a,b; cin >> a >> b; gr[a].push_back(b); gr[b].push_back(a); } LCA lca = LCA(gr,0); vector seen(n,-1); vector one(n); rep(i,n){ int u; cin >> u; seen[i] = u; one[i] = u; } queue que; que.push(0); vector visi(n,false); visi[0] = true; while(!que.empty()){ int pos = que.front(); que.pop(); for(auto nex:gr[pos]){ if(visi[nex]) continue; visi[nex] = true; seen[nex]+=seen[pos]; que.push(nex); } } int m; cin >> m; ll ren = 0; rep(i,m){ int a,b,c; cin >> a >> b >> c; ll ans = seen[a]+seen[b]-2*seen[lca.query(a,b)]+one[lca.query(a,b)]; ren += ans*c; } cout <