#include #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define FORR(i,a,b) for (int i=(a);i>=(b);i--) #define pb push_back #define pcnt __builtin_popcount #define show(x) cout<<#x<<" = "< pii; typedef vector vi; typedef vector vvi; typedef vector vpii; typedef set si; typedef pair pll; typedef vector vl; typedef vector vvl; typedef vector vpll; typedef set sl; templatestring join(vector&v) {stringstream s;FOR(i,0,sz(v))s<<' '<b)swap(a,b);for(;a>0;b%=a,swap(a,b));return b;} int modpow(ll a,ll n,int m){if(a==0)return a;ll p=1;for(;n>0;n/=2,a=a*a%m)if(n&1)p=p*a%m;return(int)p;} void dout(double d){printf("%.12f\n",d);} const int iinf = 1e9; const ll linf = 1e18; const int mod = 1e9+7; const double pi = acos(-1); const double eps = 1e-10; const int N = 100005; int n, p[N][17], d[N], u[N], su[N]; queue q, q2; vi m[N]; bool B[N]; int root(int d1, int d2){ FORR(i, 16, 0)if(p[d1][i] != p[d2][i]) return root(p[d1][i], p[d2][i]); return p[d1][0]; } main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n; int a, b, c; FOR(i, 0, n-1){ cin >> a >> b; m[a].pb(b); m[b].pb(a); } FOR(i, 0, n) cin >> u[i]; a = 0; q.push(0); B[0] = true; su[0] = u[0]; while(!q.empty()){ a++; while(!q.empty()){ b = q.front(); q.pop(); each(itr, m[b]){ if(B[*itr]) continue; B[*itr] = true; p[*itr][0] = b; d[*itr] = a; su[*itr] = su[b] + u[*itr]; q2.push(*itr); } } swap(q, q2); } FOR(i, 1, 17)FOR(j, 0, n) p[j][i] = p[p[j][i-1]][i-1]; ll ans = 0; int M; cin >> M; FOR(i, 0, M){ cin >> a >> b >> c; if(d[a] > d[b]) swap(a, b); int w = d[b] - d[a], x = b; FORR(j, 16, 0)if((1<