#include <iostream> #include <vector> #include <stack> #include <algorithm> using namespace std; const int N = 1 + 1e5, LG = 17; struct Edge{ int y, c; Edge(int y = 0, int c = 0) : y(y), c(c) {} }; int rmq[1 + LG][2 * N], depth[N], start[N], timp, n; vector<Edge> tree[N]; bool use[N]; void dfs(int x){ use[x] = true; start[x] = timp; rmq[0][ timp++ ] = x; for (Edge it : tree[x]) if (!use[ it.y ]){ depth[ it.y ] = it.c + depth[x]; dfs(it.y); rmq[0][ timp++ ] = x; } } inline int best(int x, int y){ return depth[x] < depth[y] ? x : y; } int query(int x, int y){ int L = 31 - __builtin_clz(y - x + 1); return min( depth[ rmq[L][x] ], depth[ rmq[L][y - (1 << L) + 1] ] ); } long long compute(vector<int>& v){ long long ans = (long long)depth[ rmq[0][ v.back() ] ] * ( v.size() - 1 ), cost = 0; stack<Edge> S; for (int i = v.size() - 1; i; i--){ int cnt = 1, val = query( v[i - 1], v[i] ); while ( !S.empty() && val <= S.top().y ){ cnt += S.top().c; cost -= (long long)S.top().y * S.top().c; S.pop(); } S.push( Edge(val, cnt) ); cost += (long long)val * cnt; ans += (long long)depth[ rmq[0][ v[i - 1] ] ] * ( v.size() - 1 ) - 2 * cost; } return ans; } int main(){ int nrQ, x, y, c; cin >> n >> nrQ; for (int i = 1 ; i < n ; i++){ cin >> x >> y >> c; tree[x].push_back( Edge(y, c) ); tree[y].push_back( Edge{x, c} ); } dfs(1); for (int i = 1, step = 1 ; i <= LG ; i++, step <<= 1) for (int j = 0 ; j + step < timp ; j++) rmq[i][j] = best(rmq[i - 1][j], rmq[i - 1][j + step]); vector<int> query; while (nrQ--){ cin >> c; query.resize(c); for (int i = 0; i < c; i++){ cin >> x; query[i] = start[x]; } sort( query.begin(), query.end() ); cout << compute(query) << '\n'; } return 0; }