import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static List> graph = new ArrayList<>(); static int[][] parents; static int[] depths; static long[] weights; static int[] dfs; static int current = 0; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new HashMap<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); graph.get(a).put(b, c); graph.get(b).put(a, c); } parents = new int[20][n]; depths = new int[n]; weights = new long[n]; dfs = new int[n]; setDepth(0, 0, 0, 0); for (int i = 1; i < 20; i++) { for (int j = 0; j < n; j++) { parents[i][j] = parents[i - 1][parents[i - 1][j]]; } } int q = sc.nextInt(); String result = IntStream.range(0, q).mapToObj(p -> { int k = sc.nextInt(); List list = IntStream.range(0, k) .map(i -> sc.nextInt()).boxed().sorted((a, b) -> dfs[a] - dfs[b]).toList(); long ans = 0; for (int i = 0; i < k; i++) { ans += getLength(list.get(i), list.get((i + 1) % k)); } return String.valueOf(ans / 2); }).collect(Collectors.joining("\n")); System.out.println(result); } static void setDepth(int idx, int p, int d, long v) { parents[0][idx] = p; depths[idx] = d; weights[idx] = v; dfs[idx] = current++; for (int x : graph.get(idx).keySet()) { if (x != p) { setDepth(x, idx, d + 1, v + graph.get(idx).get(x)); } } } static long getLength(int a, int b) { return weights[a] + weights[b] - weights[getLCA(a, b)] * 2; } static int getLCA(int a, int b) { if (depths[a] < depths[b]) { return getLCA(b, a); } for (int i = 19; i >= 0; i--) { if (depths[a] - depths[b] >= (1 << i)) { a = parents[i][a]; } } if (a == b) { return a; } for (int i = 19; i >= 0; i--) { if (parents[i][a] != parents[i][b]) { a = parents[i][a]; b = parents[i][b]; } } return parents[0][a]; } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }