#include using namespace std; typedef vector VI; typedef vector VVI; typedef vector VS; typedef pair PII; typedef long long LL; typedef pair PLL; #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second template istream& operator>>(istream& is, pair& p){ return is >> p.FF >> p.SS; } const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9+7; struct Edge{ int to, cost; Edge(int t, int c): to(t), cost(c) {} bool operator>(const Edge& rhs) const{ return cost > rhs.cost; } bool operator<(const Edge& rhs) const{ return cost < rhs.cost; } }; typedef vector< vector > Graph; // calc Lowest Common Ancestor // O(N logN) int N; const int MAX = 1<<17; const int LOG_MAX = 18; int depth[MAX]; int parent[LOG_MAX][MAX]; Graph G(MAX); LL cost[MAX]; LL sumcost[MAX]; void dfs(int node, int p = -1, int d = 0) { if(depth[node] >= 0) return; depth[node] = d; parent[0][node] = p; sumcost[node] = (p>=0?sumcost[p]:0) + cost[node]; for(int i=0;i depth[v]) swap(u, v); //depth[u] <= depth[v] for(int i=0;i> i & 1) v = parent[i][v]; if(u == v) return u; for(int i=LOG_MAX-1;i>=0;--i){ if(parent[i][u] != parent[i][v]){ u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } int main(){ cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; REP(i,N-1){ int u, v; cin >> u >> v; G[u].EB(v,1); G[v].EB(u,1); } REP(i,N) cin >> cost[i]; init(); int M; cin >> M; LL ans = 0; REP(i,M){ int a, b, c; cin >> a >> b >> c; if(a == b){ ans += c * cost[a]; continue; } int u = LCA(a,b); ans += c*(sumcost[a] + sumcost[b] - sumcost[u]*2 + cost[u]); } cout << ans << endl; return 0; }