#include using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) const int xN = 100000; struct Edge { int to; LL dist; }; int N; vector E[xN]; int P[xN]; LL D[xN]; int dfsI[xN]; int lcaP[20][xN]; int lcaD[xN]; int dfs(int p = 0, int i = 0) { for (Edge& e : E[p]) { if (P[p] == e.to) continue; D[e.to] = D[p] + e.dist; lcaD[e.to] = lcaD[p] + 1; P[e.to] = p; i = dfs(e.to); } dfsI[p] = i++; return i; } void init() { rep(i, N) P[i] = -1; rep(i, N) D[i] = 0; dfs(0); rep(i, N) lcaP[0][i] = P[i]; lcaP[0][0] = 0; rep(d, 19) rep(i, N) lcaP[d + 1][i] = lcaP[d][lcaP[d][i]]; } int LCA(int u, int v) { if (lcaD[u] > lcaD[v]) swap(u, v); for (int i = 19; i >= 0; i--) if (lcaD[u] + (1 << i) <= lcaD[v]) v = lcaP[i][v]; if (u == v) return u; for (int i = 19; i >= 0; i--) if (lcaP[i][u] != lcaP[i][v]) { u = lcaP[i][u]; v = lcaP[i][v]; } return lcaP[0][u]; } LL distance(int u, int v) { int g = LCA(u, v); return D[u] + D[v] - 2 * D[g]; } int main() { cin >> N; rep(i, N - 1) { int u, v, c; cin >> u >> v >> c; E[u].push_back({ v,c }); E[v].push_back({ u,c }); } init(); int Q; cin >> Q; while (Q--) { int K; cin >> K; vector> X(K); rep(i, K) { int x; cin >> x; X[i] = { dfsI[x],x }; } sort(X.begin(), X.end()); X.push_back(X.front()); LL ans = 0; rep(i, K) ans += distance(X[i].second, X[i + 1].second); cout << (ans / 2) << endl; } return 0; }