import java.io.*; import java.util.*; public class BFS { private Queue<Integer> queue; static int[] D; static int N; BFS() { queue = new LinkedList<Integer>(); } void bfs(int adj[][], int source) { D=new int[N+1]; int[] visited = new int[N+ 1]; int i, element; visited[source] = 1; queue.add(source); D[source]=0; while (!queue.isEmpty()) { element = queue.remove(); i = element; while (i <= N) { if (adj[element][i] != 0 && visited[i] == 0) { D[i]=D[element]+adj[element][i]; queue.add(i); visited[i] = 1; } i++; } } } public int lowCA(int a,int b,int A[][]){ int x=0; for(int i=1;i<=N;i++){ if((A[i][a]*A[i][b])!=0){ x=i; i=N+1; } } if(x==0) return 0; else return x; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); N=sc.nextInt(); int T=sc.nextInt(); int[][] A=new int[N+1][N+1]; long sum=0; for(int i=1;i<N;i++){ int a=sc.nextInt(); int b=sc.nextInt(); A[a][b]=sc.nextInt(); } BFS bfs=new BFS(); bfs.bfs(A,1); while(T!=0){ sum=0; int K=sc.nextInt(); int[] C=new int[K+1]; for(int i=1;i<=K;i++){ C[i]=sc.nextInt(); } for(int i=1;i<K;i++){ for(int j=i+1;j<=K;j++){ if(C[i]==C[j]) break; else{ sum+=D[C[i]]+D[C[j]]-2*D[bfs.lowCA(C[i],C[j],A)]; } } } System.out.println(sum); T--; } } }