#include #include #include #include #include #include #include #include using namespace std; #define rep(i,j,k) for(int i=j;i<=k;i++) #define repm(i,j,k) for(int i=j;i>=k;i--) #define mp make_pair typedef long long ll; struct DisjoinSet{ std::vector father,rank; DisjoinSet(int n) : father(n),rank(n) { for(int i=0;i>t; while(t--) { cin>>n; DisjoinSet s=DisjoinSet(n); cin>>m; rep(i,1,m) scanf("%d%d%d",&e[i].s,&e[i].t,&e[i].c),e[i].s--,e[i].t--; sort(e+1,e+1+m,cmp); int cnt=0; int mark=0; repm(i,m,1) { if(s.find(e[i].s)!=s.find(e[i].t)) s.merge(e[i].s,e[i].t),cnt++; if(cnt==n-1) { mark=i; break; } } if(cnt!=n-1) cout<<"-1"<cost) break; if(cnt==n-1) { if(curcost