#include <bits/stdc++.h> #define __ ios_base::sync_with_stdio(0);cin.tie(0); #define all(x) x.begin(),x.end() #define mp make_pair #define pb push_back #define sz size() #define s second #define f first #define LL long long using namespace std; const int maxn = 1e5 + 7; const int inf = 1 << 30; const int mod = 1e9 + 7; int n, T, x, y, z, m, city[maxn * 10], pre[maxn]; LL k, timer, lg[4*maxn], tin[maxn], tout[maxn]; LL w[maxn]; LL h[maxn]; LL ans, res; map < pair < LL, LL >, LL > edge; LL s[maxn]; LL ok[maxn]; LL fr[maxn]; pair < LL, LL > mn[4*maxn][20]; vector < pair <int, int> > g[maxn]; vector <LL> o; inline void dfs(int v, int par, int high) { o.pb(v); if(fr[v] == -1) fr[v] = o.sz; h[v] = high; pre[v] = par; s[v] = s[par] + w[v]; for(auto x : g[v]) { int to = x.f; if(to != par) { w[to] = x.s; dfs(to, v, high + 1); o.pb(v); } } tout[++timer] = v; } inline LL lca(LL a, LL b) { a = fr[a], b = fr[b]; if(a > b) swap(a, b); int LG = lg[b - a + 1]; return min(mn[a][LG], mn[b - (1<<LG) + 1][LG]).s; } #define fname "" int main() { #ifdef LOCAL freopen(fname".in", "r", stdin); freopen(fname".out", "w", stdout); #endif scanf("%d %d", &n, &T); for(LL i = 1; i < n; ++i) { scanf("%d %d %d", &x, &y, &z); g[x].pb(mp(y, z)); g[y].pb(mp(x, z)); } for(int i = 1; i <= n; ++i) fr[i] = -1; dfs(1, 0, 1); lg[1] = 0; for(int i = 2; i <= o.sz; ++i) lg[i] = lg[i>>1] + 1; for(int i = 1; i <= o.sz; ++i) mn[i][0] = mp(h[o[i-1]], o[i-1]); for(int k = 1; k <= lg[o.sz]; ++k) for(int i = 1; i + (1 << k) - 1 <= o.sz; ++i) mn[i][k] = min(mn[i][k-1], mn[i+(1<<(k-1))][k-1]); while(T--) { ans = 0; LL a, b, c; scanf("%d", &m); for(LL i = 1; i <= m; ++i) scanf("%d", &city[i]); if(m <= 400) { for(int i = 1; i <= m; ++i) { for(int j = i + 1; j <= m; ++j) { LL a = city[i], b = city[j]; LL c = lca(a, b); ans += s[a] + s[b] - s[c] - s[c]; } } } else { for(int i = 1; i <= m; ++i) ok[city[i]]++; for(int i = 1; i <= n; ++i) ans += ok[tout[i]] * (m - ok[tout[i]]) * w[tout[i]], ok[pre[tout[i]]] += ok[tout[i]]; for(int i = 1; i <= n; ++i) ok[i] = 0; } printf("%lld\n", ans); } return 0; }