import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum E = 17; enum INF = 10L^^18; int N, K; int[] A, B; long[] C; int[] M; long[] P; int[][] X; int[][] G; int[][] par; int[] dep; long[] dist; void dfs(int u, int p, int d, long c) { par[0][u] = p; dep[u] = d; dist[u] = c; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { dfs(v, u, d + 1, c + C[i]); } } } int lca(int u, int v) { foreach_reverse (e; 0 .. E) { if (dep[u] - (1 << e) >= dep[v]) { u = par[e][u]; } if (dep[v] - (1 << e) >= dep[u]) { v = par[e][v]; } } foreach_reverse (e; 0 .. E) { if (par[e][u] != par[e][v]) { u = par[e][u]; v = par[e][v]; } } if (u != v) { u = par[0][u]; v = par[0][v]; } return u; } void main() { try { for (; ; ) { N = readInt(); K = readInt(); A = new int[N - 1]; B = new int[N - 1]; C = new long[N - 1]; foreach (i; 0 .. N - 1) { A[i] = readInt() - 1; B[i] = readInt() - 1; C[i] = readLong(); } M = new int[K]; P = new long[K]; X = new int[][K]; foreach (k; 0 .. K) { M[k] = readInt(); P[k] = readLong(); X[k] = new int[M[k]]; foreach (m; 0 .. M[k]) { X[k][m] = readInt() - 1; } } G = new int[][N]; foreach (i; 0 .. N - 1) { G[A[i]] ~= i; G[B[i]] ~= i; } par = new int[][](E, N); dep = new int[N]; dist = new long[N]; dfs(0, -1, 0, 0); foreach (e; 0 .. E - 1) { foreach (u; 0 .. N) { par[e + 1][u] = (par[e][u] == -1) ? -1 : par[e][par[e][u]]; } } auto dss = new long[][](K, N); foreach (k; 0 .. K) { alias Entry = Tuple!(long, "c", int, "u"); BinaryHeap!(Array!Entry, "a.c > b.c") que; dss[k][] = INF; foreach (u; X[k]) { dss[k][u] = 0; que.insert(Entry(0, u)); } for (; !que.empty; ) { const c = que.front.c; const u = que.front.u; que.removeFront; if (dss[k][u] == c) { foreach (i; G[u]) { const cc = c + C[i]; const v = A[i] ^ B[i] ^ u; if (chmin(dss[k][v], cc)) { que.insert(Entry(cc, v)); } } } } } auto dd = new long[][](K, K); foreach (k; 0 .. K) { foreach (l; 0 .. K) { dd[k][l] = INF; foreach (u; X[l]) { chmin(dd[k][l], dss[k][u]); } } } foreach (m; 0 .. K) foreach (k; 0 .. K) foreach (l; 0 .. K) { chmin(dd[k][l], dd[k][m] + P[m] + dd[m][l]); } const Q = readInt(); foreach (q; 0 .. Q) { const u = readInt() - 1; const v = readInt() - 1; long ans = INF; // 0 chmin(ans, dist[u] + dist[v] - 2 * dist[lca(u, v)]); // 1 foreach (k; 0 .. K) { chmin(ans, dss[k][u] + P[k] + dss[k][v]); } // 2 foreach (k; 0 .. K) foreach (l; 0 .. K) { chmin(ans, dss[k][u] + P[k] + dd[k][l] + P[l] + dss[l][v]); } writeln(ans); } } } catch (EOFException e) { } }