#include <bits/stdc++.h> using namespace std; struct info { int d, p; // depth of lowest touched vertex on path, previous path bool operator< (const info& other) const { return d>other.d; } }; vector<info> v[100001]; int N, T, K; int A[1000001]; vector<pair<int, int>> adj[100001]; int parent[100001]; int depth[100001]; int sz[100001]; int son[100001]; int top[100001]; vector<int> seen; map<int, int> bsum; void dfs(int u, int p) { sz[u]=1; for(auto& it: adj[u]) { int v=it.first; int c=it.second; if(v==p) continue; parent[v]=u; depth[v]=depth[u]+c; dfs(v, u); sz[u]+=sz[v]; if(sz[v]>sz[son[u]]) son[u]=v; } } void dfs2(int u, int p) { if(son[parent[u]]==u) top[u]=top[parent[u]]; else top[u]=u; for(auto& it: adj[u]) { int v=it.first; if(v==p) continue; dfs2(v, u); } } void go(int u, int x) { int p=x; while(u) { v[top[u]].push_back({depth[u], p}); seen.push_back(top[u]); p=top[u]; u=parent[top[u]]; } } int main() { scanf("%d%d", &N, &T); int a, b, c; for(int i=1; i<N; i++) { scanf("%d%d%d", &a, &b, &c); adj[a].push_back({b, c}); adj[b].push_back({a, c}); } dfs(1, 1); dfs2(1, 1); while(T--) { scanf("%d", &K); for(int i=0; i<K; i++) scanf("%d", A+i); seen.clear(); long long ans=0; for(int i=0; i<K; i++) { go(A[i], -i-1); ans+=1LL*(K-1)*depth[A[i]]; } sort(seen.begin(), seen.end()); seen.resize(unique(seen.begin(), seen.end())-seen.begin()); for(auto& u: seen) { sort(v[u].begin(), v[u].end()); bsum.clear(); int cnt=0; for(auto& it: v[u]) { ans-=2LL*(cnt-bsum[it.p])*it.d; bsum[it.p]++; cnt++; } v[u].clear(); } printf("%lld\n", ans); } return 0; }