import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum E = 18; int N; int[] U, V; long[] W; int Q; int[] K; int[][] X; int[][] G; int zeit; int[] dis, fin; int[][] par; long[][] ws; int[] dep; void dfs(int u, int p, long pw) { dis[u] = zeit++; par[0][u] = p; ws[0][u] = pw; dep[u] = (p == -1) ? 0 : (dep[p] + 1); foreach (i; G[u]) { const v = U[i] ^ V[i] ^ u; if (v != p) { dfs(v, u, W[i]); } } fin[u] = zeit++; } Tuple!(int, long) lca(int u, int v) { long ret; foreach_reverse (e; 0 .. E) { if (dep[u] - (1 << e) >= dep[v]) { ret += ws[e][u]; u = par[e][u]; } if (dep[v] - (1 << e) >= dep[u]) { ret += ws[e][v]; v = par[e][v]; } } foreach_reverse (e; 0 .. E) { if (par[e][u] != par[e][v]) { ret += ws[e][u]; ret += ws[e][v]; u = par[e][u]; v = par[e][v]; } } if (u != v) { ret += ws[0][u]; ret += ws[0][v]; u = par[0][u]; v = par[0][v]; } return tuple(u, ret); } void main() { try { for (; ; ) { N = readInt(); U = new int[N - 1]; V = new int[N - 1]; W = new long[N - 1]; foreach (i; 0 .. N - 1) { U[i] = readInt(); V[i] = readInt(); W[i] = readLong(); } Q = readInt(); K = new int[Q]; X = new int[][Q]; foreach (q; 0 .. Q) { K[q] = readInt(); X[q] = new int[K[q]]; foreach (k; 0 .. K[q]) { X[q][k] = readInt(); } } G = new int[][N]; foreach (i; 0 .. N - 1) { G[U[i]] ~= i; G[V[i]] ~= i; } zeit = 0; dis = new int[N]; fin = new int[N]; par = new int[][](E, N); ws = new long[][](E, N); dep = new int[N]; const rt = 0; dfs(rt, -1, 0); par[0][rt] = rt; foreach (e; 0 .. E - 1) { foreach (u; 0 .. N) { par[e + 1][u] = par[e][par[e][u]]; ws[e + 1][u] = ws[e][u] + ws[e][par[e][u]]; } } foreach (q; 0 .. Q) { X[q].sort!((a, b) => (dis[a] < dis[b])); auto us = X[q].dup; foreach (k; 0 .. K[q] - 1) { us ~= lca(X[q][k], X[q][k + 1])[0]; } us.sort!((a, b) => (dis[a] < dis[b])); us = us.uniq.array; debug { writeln("us = ", us); } long ans; DList!int stack; foreach (u; us) { for (; !stack.empty && fin[stack.back] < dis[u]; ) { stack.removeBack; } if (!stack.empty) { ans += lca(stack.back, u)[1]; } stack.insertBack(u); } writeln(ans); } } } catch (EOFException e) { } }