import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 # md = 10**9+7 md = 998244353 class LCA: def __init__(self, parents, depth): self.n = len(parents) self.parents = parents self.depth = depth self.max_level = max(self.depth).bit_length() self.ancestor = [self.parents]+[[-1]*self.n for _ in range(self.max_level)] row0 = self.ancestor[0] for lv in range(self.max_level): row1 = self.ancestor[lv+1] for u in range(self.n): if row0[u] == -1: continue row1[u] = row0[row0[u]] row0 = row1 def anc(self, u, v): diff = self.depth[u]-self.depth[v] if diff < 0: u, v = v, u diff = abs(diff) lv = 0 while diff: if diff & 1: u = self.ancestor[lv][u] lv, diff = lv+1, diff >> 1 if u == v: return u for lv in range(self.depth[u].bit_length()-1, -1, -1): anclv = self.ancestor[lv] if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v] return self.parents[u] n, k = LI() to = [[] for _ in range(n+k)] for _ in range(n-1): u, v, c = LI() u, v = u-1, v-1 to[u].append((v, c)) to[v].append((u, c)) parent, depth, vis, dist = [-1]*n, [0]*n, [0]*n, [0]*n def dfs(root): vis[root] = 1 stack = [root] while stack: iu = stack.pop() i, u = divmod(iu, n) while i < len(to[u]) and vis[to[u][i][0]]: i += 1 if i < len(to[u]): stack.append((i+1)*n+u) v, c = to[u][i] vis[v] = 1 stack.append(v) dist[v] = dist[u]+c parent[v] = u depth[v] = depth[u]+1 dfs(0) lca = LCA(parent, depth) pp = [] for s in range(k): s += n m, p = LI() pp.append(p) for u in LI1(): to[u].append((s, p)) to[s].append((u, 0)) # pDB(pp) from heapq import * def dijkstra(to, root=0): n = len(to) dist = [inf]*n dist[root] = 0 hp = [(0, root)] while hp: d, u = heappop(hp) if d > dist[u]: continue for v, c in to[u]: nd = d+c if dist[v] <= nd: continue dist[v] = nd heappush(hp, (nd, v)) return dist dd = [] for s in range(n, n+k): dd.append(dijkstra(to, s)) # p2D(dd) for _ in range(II()): u, v = LI1() a = lca.anc(u, v) ans = dist[u]+dist[v]-dist[a]*2 # pDB(u, v, a, ans) for s in range(k): cur = dd[s][u]+dd[s][v]+pp[s] # pDB(s, cur, ans) if cur < ans: ans = cur print(ans)