import java.io.PrintWriter import java.util.* fun PrintWriter.solve() { val scan=FastScanner() val n = scan.nextInt() val k = scan.nextInt() //Making Graph-begin val cnt = IntArray(n + k) val a = IntArray(n - 1) val b = IntArray(n - 1) val c = IntArray(n - 1) for (i in 0 until n - 1) { a[i] = scan.nextInt() - 1 b[i] = scan.nextInt() - 1 c[i] = scan.nextInt() ++cnt[a[i]] ++cnt[b[i]] } val m = IntArray(k) val p = IntArray(k) val x = Array(k) {IntArray(0)} for (i in 0 until k) { m[i] = scan.nextInt() p[i] = scan.nextInt() cnt[n + i] += m[i] x[i] = IntArray(m[i]) { scan.nextInt() - 1 } for (j in x[i]) ++cnt[j] } val g = Array(n + k) { Array(0) { Pair(0, 0) } } for (i in 0 until n + k) g[i] = Array(cnt[i]) { Pair(0, 0) } for (i in 0 until n - 1) { g[a[i]][--cnt[a[i]]] = Pair(b[i], c[i]) g[b[i]][--cnt[b[i]]] = Pair(a[i], c[i]) } for (i in 0 until k) { for (j in x[i]) { g[n + i][--cnt[n + i]] = Pair(j, 0) g[j][--cnt[j]] = Pair(n + i, p[i]) } } //Making Graph-end //LCA-begin val logn = 16 val dep = IntArray(n) { -1 } val dis = LongArray(n) { 0 } val nxt = Array(logn + 1) { Array(n) { -1 } } val que = IntArray(n) var qf = 0 var qb = 0 dep[0] = 0 que[qb++]=0 while (qb > qf) { val cur = que[qf++] for ((to, cost) in g[cur]) { if (to < n && dep[to] == -1) { dep[to] = dep[cur] + 1 dis[to] = dis[cur] + cost nxt[0][to] = cur que[qb++] = to } } } nxt[0][0] = 0 for (j in 0 until logn) { for (i in 0 until n) { nxt[j + 1][i] = nxt[j][nxt[j][i]] } } fun lca(x: Int, y: Int): Int { var a = x var b = y if (dep[a] > dep[b]) { a = y b = x } val d = dep[b] - dep[a] for (j in logn downTo 0) { if (d and (1 shl j) > 0) b = nxt[j][b] } if (a == b) return a for (j in logn downTo 0) { if (nxt[j][a] != nxt[j][b]) { a = nxt[j][a] b = nxt[j][b] } } return nxt[0][a] } //LCA-end val q = scan.nextInt() val ans = LongArray(q) { 0L } val u = IntArray(q) { 0 } val v = IntArray(q) { 0 } for (i in 0 until q) { u[i] = scan.nextInt() - 1 v[i] = scan.nextInt() - 1 ans[i] = dis[u[i]] + dis[v[i]] - 2 * dis[lca(u[i], v[i])] } //Dijkstra-begin val pq = PriorityQueue() for (i in 0 until k) { val dp = LongArray(n + k) { 1000000000000000000L } dp[n + i] = 0 pq.add((n + i).toLong()) while (pq.isNotEmpty()) { var d = pq.remove() val cur = (d % (n + k)).toInt() d /= n + k if (dp[cur] < d) continue for ((to, cost) in g[cur]) { if (dp[to] > d + cost) { dp[to] = d + cost pq.add((d + cost) * (n + k) + to) } } } for (j in 0 until q) { if (ans[j] > dp[u[j]] + dp[v[j]] + p[i]) { ans[j] = dp[u[j]] + dp[v[j]] + p[i] } } } //Dijkstra-end for (x in ans) println(x) } fun main() { val writer = PrintWriter(System.out, false) writer.solve() writer.flush() } private class FastScanner { private val printable = 33..126 private val lb = '0'.toByte() private val rb = '9'.toByte() val `in` = System.`in` val buffer = ByteArray(65536) var ptr = 0 var buflen = 0 fun hasNextByte(): Boolean { if (ptr < buflen) { return true } else { ptr = 0 buflen = `in`.read(buffer) if (buflen <= 0) { return false } } return true } fun readByte(): Byte { return if (hasNextByte()) buffer[ptr++] else return -1 } fun hasNext(): Boolean { while (hasNextByte() && buffer[ptr] !in printable) ptr++ return hasNextByte() } fun next(): String { val sb = StringBuilder() var b = readByte() while (b in printable) { sb.appendCodePoint(b.toInt()) b = readByte() } return sb.toString() } fun nextInt(): Int { var n = 0 var b = readByte() while (b < lb || b > rb) b = readByte() while (lb <= b && b <= rb) { n *= 10 n += b - '0'.toByte() b = readByte() } return n } fun nextDouble(): Double { return next().toDouble() } }