#include #include #include #include #include #include #include #include using namespace std; using ll = int64_t; template ostream&operator<<(ostream&os,mapconst&m){ os<<"map { "; for(auto [k,v]:m){ os<<"{ "< ostream&operator<<(ostream&os,pairconst&m){ os<<"pair { "<e[maxn]; struct DSU{ ll f[maxn]; DSU(){for(ll i{1};i<=n;i++){f[i]=i;}} ll find(ll const&n){if(f[n]==n)return n; return f[n]=find(f[n]);} bool merge(ll const&a,ll const&b){if(find(a)==find(b))return false;f[find(a)]=find(b);return true;} friend ostream&operator<<(ostream&os,DSU const& d){ os<<"DSU { "; for(ll i{1};i<=n;i++)os<>t; while(t--){ cin>>n>>m; // cout<>u>>v; if(u>v)swap(u,v); // cout<<"uv "< mm; for(ll i{1};i<=n;i++){ mm[dsu.find(i)] = min(mm[dsu.find(i)]==0?inf:mm[dsu.find(i)],i); } // cout<uns&&mm.size()!=1){ cout<<"-1"<<'\n'; continue; } if(mm.size()==1){ cout<<"0\n"; continue; } qs=0; for(auto const[k,v]:mm){ q[++qs]=v; } sort(q+1,q+1+qs); // cout<