static void Main() { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); var I = G; int N = I[0], M = I[1]; var uf = new UnionFindTree(N); for (var i = 0; i < M; i++) { I = G; int a = I[0] - 1, b = I[1] - 1; uf.Unite(a, b); } var Q = int.Parse(ReadLine()); var sb = new StringBuilder(); while (Q-- > 0) { I = G; int x = I[0] - 1, y = I[1] - 1, z = I[2], l = 0, r = M + 1; while (l < r) { var t = (l + r) / 2; int p = uf.Find(t, x), q = uf.Find(t, y), s = p == q?uf.GetSize(t, p) : uf.GetSize(t, p) + uf.GetSize(t, q); if (s >= z) { r = t; } else { l = t + 1; } } sb.AppendLine(l.ToString()); } Write(sb); Out.Flush(); }