#include using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; using i128 = __int128; struct DSU { std::vector f, siz; DSU() {} DSU(int n) { init(n); } void init(int n) { f.resize(n); siz.assign(n, 1); std::iota(f.begin(), f.end(), 0); } int find(int x) { while (x != f[x]) { x = f[x] = f[f[x]]; } return x; } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) { return false; } f[y] = x; siz[x] += siz[y]; return true; } int get_size(int x) { return siz[find(x)]; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int N, M; std::cin >> N >> M; std::vector> adj, Queries; for (int i = 0; i < M; i++) { int u, v, l; std::cin >> u >> v >> l; u--; v--; adj.push_back({v, u, l}); } std::sort(adj.begin(), adj.end(), [](std::array& a, std::array& b){ return a[2] < b[2]; }); int Q; std::cin >> Q; std::vector ans(Q); for (int i = 0; i < Q; i++) { int P, T; std::cin >> P >> T; T--; //! 这里忘记转成0-based了 Queries.push_back({P, T, i}); } std::sort(Queries.begin(), Queries.end()); DSU dsu(N); int j = 0; for (auto [p, t, idx] : Queries) { while (j < M && adj[j][2] <= p) { auto [u, v, l] = adj[j++]; dsu.merge(u, v); } ans[idx] = dsu.get_size(t); } for (int x : ans) { std::cout << x << '\n'; } return 0; }