namespace AtCoder; #nullable enable using System.Numerics; class UnionFind { (int parent, int edgeCount)[] Verticals { get; set; } bool EnabledUndo { get; init; } Stack<(int v, (int parent, int edges))> Histories { get; init; } public UnionFind(int verticals, bool enableUndo = false) { Verticals = new (int, int)[verticals]; for (int i = 0; i < verticals; i++) Verticals[i] = (-1, 0); Histories = new(); EnabledUndo = enableUndo; } public int Union(int x, int y) { (x, y) = (Find(x), Find(y)); var (sx, sy) = (Size(x), Size(y)); if (sx < sy) (x, y, sy) = (y, x, sx); var (nx, ny) = (Verticals[x], Verticals[y]); Histories.Push((x, nx)); Histories.Push((y, ny)); if (x == y) nx.edgeCount += 1; else { ny.parent = x; nx.parent -= sy; nx.edgeCount += ny.edgeCount + 1; } (Verticals[y], Verticals[x]) = (ny, nx); return x; } public int Find(int x) { var pid = Verticals[x].parent; if (pid < 0) return x; var aid = Find(pid); if (!EnabledUndo) Verticals[x].parent = aid; return aid; } public int Size(int x) => -Verticals[Find(x)].parent; public int EdgeCount(int x) => Verticals[Find(x)].edgeCount; public bool Undo() { if (!EnabledUndo || Histories.Count == 0) return false; for (var i = 0; i < 2; i++) { var (v, h) = Histories.Pop(); Verticals[v] = h; } return true; } } static class Extensions { public static T[] Repeat(this int time, Func F) => Enumerable.Range(0, time).Select(_ => F()).ToArray(); } class AtCoder { object? Solve() { var n = Int(); var m = Int(); var k = 100000; var edges = (k + 1).Repeat(() => new List<(int, int)>()); for (var i = 0; i < m; i++) { var u = Int(); var v = Int(); var w = Int(); edges[w].Add((u, v)); } var uf = new UnionFind(n); var connected = 0; var kns = new int[k + 1]; for (var i = 0; i <= k; i++) { foreach (var (u, v) in edges[i]) { if (uf.Find(u) != uf.Find(v)) { connected++; uf.Union(u, v); } } kns[i] = n - connected; } var t = Int(); var ans = new int[t]; for (var i = 0; i < t; i++) ans[i] = kns[Int()]; Out(ans); return null; } public static void Main() => new AtCoder().Run(); public void Run() { var res = Solve(); if (res != null) { if (res is bool yes) res = yes ? "Yes" : "No"; sw.WriteLine(res); } sw.Flush(); } string[] input = Array.Empty(); int iter = 0; readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false }; string String() { while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Trim().Split(' '), 0); return input[iter++]; } T Input() where T : IParsable => T.Parse(String(), null); int Int() => Input(); void Out(object? x, string? separator = null) { separator ??= Environment.NewLine; if (x is System.Collections.IEnumerable obj and not string) { var firstLine = true; foreach (var item in obj) { if (!firstLine) sw.Write(separator); firstLine = false; sw.Write(item); } } else sw.Write(x); sw.WriteLine(); } }