#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 500000 + 5;
const int LOG = 20;
const ll INFLL = (1LL<<60);
int N, Q;
vector<int> tree[MAXN];
int up[MAXN][LOG];
int depth_[MAXN], tin[MAXN], tout[MAXN], timer_;
int sz[MAXN];
void dfs_init(int u, int p){
tin[u] = ++timer_;
up[u][0] = p;
sz[u] = 1;
for(int k=1;k<LOG;k++) up[u][k] = up[ up[u][k-1] ][k-1];
for(int v: tree[u]) if(v!=p){
depth_[v] = depth_[u] + 1;
dfs_init(v, u);
sz[u] += sz[v];
}
tout[u] = ++timer_;
}
bool is_anc(int u, int v){ // u ancestor of v
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca(int u, int v){
if(is_anc(u,v)) return u;
if(is_anc(v,u)) return v;
for(int k=LOG-1;k>=0;k--){
if(!is_anc(up[u][k], v)) u = up[u][k];
}
return up[u][0];
}
int dist_tree(int u, int v){
int w = lca(u,v);
return depth_[u] + depth_[v] - 2*depth_[w];
}
// build virtual tree nodes from 'nodes_in' (contains specials + root)
// returns nodes (unique, sorted by tin) and fills adj (indices 0..m-1)
vector<int> build_virtual_tree(vector<int> nodes_in, vector<vector<int>> &adj){
// sort input by tin
sort(nodes_in.begin(), nodes_in.end(), [&](int a,int b){ return tin[a] < tin[b]; });
// add LCAs of adjacent nodes
int m0 = nodes_in.size();
for(int i=0;i<m0-1;i++){
nodes_in.push_back(lca(nodes_in[i], nodes_in[i+1]));
}
sort(nodes_in.begin(), nodes_in.end(), [&](int a,int b){ return tin[a] < tin[b]; });
nodes_in.erase(unique(nodes_in.begin(), nodes_in.end()), nodes_in.end());
int m = (int)nodes_in.size();
adj.assign(m, {});
unordered_map<int,int> idx; idx.reserve(m*2);
for(int i=0;i<m;i++) idx[nodes_in[i]] = i;
// stack to connect edges
vector<int> st;
st.push_back(nodes_in[0]);
for(int i=1;i<m;i++){
int u = nodes_in[i];
while(!st.empty() && !is_anc(st.back(), u)) st.pop_back();
if(st.empty()){
// defensive: shouldn't happen with LCAs added, but handle
st.push_back(u);
continue;
}
int p = st.back();
int pi = idx[p], ui = idx[u];
adj[pi].push_back(ui);
adj[ui].push_back(pi);
st.push_back(u);
}
return nodes_in;
}
// Dijkstra on virtual tree (nodes: original ids vector, adj: adjacency by indices)
// sources: vector of original ids (specials + root)
vector<int> dijkstra_virtual(const vector<int> &nodes, const vector<vector<int>> &adj, const vector<int> &sources){
int m = (int)nodes.size();
unordered_map<int,int> idx; idx.reserve(m*2);
for(int i=0;i<m;i++) idx[nodes[i]] = i;
vector<ll> dist(m, INFLL);
vector<int> owner(m, INT_MAX);
// min-heap: (dist, owner, index)
using T = tuple<ll,int,int>;
priority_queue<T, vector<T>, greater<T>> pq;
for(int s: sources){
auto it = idx.find(s);
if(it == idx.end()) continue; // should not happen
int si = it->second;
if(dist[si] > 0 || (dist[si]==0 && owner[si] > s)){
dist[si] = 0;
owner[si] = s;
pq.emplace(0LL, s, si);
}
}
while(!pq.empty()){
auto [d, own, u] = pq.top(); pq.pop();
if(d > dist[u]) continue;
if(d==dist[u] && own > owner[u]) continue;
for(int v : adj[u]){
ll nd = d + (ll)dist_tree(nodes[u], nodes[v]);
if(nd < dist[v] || (nd == dist[v] && own < owner[v])){
dist[v] = nd;
owner[v] = own;
pq.emplace(nd, own, v);
}
}
}
return owner; // owner[i] = original id of special that claims nodes[i]
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for(int i=0;i<N-1;i++){
int u,v; cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
timer_ = 0;
depth_[1] = 0;
dfs_init(1, 1);
cin >> Q;
vector<ll> ans(N+1, 0);
while(Q--){
int m; cin >> m;
vector<int> special(m);
for(int i=0;i<m;i++) cin >> special[i];
// save original order to print later
vector<int> original_special = special;
// add root (1)
special.push_back(1);
// build virtual tree
vector<vector<int>> adj;
vector<int> nodes = build_virtual_tree(special, adj);
int M = nodes.size();
// run dijkstra on virtual tree
vector<int> owner = dijkstra_virtual(nodes, adj, special); // size M
// root virtual tree at index 0 and compute children (directed)
vector<int> parent(M, -1);
vector<vector<int>> children(M);
// BFS/stack to set parent
vector<int> stk;
stk.reserve(M);
stk.push_back(0);
parent[0] = -2;
for(int i=0;i<(int)stk.size();i++){
int u = stk[i];
for(int v : adj[u]){
if(parent[v] == -1){
parent[v] = u;
children[u].push_back(v);
stk.push_back(v);
}
}
}
// compute segment sizes: seg[u] = sz[nodes[u]] - sum(sz[nodes[child]])
vector<ll> seg(M);
for(int i=0;i<M;i++) seg[i] = (ll)sz[nodes[i]];
for(int u=0; u<M; ++u){
for(int v : children[u]){
seg[u] -= (ll)sz[nodes[v]];
}
}
// add to answers: ans[ owner[i] ] += seg[i]
unordered_set<int> usedOwners;
usedOwners.reserve(M*2);
for(int i=0;i<M;i++){
int o = owner[i];
// safety: if owner[i] was not set (INT_MAX) - shouldn't happen because root included
if(o==INT_MAX) continue;
ans[o] += seg[i];
usedOwners.insert(o);
}
// print answers in original order
for(int x : original_special){
cout << ans[x] << " ";
}
cout << "\n";
// reset ans for used owners
for(int o : usedOwners) ans[o] = 0;
}
return 0;
}