#include <cstdio> #include <vector> #include <algorithm> #include <vector> using namespace std; #define MP make_pair #define A first #define B second int N,T,K; vector<pair<int,int> > adj[100013]; int visited[20][100013]; int root[100013][20]; int side[100013][20]; int dist[100013][20]; int s[100013]; int level[100013]; vector<int> cur[100013]; inline void dfs(int x, int w) { visited[w][x] = 1; s[x] = 1; for (auto i: adj[x]) if (visited[w][i.A]<1) { dfs(i.A,w); s[x]+=s[i.A]; } } inline int look(int x, int w, int tot) { visited[w][x] = 2; int ok = 1; for (auto i: adj[x]) if (visited[w][i.A]<2) { if (s[i.A]>tot/2) ok = 0; } if (tot-s[x]>tot/2) ok = 0; if (ok) return x; for (auto i: adj[x]) if (visited[w][i.A]<2) { int pos = look(i.A,w,tot); if (pos) return pos; } return 0; } inline void upd(int x, int w, int c, int d, int s) { visited[w][x] = 3; root[x][w] = c; side[x][w] = s; dist[x][w] = d; for (auto i: adj[x]) if (visited[w][i.A]<3) { upd(i.A,w,c,d+i.B,s); } } inline void decomp(int x, int w) { dfs(x,w); int c = look(x,w,s[x]); level[c] = w; for (int i=0;i<20;i++) visited[i][c] = 4; root[c][w] = c; cur[c].push_back(0); side[c][w] = 0; for (auto i: adj[c]) if (visited[w][i.A]<3) { cur[c].push_back(0); upd(i.A,w,c,i.B,cur[c].size()-1); } for (auto i: adj[c]) if (level[i.A]==-1) decomp(i.A,w+1); } int tot[100013]; vector<int> impt; inline int readint() { char c; while ((c=getchar_unlocked())<'0'); int res = c-'0'; while ((c=getchar_unlocked())>='0') res = res*10+c-'0'; return res; } #include <cassert> int main() { N = readint(); T = readint(); for (int i=1;i<N;i++) { int a,b,c; a = readint(); b = readint(); c = readint(); adj[a].push_back(MP(b,c)); adj[b].push_back(MP(a,c)); } for (int i=1;i<=N;i++) level[i] = -1; decomp(1,0); for (int i=1;i<=N;i++) assert(level[i]<20); for (int t=0;t<T;t++) { K = readint(); impt.clear(); for (int i=0;i<K;i++) { int x; x = readint(); impt.push_back(x); } for (int i: impt) for (int j=0;j<=level[i];j++) { tot[root[i][j]]+=1; cur[root[i][j]][side[i][j]]+=1; } long long ans = 0; for (int i: impt) for (int j=0;j<=level[i];j++) { ans+=(long long) dist[i][j]*(tot[root[i][j]]-cur[root[i][j]][side[i][j]]); } for (int i: impt) for (int j=0;j<=level[i];j++) { tot[root[i][j]] = 0; cur[root[i][j]][side[i][j]] = 0; } printf("%lld\n",ans); } return 0; }