#include #include #include #include using namespace std; using ll = long long; using tu = tuple; struct UnionFind{ vector par, siz, rank; UnionFind(int n) : par(n, -1), siz(n, 1), rank(n, 1) {} //コンストラクタ //根を求めるやつ int root(int x){ if(par[x]==-1) return x; else return par[x]=root(par[x]); } //xを含むグループとyを含むグループとを併合する bool unite(int x, int y){ x=root(x); y=root(y); if(x==y) return false; if(rank[x]> n >> m; vector edge; for(int i=0; i> u >> v >> w; u--, v--; edge.emplace_back(w, u, v); } sort(begin(edge), end(edge)); int q; cin >> q; vector query; for(int i=0; i> p >> t; t--; query.emplace_back(p, t, i); } sort(begin(query), end(query)); int ei=0; vector ans(q); UnionFind uf(n); for(int i=0; i