#include using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #define let auto const struct lazy_segment_tree { using T = i64; using L = i64; T t_ide() { return 0; } L l_ide() { return 0; } T ope(const T& a, const T& b) { return a + b; } L lazy_ope(const L& a, const L& b) { return b; } T effect(const T& t, const L& l, const i64 len) { return l * len; } i64 n; vector node; vector lazy; vector flag; /* create lazy_segment_tree for sequence [t_ide; sz]A */ lazy_segment_tree(vector init) { n = 1; while(n < init.size()) n *= 2; node.assign(n * 2, t_ide()); lazy.assign(n * 2, l_ide()); flag.assign(n * 2, false); for(int i = 0;i < init.size();i++) node[i + n - 1] = init[i]; for(int i = n - 2;i >= 0;i--) node[i] = ope(node[i * 2 + 1], node[i * 2 + 2]); } void eval(i64 l, i64 r, i64 k) { if(flag[k]) { node[k] = effect(node[k], lazy[k], r - l); if(r - l > 1) { lazy[k * 2 + 1] = lazy_ope(lazy[k * 2 + 1], lazy[k]); flag[k * 2 + 1] = true; lazy[k * 2 + 2] = lazy_ope(lazy[k * 2 + 2], lazy[k]); flag[k * 2 + 2] = true; } flag[k] = false; lazy[k] = l_ide(); } } /* for i in [a, b) effect(a[i], lx) */ void update(i64 a, i64 b, L lx, i64 l = 0, i64 r = -1, i64 k = 0) { if(r < 0) r = n; eval(l, r, k); if(b <= l || r <= a) return; else if(a <= l && r <= b) { lazy[k] = lazy_ope(lazy[k], lx); flag[k] = true; eval(l, r, k); } else { update(a, b, lx, l, (l + r) / 2, k * 2 + 1); update(a, b, lx, (l + r) / 2, r, k * 2 + 2); node[k] =ope(node[k * 2 + 1], node[k * 2 + 2]); } } /* the sum of a[i] for i in [a, b) */ T get(i64 a, i64 b, i64 l = 0, i64 r = -1, i64 k = 0) { if(r < 0) r = n; eval(l, r, k); if(b <= l || r <= a) return t_ide(); else if(a <= l && r <= b) return node[k]; else return ope(get(a, b, l, (l + r) / 2, k * 2 + 1), get(a, b, (l + r) / 2, r, k * 2 + 2)); } }; vector vec; vector idx; vector L1; vector R1; vector L2; vector R2; vector p; vector> G; int main() { cin.tie(nullptr); std::ios::sync_with_stdio(false); i64 N; cin >> N; idx.resize(N + 1, -1); L1.resize(N + 1, -1); R1.resize(N + 1, -1); L2.resize(N + 1, -1); R2.resize(N + 1, -1); p.resize(N + 1, -1); G.resize(N + 1); for(int i = 0;i < N - 1; i++) { i64 a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } G[N].push_back(0); queue que; que.push(N); idx[N] = vec.size(); vec.push_back(N); while(!que.empty()) { i64 v = que.front(); que.pop(); for(auto x: G[v]) { if(idx[x] != -1) continue; que.push(x); idx[x] = vec.size(); vec.push_back(x); p[x] = v; if(L1[v] == -1) L1[v] = idx[x]; R1[v] = idx[x] + 1; i64 pp = p[v]; if(pp != -1) { if(L2[pp] == -1) L2[pp] = idx[x]; R2[pp] = idx[x] + 1; } } } vector A(N + 1, 0); for(int i = 0;i < N;i++) cin >> A[idx[i]]; lazy_segment_tree seg(A); i64 Q; cin >> Q; for(int q = 0;q < Q;q++) { i64 x; cin >> x; i64 ad = 0; if(p[p[x]] != -1) { ad += seg.get(idx[p[p[x]]], idx[p[p[x]]] + 1); } ad += seg.get(idx[p[x]], idx[p[x]] + 1); ad += seg.get(L1[p[x]], R1[p[x]]); if(L1[x] != -1) ad += seg.get(L1[x], R1[x]); if(L2[x] != -1) ad += seg.get(L2[x], R2[x]); if(p[p[x]] != -1) { seg.update(idx[p[p[x]]], idx[p[p[x]]] + 1, 0); } seg.update(idx[p[x]], idx[p[x]] + 1, 0); seg.update(L1[p[x]], R1[p[x]], 0); if(L1[x] != -1) seg.update(L1[x], R1[x], 0); if(L2[x] != -1) seg.update(L2[x], R2[x], 0); seg.update(idx[x], idx[x] + 1, ad); cout << ad << endl; } }