// #define YUKICODER #define CODEFORCES #include #define rep(i, n) for(int i=0;i<(int)(n);i++) #define pb push_back #define pob pop_back #define eb emplace_back #define nall(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define accu accumulate #define bs binary_search #define lb lower_bound #define ub upper_bound #ifdef CODEFORCES #define yes cout<<"YES\n" #define no cout<<"NO\n" #define yesno(a) cout<<(a?"YES\n":"NO\n") #define yesnoout(a, b) cout<<(a?"YES\n":"NO")<<(a?b:"")<<"\n" #else #define yes cout<<"Yes\n" #define no cout<<"No\n" #define yesno(a) cout<<(a?"Yes\n":"No\n") #define yesnoout(a, b) cout<<(a?"Yes\n":"No")<<(a?b:"")<<"\n" #endif using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair; using pll = pair; template using pq = priority_queue; template using pqg = priority_queue, greater>; template using vec = vector; template using vv = vector>; template using vvv = vector>; const ll MOD = 998244353ll; // const ll MOD = 1000000007ll; // template struct dsu{ vector par, sz; dsu(int n){ par.resize(n); sz.assign(n, 1); iota(par.begin(), par.end(), 0); } int root(int x){ if (par[x] == x) return x; return par[x] = root(par[x]); } bool merge(int x, int y){ x = root(x), y = root(y); if (x == y) return false; if (sz[x] < sz[y]) swap(x, y); par[y] = x, sz[x] += sz[y]; return true; } bool same(int x, int y){ return root(x) == root(y); } int size(int x){ return sz[root(x)]; } }; void solve(); signed main(){ ios::sync_with_stdio(false); cin.tie(nullptr); unsigned T = 1; // cin >> T; cout << fixed << setprecision(20); while (T--) solve(); return 0; } void solve(){ int N, M, Q; cin >> N >> M; vec> T; rep(i, M){ int u, v, l; cin >> u >> v >> l; u--, v--; T.emplace_back(1, l, u, v); } cin >> Q; rep(i, Q){ int p, t; cin >> p >> t; t--; T.emplace_back(2, p, t, i); } sort(nall(T), [](auto x, auto y){ if (get<1>(x) == get<1>(y)) return get<0>(x) < get<0>(y); return get<1>(x) < get<1>(y); }); dsu uf(N); vec ans(Q); for (auto [t, p, u, v] : T){ if (t == 1) uf.merge(u, v); else ans[v] = uf.size(u); } for (int x : ans) cout << x << endl; }