#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MAXN = 100000 + 10; vector<PII> G[MAXN]; int dfn[MAXN], st[MAXN], ed[MAXN]; int fa[MAXN][20], dep[MAXN], dis[MAXN]; int n, nq, idx; void dfs(int u, int f = -1) { dfn[u] = st[u] = idx++; if (!~f) { dep[u] = dis[u] = 0; } for (auto &x : G[u]) { if (x.first != f) { int v = x.first, w = x.second; fa[v][0] = u; dep[v] = dep[u] + 1; dis[v] = dis[u] + w; dfs(v, u); } } ed[u] = idx++; } namespace LCA { const static int POW = 18; void build(int n) { for (int i = 1; (1 << i) <= n; ++i) { for (int j = 0; j < n; ++j) { if (~fa[j][i - 1]) { fa[j][i] = fa[fa[j][i - 1]][i - 1]; } } } } int up(int u, int d) { for (int i = 0; d; ++i, d >>= 1) { if (d & 1) { u = fa[u][i]; } } return u; } int ask(int u, int v) { if (dep[u] < dep[v]) { swap(u, v); } u = up(u, dep[u] - dep[v]); for (int i = POW; i >= 0; --i) { if (fa[u][i] == fa[v][i]) { continue; } u = fa[u][i]; v = fa[v][i]; } if (u != v) { u = fa[u][0]; } return u; } } struct Edge { int v, w, nx; } E[MAXN << 1]; int sz[MAXN], vis[MAXN], mark[MAXN]; int a[MAXN * 10], q[MAXN], g[MAXN], eid; bool cmp(int a, int b) { return dfn[a] < dfn[b]; } void addedge(int u, int v) { int z = LCA::ask(u, v); int w = dis[u] + dis[v] - 2 * dis[z]; E[eid].v = v; E[eid].w = w; E[eid].nx = g[u]; g[u] = eid++; E[eid].v = u; E[eid].w = w; E[eid].nx = g[v]; g[v] = eid++; } LL f[MAXN]; void dp1(int u, int p = -1) { f[u] = 0; sz[u] = mark[u]; for (int it = g[u]; ~it; it = E[it].nx) { if (E[it].v != p) { int v = E[it].v, w = E[it].w; dp1(v, u); f[u] += f[v] + (LL) w * sz[v]; sz[u] += sz[v]; } } } void dp2(int u, int p = -1) { for (int it = g[u]; ~it; it = E[it].nx) { if (E[it].v != p) { int v = E[it].v, w = E[it].w; f[v] = f[u] + LL(sz[a[0]] - sz[v] * 2) * w; dp2(v, u); } } } void solve(int tot, int a[]) { int m = tot, i, x, t; sort(a, a + m, cmp); for (int i = 0; i < m; ++i) { vis[a[i]] = 1; } for (i = 1; i < m; ++i) { if (!vis[x = LCA::ask(a[i], a[i - 1])]) { vis[a[tot++] = x] = 1; } } m = tot; sort(a, a + m, cmp); eid = 0; for (q[t = 1] = a[0], i = 1; i < m; q[++t] = a[i++]) { while (st[a[i]] < st[q[t]] || ed[a[i]] > ed[q[t]]) { --t; } addedge(q[t], a[i]); } dp1(a[0]); dp2(a[0]); LL ret(0); for (int i = 0; i < m; ++i) { if (mark[a[i]]) { ret += f[a[i]] * mark[a[i]]; } } ret /= 2; printf("%lld\n", ret); for (int i = 0; i < m; ++i) { vis[a[i]] = mark[a[i]] = 0; g[a[i]] = -1; } } int main() { scanf("%d%d", &n, &nq); memset(fa, -1, sizeof(fa)); memset(g, -1, sizeof(g)); for (int i = 1; i < n; ++i) { int u, v, w; scanf("%d%d%d", &u, &v, &w); --u; --v; G[u].push_back(PII(v, w)); G[v].push_back(PII(u, w)); } idx = 0; dfs(0); LCA::build(n); while (nq--) { int k; scanf("%d", &k); for (int i = 0; i < k; ++i) { scanf("%d", a + i); a[i]--; mark[a[i]]++; } sort(a, a + k); k = unique(a, a + k) - a; solve(k, a); } return 0; }