#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (1LL<<60);
const int MAXN = 500000 + 5;
const int LOG = 20;
int N;
vector<int> tree[MAXN];
int up[MAXN][LOG];
int tin[MAXN], tout[MAXN], timerdfs;
int depthArr[MAXN];
int subSize[MAXN];
void dfs_prep(int u, int p){
tin[u] = ++timerdfs;
up[u][0] = (p == -1 ? u : p);
for(int k=1;k<LOG;k++) up[u][k] = up[ up[u][k-1] ][k-1];
subSize[u] = 1;
for(int v: tree[u]) if(v != p){
depthArr[v] = depthArr[u] + 1;
dfs_prep(v, u);
subSize[u] += subSize[v];
}
tout[u] = ++timerdfs;
}
inline bool is_anc(int a, int b){ // a ancestor of b
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int lca(int a, int b){
if(is_anc(a,b)) return a;
if(is_anc(b,a)) return b;
for(int k=LOG-1;k>=0;k--){
if(!is_anc(up[a][k], b)) a = up[a][k];
}
return up[a][0];
}
inline int dist_tree(int a, int b){
int L = lca(a,b);
return depthArr[a] + depthArr[b] - 2*depthArr[L];
}
int idxArr[MAXN]; // mapping node -> index in virtual nodes (reset only for touched)
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for(int i=1;i<=N;i++){
tree[i].clear();
idxArr[i] = -1;
}
for(int i=0;i<N-1;i++){
int u,v; cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
timerdfs = 0;
depthArr[1] = 0;
dfs_prep(1, -1);
int Q; cin >> Q;
vector<ll> globalAns(N+1, 0);
while(Q--){
int m; cin >> m;
vector<int> specials(m);
for(int i=0;i<m;i++) cin >> specials[i];
// build virtual node list
vector<int> nodes = specials;
sort(nodes.begin(), nodes.end(), [&](int a,int b){ return tin[a] < tin[b]; });
int orig = nodes.size();
for(int i=0;i<orig-1;i++){
nodes.push_back(lca(nodes[i], nodes[i+1]));
}
sort(nodes.begin(), nodes.end(), [&](int a,int b){ return tin[a] < tin[b]; });
nodes.erase(unique(nodes.begin(), nodes.end()), nodes.end());
int K = nodes.size();
// map nodes -> indices (and remember touched to reset later)
vector<int> touched = nodes;
for(int i=0;i<K;i++) idxArr[nodes[i]] = i;
// build virtual tree adjacency (by indices), weighted edges = dist between original nodes
vector<vector<pair<int,int>>> adj(K);
vector<int> st;
st.push_back(nodes[0]);
for(int i=1;i<K;i++){
int cur = nodes[i];
while((int)st.size() > 1 && !is_anc(st.back(), cur)){
int v = st.back(); st.pop_back();
int p = st.back();
int vi = idxArr[v], pi = idxArr[p];
int w = dist_tree(p, v);
adj[pi].push_back({vi, w});
adj[vi].push_back({pi, w});
}
if(!is_anc(st.back(), cur)){
// now st.size()==1 and still not ancestor: connect and continue
int v = st.back(); st.pop_back();
if(!st.empty()){
int p = st.back();
int vi = idxArr[v], pi = idxArr[p];
int w = dist_tree(p, v);
adj[pi].push_back({vi, w});
adj[vi].push_back({pi, w});
}
st.push_back(v);
}
st.push_back(cur);
}
while(st.size() > 1){
int v = st.back(); st.pop_back();
int p = st.back();
int vi = idxArr[v], pi = idxArr[p];
int w = dist_tree(p, v);
adj[pi].push_back({vi, w});
adj[vi].push_back({pi, w});
}
// multi-source Dijkstra on virtual tree
using T = tuple<ll,int,int>; // dist, owner(original id), index
priority_queue<T, vector<T>, greater<T>> pq;
vector<ll> dist(K, INF);
vector<int> owner(K, INT_MAX);
for(int s : specials){
int si = idxArr[s];
if(si < 0) continue;
if(dist[si] > 0 || (dist[si]==0 && s < owner[si])){
dist[si] = 0;
owner[si] = s;
pq.emplace(0LL, s, si);
}
}
while(!pq.empty()){
auto [dcur, own, u] = pq.top(); pq.pop();
if(dcur != dist[u] || own != owner[u]) continue;
for(auto &e : adj[u]){
int v = e.first;
int w = e.second;
ll nd = dcur + (ll)w;
if(nd < dist[v] || (nd == dist[v] && own < owner[v])){
dist[v] = nd;
owner[v] = own;
pq.emplace(nd, own, v);
}
}
}
// orient virtual tree as rooted at index 0 to compute children
vector<int> parent(K, -1);
vector<vector<int>> children(K);
parent[0] = -2;
vector<int> bfs;
bfs.push_back(0);
for(size_t i=0;i<bfs.size();i++){
int u = bfs[i];
for(auto &e: adj[u]){
int v = e.first;
if(parent[v] == -1){
parent[v] = u;
children[u].push_back(v);
bfs.push_back(v);
}
}
}
// compute segment sizes and add to answers
vector<ll> seg(K);
for(int i=0;i<K;i++) seg[i] = subSize[ nodes[i] ];
for(int u=0; u<K; ++u){
for(int v : children[u]){
seg[u] -= subSize[ nodes[v] ];
}
}
// collect used owners to reset later
vector<int> usedOwners;
usedOwners.reserve(K);
for(int i=0;i<K;i++){
int o = owner[i];
if(o == INT_MAX) continue; // shouldn't happen
globalAns[o] += seg[i];
usedOwners.push_back(o);
}
// print answers in input order for this query
for(int s : specials){
cout << globalAns[s] << " ";
}
cout << "\n";
// reset globalAns for owners used (unique)
sort(usedOwners.begin(), usedOwners.end());
usedOwners.erase(unique(usedOwners.begin(), usedOwners.end()), usedOwners.end());
for(int o : usedOwners) globalAns[o] = 0;
// reset idxArr for touched nodes
for(int u : touched) idxArr[u] = -1;
}
return 0;
}