#include using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define each(e, v) for(auto &e: v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair; using pil = pair; using pli = pair; using pll = pair; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; template bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; struct io_setup{ io_setup(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; template struct Weighted_Graph{ struct edge{ int to; T cost; int id; edge(int to, T cost, int id) : to(to), cost(cost), id(id) {} }; vector> es; vector d; vector pre_v; vector keep; const T INF_T; const int n; int m; Weighted_Graph(int n) : es(n), d(n), pre_v(n), INF_T(numeric_limits::max()/2), n(n), m(0) {} void add_edge(int from, int to, T cost){ es[from].emplace_back(to, cost, m); if(!directed) es[to].emplace_back(from, cost, m); m++; } T dijkstra(int s, int t = 0){ fill(begin(d), end(d), INF_T); using P = pair; priority_queue, greater

> que; que.emplace(d[s] = 0, s); while(!que.empty()){ auto [p, i] = que.top(); que.pop(); if(p > d[i]) continue; for(auto &e: es[i]){ if(d[i]+e.cost < d[e.to]){ pre_v[e.to] = i, que.emplace(d[e.to] = d[i]+e.cost, e.to); } } } return d[t]; } vector shortest_path(int s, int t){ keep.clear(); if(dijkstra(s, t) == INF_T) return keep; for(int now = t; now != s; now = pre_v[now]) keep.push_back(now); keep.push_back(s), reverse(begin(keep), end(keep)); return keep; } }; template struct Weighted_Graph_2{ struct edge{ int to; T cost; int id; edge(int to, T cost, int id) : to(to), cost(cost), id(id) {} }; vector> es; vector> par; //par[i][j] := 頂点jの2^i個前の祖先 vector depth; vector di; const T INF_T; const int n, height; int m; Weighted_Graph_2(int n) : es(n), depth(n), di(n), INF_T(numeric_limits::max()/2), n(n), height(32-__builtin_clz(n)), m(0){ par.assign(height, vector(n)); } void add_edge(int from, int to, T cost){ es[from].emplace_back(to, cost, m); if(!directed) es[to].emplace_back(from, cost, m); m++; } void prepare(int now, int pre = -1){ if(pre == -1) depth[now] = 0, di[now] = 0; par[0][now] = pre; for(auto &e: es[now]){ if(e.to != pre){ depth[e.to] = depth[now]+1, di[e.to] = di[now]+e.cost; prepare(e.to, now); } } } void build(int root = 0){ prepare(root); for(int j = 0; j < height-1; j++){ for(int i = 0; i < n; i++){ if(par[j][i] == -1) par[j+1][i] = -1; else par[j+1][i] = par[j][par[j][i]]; } } } int lca(int u, int v){ if(depth[u] < depth[v]) swap(u, v); int D = depth[u]-depth[v]; for(int i = 0; i < height; i++){ if((D>>i)&1) u = par[i][u]; } if(u == v) return u; for(int i = height-1; i >= 0; i--){ if(par[i][u] != par[i][v]) u = par[i][u], v = par[i][v]; } return par[0][u]; } T dist(int u, int v){ return di[u]+di[v]-2*di[lca(u, v)]; } }; int main(){ int N, K; cin >> N >> K; Weighted_Graph G(N+K); Weighted_Graph_2 G2(N); rep(i, N-1){ int u, v; ll c; cin >> u >> v >> c; u--, v--; G.add_edge(u, v, c*2), G2.add_edge(u, v, c); } G2.build(0); rep(i, K){ int M; ll c; cin >> M >> c; while(M--){ int u; cin >> u; u--; G.add_edge(u, N+i, c); } } vector> d(K, vector(N+K)); rep(i, K){ G.dijkstra(N+i); d[i] = G.d; } int Q; cin >> Q; while(Q--){ int u, v; cin >> u >> v; u--, v--; ll ans = G2.dist(u, v); rep(i, K) chmin(ans, (d[i][u]+d[i][v])/2); cout << ans << '\n'; } }