// World Cup City #include <bits/stdc++.h> #define rf freopen("inp.in","r",stdin) #define F first #define S second #define PII pair<int,int> using namespace std; // SWEG INPUT inline int scan(){ int x = 0; char c = getchar_unlocked(); while(c < '0' || c > '9'){ c = getchar_unlocked(); } while(c >= '0' && c <= '9'){ x = (x << 3) + (x << 1) + c - '0'; c = getchar_unlocked(); } return x; } const int MAX = 400050; int N , T , K ; vector<PII>adjList[MAX]; int SUM[MAX] , ST[MAX] ; long long ans = 0; int S[MAX] , M[MAX] , cur = 0; int FLAT[MAX] , ORDER[MAX] , P[MAX] , L[MAX]; int LOG[MAX] , SPARSE_TABLE[20][MAX]; int FLAT_INV[MAX]; int timer = 0 , tim = 0; inline void buildLogTable( int v ){ int g = 2; while (g <= v) { LOG[g] = 1; g = (g << 1); } for (int i = 1; i <= v; i++) LOG[i] += LOG[i - 1]; } inline void buildSparseTable( int v ){ for( int i = 0 ; i < v ; i++) SPARSE_TABLE[0][i] = FLAT[i]; for (int j = 1; (1 << j) < v; j++) { for (int i = 0; i + (1 << j) <= v; i++) { int g = L[SPARSE_TABLE[j - 1][i]]; int p = L[SPARSE_TABLE[j - 1][i + (1 << (j - 1))]]; SPARSE_TABLE[j][i] = g < p ? SPARSE_TABLE[j - 1][i] : SPARSE_TABLE[j - 1][i + (1 << (j - 1))]; } } } inline void dfs( int node , int par , int cost , int depth){ ORDER[tim++] = node; SUM[node] = cost; P[node] = par; FLAT[timer++] = node; L[node] = depth; for( auto next : adjList[node]) if( next.F != par )dfs(next.F,node,next.S + cost , depth + 1) , FLAT[timer++] = node; } inline void precompute(){ dfs( 1 , 1 , 0 , 0); buildLogTable( 2 * N - 1 ); buildSparseTable( 2 * N - 1); for( int i = 2 * N - 2 ; i >= 0 ; i-- ) FLAT_INV[FLAT[i]] = i; } inline long long dp(){ long long res = 0; for( int i = N - 1 ; i >= 0 ; i--){ int node = i; for( auto nxt : adjList[node]){ if( nxt.F == P[node] ) continue; res += ST[nxt.F]*1LL*( K - ST[nxt.F])*nxt.S; ST[node] += ST[nxt.F]; } } return res; } inline int fun(int l, int r) { int k = LOG[r - l + 1]; return L[SPARSE_TABLE[k][l]] <= L[SPARSE_TABLE[k][r - (1 << k) + 1]] ? SPARSE_TABLE[k][l] : SPARSE_TABLE[k][r - (1 << k) + 1]; } inline int lca(int a, int b) { return fun(min(FLAT_INV[a], FLAT_INV[b]), max(FLAT_INV[a], FLAT_INV[b])); } inline int dist( int a , int b){ return SUM[a] + SUM[b] - ( SUM[lca(a,b)] << 1); } int main(){ N = scan() , T = scan(); for(int i = 1 ; i <= N - 1 ; i++){ int A , B , C; A = scan() , B = scan() , C = scan(); adjList[A].emplace_back(make_pair(B,C) ); adjList[B].emplace_back(make_pair(A,C) ); } precompute(); while(T--){ K = scan(); cur = 0 , ans = 0LL; for(int i = 1 ; i <= K ; i++){ int x ; x = scan(); S[++cur] = x; ST[x]++; } if( K >= 400 ){ ans = dp(); for( int i = 1 ; i <= N ; i++) ST[i] = 0LL; } else { for( int i = 1 ; i <= cur ; i++ ) for( int j = i+1 ; j <= cur ; j++) ans = ans + dist( S[i] , S[j] ); } printf("%lld\n",ans); } return 0; }