#include <bits/stdc++.h> using namespace std; #define g getchar_unlocked template<typename T> inline void getint(T &num) { num = 0; int ch = g(); while (!isdigit(ch)) { ch = g(); } while (isdigit(ch)) { num = num * 10 + ch - 48; ch = g(); } } const int N = 1e5 + 3; int n, q; bool isDone[N]; long long sum[N]; long long cnt[N]; long long ans[N]; vector<pair<int, int>> inQuery[N]; vector<pair<int, int>> adj[N]; int centerBest; int centerWhere; int findCenter(int u, int par, int allSize) { if (isDone[u]) return 0; int ret = 1; int cur = 0; for (auto it : adj[u]) { if (it.first != par) { int sv = findCenter(it.first, u, allSize); ret += sv; cur = max(cur, sv); } } if (allSize != 1e9) { cur = max(cur, allSize - 1 - cur); if (cur < centerBest) { centerBest = cur; centerWhere = u; } } return ret; } unordered_set<int> used; void dfs(int u, int par, int weight, bool addToCnt) { if (isDone[u]) return; if (!addToCnt) { for (auto it : inQuery[u]) { used.insert(it.first); ans[it.first] += cnt[it.first] * weight * it.second + sum[it.first] * it.second; } } else { for (auto it : inQuery[u]) { sum[it.first] += 1LL * it.second * weight; cnt[it.first] += it.second; } } for (auto it : adj[u]) { if (isDone[it.first] || it.first == par) continue; dfs(it.first, u, weight + it.second, addToCnt); } } void solve(int u) { if (isDone[u]) return; int size = findCenter(u, -1, 1e9); centerBest = 1e9; centerWhere = -1; findCenter(u, -1, size); u = centerWhere; used.clear(); for (auto it : adj[u]) { if (isDone[it.first]) continue; dfs(it.first, u, it.second, false); dfs(it.first, u, it.second, true); } for (auto it : inQuery[u]) { ans[it.first] += sum[it.first] * it.second; } for (int q : used) { cnt[q] = sum[q] = 0; } isDone[u] = true; for (auto it : adj[u]) { solve(it.first); } } int main() { getint(n); getint(q); for (int i = 0; i < n - 1; i++) { int u, v, w; getint(u); getint(v); getint(w); u--; v--; adj[u].push_back(make_pair(v, w)); adj[v].push_back(make_pair(u, w)); } for (int i = 0; i < q; i++) { int k; getint(k); unordered_map<int, int> mp; while (k--) { int x; getint(x); x--; mp[x]++; } for (auto it : mp) { inQuery[it.first].push_back(make_pair(i, it.second)); } } solve(0); for (int i = 0; i < q; i++) { printf("%lld\n", ans[i]); } return 0; }