#include using namespace std; class UnionFind{ private: vector par,P; vector> S; vector>> Query; public: UnionFind(vector A,vector>> Q){ int N = A.size(); Query = Q; par.resize(N,-1),P.resize(N),S.resize(N); iota(P.begin(),P.end(),0); for(int i=0; i unite(int u, int v){ //u,vを連結する 連結してた->false,した->trueを返す. u = root(u),v = root(v); if(u == v) return {}; if(S.at(u).size() < S.at(v).size()) swap(u,v); //Union by size. par.at(v) = u; for(auto &s : S.at(v)) S.at(u).insert(s); S.at(v).clear(); int pu = P.at(u),pv = P.at(v); if(Query.at(pu).size() < Query.at(pv).size()) swap(pu,pv); P.at(u) = pu; for(auto &q : Query.at(pv)) Query.at(pu).insert(q); Query.at(pv).clear(); vector ret; vector> era; auto itr = Query.at(pu).begin(); while(itr != Query.at(pu).end()){ auto [c,pos] = *itr; itr++; if(c <= S.at(u).size()) ret.push_back(pos),era.push_back({c,pos}); else break; } for(auto e : era) Query.at(pu).erase(e); return ret; } bool issame(int u, int v){ //同じ連結成分ならtrue. if(root(u) == root(v)) return true; else return false; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,M,Q; cin >> N >> M >> Q; vector A(N); for(auto &a : A) cin >> a,a--; vector> edge(M); for(auto &[c,a,b] : edge) cin >> a >> b >> c,a--,b--; sort(edge.begin(),edge.end()); vector answer(Q,-1); vector>> Query(N); for(int i=0; i> s >> c,s--; if(c == 1) answer.at(i) = 0; else Query.at(s).insert({c,i}); } UnionFind Z(A,Query); for(auto [c,a,b] : edge) for(auto pos : Z.unite(a,b)) answer.at(pos) = c; for(auto a : answer) cout << a << "\n"; }