#include using namespace std; typedef long long ll; typedef pair P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) struct LazySegmentTree { private: int n; vector node,lazy; vector upd; public: LazySegmentTree(vector v) { int sz = v.size(); n = 1; while (n < sz) n *= 2; node.resize(2*n-1); lazy.resize(2*n-1,0); upd.resize(2*n-1,false); for (int i = 0;i < sz;++i) node[i+n-1] = v[i]; for (int i = n-2;i >= 0;--i) node[i] = node[i*2+1]+node[i*2+2]; } void eval(int k,int l,int r) { if (upd[k]) { node[k] = lazy[k]*(r-l); if (r-l > 1) { lazy[2*k+1] = lazy[2*k+2] = lazy[k]; upd[2*k+1] = upd[2*k+2] = true; } lazy[k] = 0; upd[k] = false; } } void update(int a,int b,ll x,int k = 0,int l = 0,int r = -1) { if (r < 0) r = n; eval(k,l,r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] = x; upd[k] = true; eval(k,l,r); } else { update(a,b,x,2*k+1,l,(l+r)/2); update(a,b,x,2*k+2,(l+r)/2,r); node[k] = node[2*k+1]+node[2*k+2]; } } ll getsum(int a,int b,int k = 0,int l = 0,int r = -1) { if (r < 0) r = n; if (b <= l || r <= a) return 0; eval(k,l,r); if (a <= l && r <= b) return node[k]; ll vl = getsum(a,b,k*2+1,l,(l+r)/2); ll vr = getsum(a,b,2*k+2,(l+r)/2,r); return vl+vr; } }; int main() { int n; cin >> n; vector> edges(n); vector a(n); LazySegmentTree seg(vector(n,0)); for (int i = 0;i < n-1;++i) { int u,v; cin >> u >> v; edges[u].push_back(v); edges[v].push_back(u); } for (int i = 0;i < n;++i) cin >> a[i]; vector idx(n,-1),par(n),cl(n,-1),cr(n,-1),cl2(n,-1),cr2(n,-1); queue que; // vector

child(n,P(-1,-1)),child2(n,P(-1,-1)); // vector used(n,false); que.push(0); idx[0] = 0; par[0] = -1; int c = 0; while (!que.empty()) { int t = que.front(); que.pop(); // used[t] = true; int s = idx[t]; for (int& i : edges[t]) if (idx[i] < 0) { c++; idx[i] = c; par[c] = s; if (cl[s] < 0) cl[s] = c; cr[s] = c+1; if (par[s] >= 0) { if (cl2[par[s]] < 0) cl2[par[s]] = c; cr2[par[s]] = c+1; } que.push(i); } } for (int i = 0;i < n;++i) seg.update(idx[i],idx[i]+1,a[i]); int q; cin >> q; for (int z = 0;z < q;++z) { int x; cin >> x; int y = idx[x]; ll sum = seg.getsum(y,y+1); seg.update(y,y+1,0); if (par[y] >= 0) { sum += seg.getsum(par[y],par[y]+1); sum += seg.getsum(cl[par[y]],cr[par[y]]); seg.update(par[y],par[y]+1,0); seg.update(cl[par[y]],cr[par[y]],0); if (par[par[y]] >= 0) { int k = par[par[y]]; sum += seg.getsum(k,k+1); seg.update(k,k+1,0); } } if (cl[y] >= 0) { sum += seg.getsum(cl[y],cr[y]); seg.update(cl[y],cr[y],0); if (cl2[y] >= 0) { sum += seg.getsum(cl2[y],cr2[y]); seg.update(cl2[y],cr2[y],0); } } seg.update(y,y+1,sum); cout << sum << endl; } }