#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define MAX_MOD 1000000007 #define REP(i,n) for(int i = 0;i < n;++i) vector sectors_first[200001]; vector sectors_cal[200001]; int kazu[200001] = {}; long long score[200001] = {}; int main() { int n; cin >> n; for (int i = 0;i < n-1;++i) { int a, b; scanf("%d%d",&a,&b); sectors_first[a].push_back(b); sectors_first[b].push_back(a); } for (int i = 0;i < n;++i) { scanf("%d", &kazu[i]); } score[150000] = 1; score[0] = kazu[0]+1;//1+ queue next_go; next_go.push(0); while (next_go.empty() == false) { int hoge = next_go.front(); next_go.pop(); for (int q = 0;q < sectors_first[hoge].size();++q) { if (score[sectors_first[hoge][q]] == 0) { sectors_cal[sectors_first[hoge][q]].push_back(hoge); score[sectors_first[hoge][q]] = kazu[sectors_first[hoge][q]] + score[hoge]; next_go.push(sectors_first[hoge][q]); } } } sectors_cal[0].push_back(150000); long long ans = 0; int m; cin >> m; for (int i = 0;i < m;++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); long long pre_ans = score[a] + score[b] - 2; bool ok[200000] = {}; queue hogee; hogee.push(a); hogee.push(b); while (hogee.empty() == false) { int wow = hogee.front();hogee.pop(); if (ok[wow] == false) { ok[wow] = true; if (wow != 0) { hogee.push(sectors_cal[wow][0]); } }else{ pre_ans -= score[wow] - 1+score[sectors_cal[wow][0]]-1; goto ok; } } ok:; ans += pre_ans * c; } cout << ans << endl; }