#include #include using namespace std; using ll = long long; using S = ll; using F = bool; S e(){return 0;} S op(S l, S r){return l + r;} S mapping(F f, S x){return f ? 0 : x;} F composition(F f, F g){return f | g;} F id(){return false;} int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, u, v, Q; cin >> n; vector> g(n); vector ent(n, -1), par(n, -1), mn(n, n), mx(n, -1), mn2(n, n), mx2(n, -1); vector tmp(n); for(int i = 1; i < n; i++){ cin >> u >> v; g[u].emplace_back(v); g[v].emplace_back(u); } queue nxt; nxt.emplace(0); for(int timer = 0; timer < n; timer++){ v = nxt.front(); nxt.pop(); ent[v] = timer; int p = par[v]; if(p != -1){ mn[p] = min(mn[p], timer); mx[p] = timer; p = par[p]; if(p != -1){ mn2[p] = min(mn2[p], timer); mx2[p] = timer; } } for(auto &&u : g[v]){ if(par[v] == u) continue; par[u] = v; nxt.emplace(u); } } for(int i = 0; i < n; i++){ cin >> v; tmp[ent[i]] = v; } atcoder::lazy_segtree seg(tmp); auto update = [&](int l, int r){ if(l == n) return 0ll; ll res = seg.prod(l, r); seg.apply(l, r, true); return res; }; auto f = [&](int v){ ll ans = 0; if(v){ int p = par[v]; ans += update(mn[p], mx[p] + 1); ans += seg.get(ent[p]); seg.set(ent[p], 0); if(par[p] != -1){ ans += seg.get(ent[par[p]]); seg.set(ent[par[p]], 0); } }else{ ans += seg.get(ent[0]); seg.set(ent[0], 0); } ans += update(mn[v], mx[v] + 1); ans += update(mn2[v], mx2[v] + 1); seg.set(ent[v], ans); return ans; }; cin >> Q; while(Q--){ cin >> v; cout << f(v) << '\n'; } }