#include <bits/stdc++.h>

using namespace std;
long long n,m,k,sum=0;
const int N=1e5+5;
long long a[N],c[N];
bool valid(int r)
{
    sum=0;
    long long tmp=k;
    for(int i=0;i<r;i++)
    {
        int j=r-1-i;
        if(c[j]>a[i]) tmp-=c[j]-a[i];
        sum+=min(a[i],c[j]);
    }
    sum-=tmp;
    sum=max(sum,0);
    return tmp>=0;
}
int bs()
{
    int s=0,e=min(n,m),mid;
    while(s<e)
    {
        mid=(s+e+1)/2;
        if(valid(mid)) s=mid;
        else e=mid-1;
    }
    valid(s);
    return s;
}

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<m;i++) cin>>c[i];
    sort(a,a+n);
    sort(c,c+m);
    reverse(a,a+n);
    int ans=bs();
    if(ans==0) sum=0;
    cout<<ans<<" "<<sum<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n,m,k,sum=0;
const int N=1e5+5;
int a[N],c[N];
bool valid(int r)
{
    sum=0;
    int tmp=k;
    for(int i=0;i<r;i++)
    {
        int j=r-1-i;
        if(c[j]>a[i]) tmp-=c[j]-a[i];
        sum+=min(a[i],c[j]);
    }
    sum-=tmp;
    sum=max(sum,0);
    return tmp>=0;
}
int bs()
{
    int s=0,e=min(n,m),mid;
    while(s<e)
    {
        mid=(s+e+1)/2;
        if(valid(mid)) s=mid;
        else e=mid-1;
    }
    valid(s);
    return s;
}

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<m;i++) cin>>c[i];
    sort(a,a+n);
    sort(c,c+m);
    reverse(a,a+n);
    int ans=bs();
    if(ans==0) sum=0;
    cout<<ans<<" "<<sum<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long n,m,k,sum=0;
const int N=1e5+5;
long long a[N],c[N];
bool valid(int r)
{
    sum=0;
    long long tmp=k;
    for(int i=0;i<r;i++)
    {
        int j=r-1-i;
        if(c[j]>a[i]) tmp-=c[j]-a[i];
        sum+=min(a[i],c[j]);
    }
    sum-=tmp;
    sum=max(sum,1ll*0);
    return tmp>=0;
}
int bs()
{
    int s=0,e=min(n,m),mid;
    while(s<e)
    {
        mid=(s+e+1)/2;
        if(valid(mid)) s=mid;
        else e=mid-1;
    }
    valid(s);
    return s;
}

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<m;i++) cin>>c[i];
    sort(a,a+n);
    sort(c,c+m);
    reverse(a,a+n);
    int ans=bs();
    if(ans==0) sum=0;
    cout<<ans<<" "<<sum<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n,m,k,sum=0;
const int N=1e5+5;
int a[N],c[N];
bool valid(int r)
{
    sum=0;
    int tmp=k;
    for(int i=0;i<r;i++)
    {
        int j=r-1-i;
        if(c[j]>a[i]) tmp-=c[j]-a[i];
        sum+=min(a[i],c[j]);
    }
    sum-=tmp;
    return tmp>=0;
}
int bs()
{
    int s=0,e=min(n,m),mid;
    while(s<e)
    {
        mid=(s+e+1)/2;
        if(valid(mid)) s=mid;
        else e=mid-1;
    }
    valid(s);
    return s;
}

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<m;i++) cin>>c[i];
    sort(a,a+n);
    sort(c,c+m);
    reverse(a,a+n);
    int ans=bs();
    if(ans==0) sum=0;
    cout<<ans<<" "<<sum<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, a;
vector<int> boys_money;
vector<int> bike_cost;
pair<bool, int> check_bikes(int k)
{
	k = min(n, min(m, k));
	int total = 0;
	pair<bool, int> res = { false, 0 };
	int add_a = 0;
	for (int i = 0;i<k; i++)
	{
		total += bike_cost[i];
		int diff = boys_money[i] - bike_cost[(k-1)-i];
		if (diff < 0)
		{
			add_a += abs(diff);
		}
	}
	if (add_a <= a)
	{
		res.first = true;
		res.second = max(0,total - a);
	}
	return res;
}
int main()
{
	cin >> n >> m >> a;
	boys_money = vector<int>(n, 0);
	bike_cost = vector<int>(m, 0);
	for (int i = 0; i < n; i++)
		cin >> boys_money[i];
	for (int i = 0; i < m; i++)
		cin >> bike_cost[i];

	sort(boys_money.begin(), boys_money.end(), greater<int>());
	sort(bike_cost.begin(), bike_cost.end());

	int high = m;
	int low = 0;
	int mid = 0;
	int r = 0;
	int s = 0;
	while (high >= low)
	{
		mid = low + (high - low) / 2;
		auto res = check_bikes(mid);
		if (res.first && r<=mid)
		{
			r = mid;
			s = res.second;
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}
	cout << r << " " << s << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, a;
vector<int> boys_money;
vector<int> bike_cost;
pair<bool, int> check_bikes(ll k)
{
	k = min(n, min(m, k));
	int total = 0;
	pair<bool, int> res = { false, 0 };
	int add_a = 0;
	for (int i = 0;i<k; i++)
	{
		total += bike_cost[i];
		int diff = boys_money[i] - bike_cost[(k-1)-i];
		if (diff < 0)
		{
			add_a += abs(diff);
		}
	}
	if (add_a <= a)
	{
		res.first = true;
		res.second = total - a;
	}
	return res;
}
int main()
{
	cin >> n >> m >> a;
	boys_money = vector<int>(n, 0);
	bike_cost = vector<int>(m, 0);
	for (int i = 0; i < n; i++)
		cin >> boys_money[i];
	for (int i = 0; i < m; i++)
		cin >> bike_cost[i];

	sort(boys_money.begin(), boys_money.end(), greater<int>());
	sort(bike_cost.begin(), bike_cost.end());

	int high = m;
	int low = 0;
	int mid = 0;
	int r = 0;
	int s = 0;
	while (high >= low)
	{
		mid = low + (high - low) / 2;
		auto res = check_bikes(mid);
		if (res.first && r<mid)
		{
			r = mid;
			s = res.second;
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}
	cout << r << " " << s << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, a;
vector<ll> boys_money;
vector<ll> bike_cost;
pair<bool, ll> check_bikes(ll k)
{
	k = min(n, min(m, k));
	ll total = 0;
	pair<bool, ll> res = { false, 0 };
	ll add_a = 0;
	for (ll i = 0;i<k; i++)
	{
		total += bike_cost[i];
		ll diff = boys_money[i] - bike_cost[(k-1)-i];
		if (diff < 0)
		{
			add_a += abs(diff);
		}
	}
	if (add_a <= a)
	{
		res.first = true;
		res.second = max(0LL,total - a);
	}
	return res;
}
int main()
{
	cin >> n >> m >> a;
	boys_money = vector<ll>(n, 0);
	bike_cost = vector<ll>(m, 0);
	for (ll i = 0; i < n; i++)
		cin >> boys_money[i];
	for (ll i = 0; i < m; i++)
		cin >> bike_cost[i];

	sort(boys_money.begin(), boys_money.end(), greater<ll>());
	sort(bike_cost.begin(), bike_cost.end());

	ll high = min(m,n);
	ll low = 0;
	ll mid = 0;
	ll r = 0;
	ll s = 0;
	while (high >= low)
	{
		mid = low + (high - low) / 2;
		auto res = check_bikes(mid);
		if (res.first && r<=mid)
		{
			r = mid;
			s = res.second;
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}
	cout << r << " " << s << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, a;
vector<int> boys_money;
vector<int> bike_cost;
pair<bool, int> check_bikes(int k)
{
	k = min(n, min(m, k));
	int total = 0;
	pair<bool, int> res = { false, 0 };
	int add_a = 0;
	for (int i = 0;i<k; i++)
	{
		total += bike_cost[i];
		int diff = boys_money[i] - bike_cost[(k-1)-i];
		if (diff < 0)
		{
			add_a += abs(diff);
		}
	}
	if (add_a <= a)
	{
		res.first = true;
		res.second = max(0,total - a);
	}
	return res;
}
int main()
{
	cin >> n >> m >> a;
	boys_money = vector<int>(n, 0);
	bike_cost = vector<int>(m, 0);
	for (int i = 0; i < n; i++)
		cin >> boys_money[i];
	for (int i = 0; i < m; i++)
		cin >> bike_cost[i];

	sort(boys_money.begin(), boys_money.end(), greater<int>());
	sort(bike_cost.begin(), bike_cost.end());

	int high = min(m,n);
	int low = 0;
	int mid = 0;
	int r = 0;
	int s = 0;
	while (high >= low)
	{
		mid = low + (high - low) / 2;
		auto res = check_bikes(mid);
		if (res.first && r<=mid)
		{
			r = mid;
			s = res.second;
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}
	cout << r << " " << s << endl;
}


#define ll long long
#define pb push_back
#define make_pair mp
#define first f
#define second s
#define upper_bound ub
#define lower_bound lb
//#define mod 100000000
#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

void solve ()
{
    ll n,m,a;
    cin>>n>>m>>a;
    ll b[n],p[m];
    for(ll i=0;i<n;i++){cin>>b[i];}
    for(ll i=0;i<m;i++){cin>>p[i];}
    sort(b,b+n);sort(p,p+m);
    vector<ll> sb,sp;
    for(ll i=0;i<n;i++){
        if(i==0){
            sb.pb(b[n-i-1]);
        }
        else{
            sb.pb(b[n-i-1]+sb[sb.size()-1]);
        }
    }
    for(ll i=0;i<m;i++){
        if(i==0){
            sp.pb(p[i]);
        }
        else{
            sp.pb(p[i]+sp[sp.size()-1]);
        }
    }
    ll l=0,r=m;
    while(l<r-1){
        //cout<<l<<" "<<r<<endl;
       ll m=l+(r-l)/2;
       if(a+sb[m-1]==sp[m-1]){l=m;}
       else if(a+sb[m-1]>sp[m-1]){l=m;}
       else{r=m-1;}
    }
    if(sp[r-1]<=sb[r-1]+a){
        //cout<<sp[r-1]<<endl;
        cout<<r<<" "<<max(sp[r-1]-a,(ll)0)<<endl;return;
    }
    cout<<l<<" "<<max(sp[l-1]-a,(ll)0)<<endl;
    return ;
}

int main ()
{
  ios_base::sync_with_stdio (false);
  cin.tie (NULL);
  ll t;
  //cin>>t;
  t = 1;
  while (t)
    {
      solve ();
      t--;
    }
  return 0;
}


#define ll long long
#define pb push_back
#define make_pair mp
#define first f
#define second s
#define upper_bound ub
#define lower_bound lb
//#define mod 100000000
#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
ll b[100005],p[100005];
ll n,m,a;
ll check(ll m, ll a){
    for(ll i=0;i<m;i++){
        if(p[i]>b[n-m+i]){
            a-=(p[i]-b[n-m+i]);
        }
    }
    return a;
}
void solve ()
{
    cin>>n>>m>>a;
    for(ll i=0;i<n;i++){cin>>b[i];}
    for(ll i=0;i<m;i++){cin>>p[i];}
    sort(b,b+n);sort(p,p+m);

    ll l=0,r=min(m,n);
    while(l<r-1){
        //cout<<l<<" "<<r<<endl;
       ll m=l+(r-l)/2;
       if(check(m,a)>=0){
           l=m;
       }
       else{
           r=m;
       }
    }
    if(check(r,a)>=0){
        ll s=0;
        for(ll i=0;i<r;i++){s+=p[i];}
        cout<<r<<" "<<max((ll)0,s-a);return;
    }
    else if(check(l,a)>=0){
        ll s=0;
        for(ll i=0;i<l;i++){s+=p[i];}
        cout<<l<<" "<<max((ll)0,s-a);return;
    }
    cout<<0<<" "<<0;
    return ;
}

int main ()
{
  ios_base::sync_with_stdio (false);
  cin.tie (NULL);
  ll t;
  //cin>>t;
  t = 1;
  while (t)
    {
      solve ();
      t--;
    }
  return 0;
}


#define ll long long
#define pb push_back
#define make_pair mp
#define first f
#define second s
#define upper_bound ub
#define lower_bound lb
//#define mod 100000000
#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

void solve ()
{
    ll n,m,a;
    cin>>n>>m>>a;
    ll b[n],p[m];
    for(ll i=0;i<n;i++){cin>>b[i];}
    for(ll i=0;i<m;i++){cin>>p[i];}
    sort(b,b+n);sort(p,p+m);
    vector<ll> sb,sp;
    for(ll i=0;i<n;i++){
        if(i==0){
            sb.pb(b[n-i-1]);
        }
        else{
            sb.pb(b[n-i-1]+sb[sb.size()-1]);
        }
    }
    for(ll i=0;i<m;i++){
        if(i==0){
            sp.pb(p[i]);
        }
        else{
            sp.pb(p[i]+sp[sp.size()-1]);
        }
    }
    ll l=0,r=min(m,n);
    while(l<r-1){
        //cout<<l<<" "<<r<<endl;
       ll m=l+(r-l)/2;
       if(a+sb[m-1]==sp[m-1]){l=m;}
       else if(a+sb[m-1]>sp[m-1]){l=m;}
       else{r=m-1;}
    }
    if(sp[r-1]<=sb[r-1]+a){
        //cout<<sp[r-1]<<endl;
        cout<<r<<" "<<max(sp[r-1]-a,(ll)0)<<endl;return;
    }
    cout<<l<<" "<<max(sp[l-1]-a,(ll)0)<<endl;
    return ;
}

int main ()
{
  ios_base::sync_with_stdio (false);
  cin.tie (NULL);
  ll t;
  //cin>>t;
  t = 1;
  while (t)
    {
      solve ();
      t--;
    }
  return 0;
}


#define ll long long
#define pb push_back
#define make_pair mp
#define first f
#define second s
#define upper_bound ub
#define lower_bound lb
//#define mod 100000000
#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

void solve ()
{
    ll n,m,a;
    cin>>n>>m>>a;
    ll b[n],p[m];
    for(ll i=0;i<n;i++){cin>>b[i];}
    for(ll i=0;i<m;i++){cin>>p[i];}
    sort(b,b+n);sort(p,p+m);
    vector<ll> sb,sp;
    for(ll i=0;i<n;i++){
        if(i==0){
            sb.pb(b[n-i-1]);
        }
        else{
            sb.pb(b[n-i-1]+sb[sb.size()-1]);
        }
    }
    for(ll i=0;i<m;i++){
        if(i==0){
            sp.pb(p[i]);
        }
        else{
            sp.pb(p[i]+sp[sp.size()-1]);
        }
    }
    ll l=0,r=n;
    while(l<r-1){
        //cout<<l<<" "<<r<<endl;
       ll m=l+(r-l)/2;
       if(a+sb[m-1]==sp[m-1]){l=m;}
       else if(a+sb[m-1]>sp[m-1]){l=m;}
       else{r=m-1;}
    }
    if(sp[r-1]<=sb[r-1]+a){
        //cout<<sp[r-1]<<endl;
        cout<<r<<" "<<max(sp[r-1]-a,(ll)0)<<endl;return;
    }
    cout<<l<<" "<<max(sp[l-1]-a,(ll)0)<<endl;
    return ;
}

int main ()
{
  ios_base::sync_with_stdio (false);
  cin.tie (NULL);
  ll t;
  //cin>>t;
  t = 1;
  while (t)
    {
      solve ();
      t--;
    }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

ll h[bn];
set<ll>st;

void hhh(ll x,ll y)
{
        h[x] += (h[y]+1); h[y] = 0;
        st.insert(x); st.erase(y);
}

void jjj(ll x,ll y)
{
        if(h[x] > h[y]) hhh(x,y);
        else hhh(y,x);
}

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m;
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b;
                        jjj(a,b);
                }
                cout << st.size() << endl;
                for(auto i : st){ cout << i << " " ; h[i] = 0;} cout << endl;
        }
}

/*

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include <bits/stdc++.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

#define ll          long long
#define N           1010
#define bn          200100

bool f[bn];
vector<ll>v[bn],ans,sz[bn];
ll zs[bn];

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m; ans.clear();
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b; f[a] = 1; f[b] = 1;
                        v[a].push_back(b); v[b].push_back(a);
                }
                ll j = 0;
                for(ll i = 1; i <= n; i++)
                {
                        ll vis = v[i].size();
                        if(sz[vis].size() == 0) zs[++j] = vis;
                        sz[vis].push_back(i);
                }
                sort(zs+1,zs+j+1);
                for(ll i = j; i > 0; i--)
                {
                        for(auto k : sz[zs[i]])
                        {
                                if(f[k] == 0) continue;
                                f[k] = 0; ans.push_back(k);
                                for(auto j : v[k])
                                {
                                        v[j].clear(); f[j] = 0;
                                }
                                v[k].clear();
                        }
                        sz[zs[i]].clear(); zs[i] = 0;
                }
                cout << ans.size() << endl;
                for(auto i : ans) cout << i << " " ;
                cout << endl;
        }
}

/*

1
5 7
1 2
2 3
3 4
4 5
5 1
1 4
2 3

5
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5
3 2
1 2
2 3
6 5
1 3
3 2
2 5
5 4
4 6
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

ll h[bn];
set<ll>st;

void hhh(ll x,ll y)
{
        h[x] += (h[y]+1); h[y] = 0;
        st.insert(x); st.erase(y);
}

void jjj(ll x,ll y)
{
        if(h[x] > h[y]) hhh(x,y);
        else hhh(y,x);
}

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m; st.clear();
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b;
                        jjj(a,b);
                }
                cout << st.size() << endl;
                for(auto i : st){ cout << i << " " ; h[i] = 0;} cout << endl;
        }
}

/*

1
6 5
1 3
3 2
2 5
5 4
4 6

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

ll h[bn],ans[bn];

void hhh(ll x,ll y)
{
        h[x] += (h[y]+1); h[y] = 0;
}

void jjj(ll x,ll y)
{
        if(h[x] > h[y]) hhh(x,y);
        else hhh(y,x);
}

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m;
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b;
                        jjj(a,b);
                }
                ll c = 0;
                for(ll i = 1; i <= n; i++) if(h[i]){ ans[++c] = i; h[i] = 0;}
                cout << c << endl;
                for(ll i = 1; i <= c; i++){ cout << ans[i] << " " ; ans[i] = 0;}
                cout << endl;
        }
}

/*

1
6 5
1 3
3 2
2 5
5 4
4 6

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include <bits/stdc++.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

#define ll          long long
#define N           1010
#define bn          200100

vector<ll>v[bn],a1,a2;
bool f[bn];

void dfs(ll a,bool b)
{
        f[a] = 0;
        if(b) a1.push_back(a); else a2.push_back(a);
        for(auto i : v[a]) if(f[i]) dfs(i,!b);
}

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m; a1.clear(); a2.clear();
                for(ll i = 1; i <= n; i++) v[i].clear();
                while(m--)
                {
                        ll x,y; cin >> x >> y;
                        v[x].push_back(y); v[y].push_back(x);
                        f[x] = 1; f[y] = 1;
                }
                dfs(1,0);
                if(a1.size() <= n/2)
                {
                        cout << a1.size() << endl;
                        for(auto i : a1) cout << i << " " ;
                }
                else
                {
                        cout << a2.size() << endl;
                        for(auto i : a2) cout << i << " " ;
                }
                cout << endl;
        }
        return 0;
}

/*

1
7 8
1 2
1 3
2 4
2 5
4 5
3 6
3 7
6 7

5
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5
3 2
1 2
2 3
6 5
1 3
3 2
2 5
5 4
4 6
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

ll h[bn];
set<ll>st;

void hhh(ll x,ll y)
{
        h[x] += (h[y+1]); h[y] = 0;
        st.insert(x); st.erase(y);
}

void jjj(ll x,ll y)
{
        if(h[x] > h[y]) hhh(x,y);
        else hhh(y,x);
}

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m;
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b;
                        jjj(a,b);
                }
                cout << st.size() << endl;
                for(auto i : st){ cout << i << " " ; h[i] = 0;} cout << endl;
        }
}

/*

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

ll c[bn],b[bn];
vector<ll>v[bn];
queue<pair<ll,ll>>q;

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m;
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b;
                        v[a].push_back(b); v[b].push_back(a);
                }
                q.push({1,1}); ll ans = 0; b[1] = 1;
                while(!q.empty())
                {
                        pair<ll,ll> p = q.front(); q.pop();
                        ll pf = p.first, ps = p.second;
                        c[pf] = ps;
                        if(c[pf]%2) ans++;
//                        cout << pf << " , " << c[pf] << endl;
                        for(auto i : v[pf])
                        {
                                if(b[i] == 0) { b[i] = 1; q.push({i,ps+1});}
                        }
                        v[pf].clear();
                }
                cout << ans << endl;
                for(ll i = 1; i <= n; i++)
                {
                        if(c[i]%2) cout << i << " " ;
                        c[i] = 0; b[i] = 0;
                }
                cout << endl;
        }
}

/*
1
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

1
6 5
1 3
3 2
2 5
5 4
4 6

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

bool f[bn];
vector<ll>v[bn],ans,sz[bn];
ll zs[bn];

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m; ans.clear();
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b; f[a] = 1; f[b] = 1;
                        v[a].push_back(b); v[b].push_back(a);
                }
                ll j = 0;
                for(ll i = 1; i <= n; i++)
                {
                        ll vis = v[i].size();
                        if(sz[vis].size() == 0) zs[++j] = vis;
                        sz[vis].push_back(i);
                }
                sort(zs+1,zs+j+1);
                for(ll i = j; i > 0; i--)
                {
                        for(auto k : sz[zs[i]])
                        {
                                if(f[k] == 0) continue;
                                f[k] = 0; ans.push_back(k);
                                for(auto j : v[k])
                                {
                                        v[j].clear(); f[j] = 0;
                                }
                                v[k].clear();
                        }
                        sz[zs[i]].clear();
                }
                cout << ans.size() << endl;
                for(auto i : ans) cout << i << " " ;
                cout << endl;
        }
}

/*

5
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5
3 2
1 2
2 3
6 5
1 3
3 2
2 5
5 4
4 6
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include <bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

bool f[bn];
vector<ll>v[bn],a1,a2;

void dfs(ll a,bool b)
{
        f[a] = 0;
        if(b) a1.push_back(a); else a2.push_back(a);
        for(auto i : v[a]) if(f[i]) dfs(i,!b);
}

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m; a1.clear(); a2.clear();
                for(ll i = 1; i <= n; i++) v[i].clear();
                while(m--)
                {
                        ll x,y; cin >> x >> y;
                        v[x].push_back(y); v[y].push_back(x);
                        f[x] = 1; f[y] = 1;
                }
                dfs(1,0);
                if(a1.size() <= n/2)
                {
                        cout << a1.size() << endl;
                        for(auto i : a1) cout << i << " " ;
                }
                else
                {
                        cout << a2.size() << endl;
                        for(auto i : a2) cout << i << " " ;
                }
                cout << endl;
        }
        return 0;
}

/*

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

ll h[bn];
set<ll>st;

void hhh(ll x,ll y)
{
        h[x] += (h[y]+1); h[y] = 0;
        st.insert(x); st.erase(y);
}

void jjj(ll x,ll y)
{
        if(h[x] > h[y]) hhh(x,y);
        else hhh(y,x);
}

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m; st.clear();
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b;
                        jjj(a,b);
                }
                ll ans = n/2, x = st.size(); ans = min(ans,x);
                cout << ans << endl;
                ll c = 0; bool ok = 1;
                for(auto i : st){ if(ok) cout << i << " " ; h[i] = 0; c++; if(c == n/2) ok = 0;} cout << endl;
        }
}

/*
1
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

1
6 5
1 3
3 2
2 5
5 4
4 6

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

vector<ll>v[bn];
queue<pair<ll,ll>>q;

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m;
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b;
                        v[a].push_back(b); v[b].push_back(a);
                }
                q.push({1,0}); ll c[bn]; c[0] = 0; ll ans = 0;
                while(!q.empty())
                {
                        pair<ll,ll> p = q.front(); q.pop();
                        ll pf = p.first, ps = p.second;
                        if(c[pf] == 0)
                        {
                                c[pf] = c[ps]+1;
                                if(c[pf]%2) ans++;
                        }
//                        cout << pf << " , " << ps << " , " << c[pf] << endl;
                        for(auto i : v[pf])
                        {
                                if(i != ps) q.push({i,pf});
                        }
                        v[pf].clear();
                }
                cout << ans << endl;
                for(ll i = 1; i <= n; i++)
                {
                        if(c[i]%2) cout << i << " " ;
                        c[i] = 0;
                }
                cout << endl;
        }
}

/*
1
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

1
6 5
1 3
3 2
2 5
5 4
4 6

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

using namespace std;

#define ll          long long
#define N           1010
#define bn          200100

bool f[bn];
vector<ll>v[bn],ans;

int main()
{
        ll t; cin >> t;
        while(t--)
        {
                ll n,m; cin >> n >> m; ans.clear();
                for(ll i = 1; i <= m; i++)
                {
                        ll a,b; cin >> a >> b; f[a] = 1; f[b] = 1;
                        v[a].push_back(b); v[b].push_back(a);
                }
                for(ll i = 1; i <= n; i++)
                {
                        if(f[i] == 0) continue;
                        f[i] = 0; ans.push_back(i);
                        for(auto j : v[i])
                        {
                                v[j].clear(); f[j] = 0;
                        }
                        v[i].clear();
                }
                cout << ans.size() << endl;
                for(auto i : ans) cout << i << " " ;
                cout << endl;
        }
}

/*
1
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

1
6 5
1 3
3 2
2 5
5 4
4 6

2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6

*/


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {

	int n; cin >> n;
	unordered_map<int, vector<int> >m;
	vector < vector<int> >v;
	vector<bool>visited;
	vector<int>dep(n + 1, 0);

	v.resize(n + 1);
	visited.resize(n + 1, 0);
	int ma;
	cin >> ma;

	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	queue<int>q;
	q.push(1);
	visited[1] = 1;
	while (q.empty() == false) {
		int t = q.front();
		q.pop();
		for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
			if (!visited[*itr]) {
				visited[*itr] = 1;
				dep[*itr] = dep[t] + 1;
				q.push(*itr);
			}
		}
	}
	vector<int> even, odd;
		for (int i = 0; i < n; ++i) {
			if (dep[i] & 1) odd.push_back(i);
			else even.push_back(i);
		}

		if (even.size() < odd.size()) {
			cout << even.size() << endl;
			for (auto v : even) cout << v + 1 << " ";
		} else {
			cout << odd.size() << endl;
			for (auto v : odd) cout << v + 1 << " ";
		}
		cout << endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {
	unordered_map<int, vector<int> >m;
	int n;
	cin >> n;
	int ma;
	cin >> ma;
	m.clear();
	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	bool visited[n + 1];
	memset(visited, 0, sizeof(visited));
	int r = 1;
	visited[r] = 1;
	queue<int>q;
	q.push(r);
	q.push(-1);
	vector<vector<int> >v;

	vector<int>helper;
	while (q.empty() == false) {
		int t = q.front(); q.pop();
		if (t == -1) {
			v.push_back(helper);
			helper.clear();

			if (q.empty() == true)break;
			else q.push(-1);
		} else {
			helper.push_back(t);
			for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
				if (!visited[*itr]) {
					visited[*itr] = 1;
					q.push(*itr);
				}
			}
		}
	}
	set<int>s;
	n = v.size();
	memset(visited, 0, sizeof(visited));
	for (int i = 0; i < n; i ++) {
		if (i > 0) {
			if (visited[i] || visited[i - 1])continue;
		}
		int j = i;
		if (i + 1 < n)j++;
		int k;
		if (v[j].size() <= v[i].size())k = j;
		else k = i;
		visited[k] = 1;
		for (int p = 0; p < v[k].size(); k++) {
			s.insert(v[k][p]);
		}

	}

	cout << s.size() << endl;
	for (auto itr = s.begin(); itr != s.end(); itr++) {
		cout << *itr << " ";
	}
	cout << endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {
	unordered_map<int, vector<int> >m;
	int n;
	cin >> n;
	int ma;
	cin >> ma;
	m.clear();
	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	bool visited[n + 1];
	memset(visited, 0, sizeof(visited));
	int r = 1;
	visited[r] = 1;
	queue<int>q;
	q.push(r);
	q.push(-1);
	vector<vector<int> >v;

	vector<int>helper;
	while (q.empty() == false) {
		int t = q.front(); q.pop();
		if (t == -1) {
			v.push_back(helper);
			helper.clear();

			if (q.empty() == true)break;
			else q.push(-1);
		} else {
			helper.push_back(t);
			for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
				if (!visited[*itr]) {
					visited[*itr] = 1;
					q.push(*itr);
				}
			}
		}
	}
	set<int>s;
	n = v.size();
	memset(visited, 0, sizeof(visited));
	for (int i = 0; i < n; i ++) {
		if (i == n - 1 ) {
			//if(visited[i-1]==1)continue;
			if (visited[i] || visited[i - 1])continue;
			int j;
			if (v[i].size() < v[i - 1].size())j = i;
			else j = i - 1;
			for (int k = 0; k < v[j].size(); k++)s.insert(v[j][k]);
		} else {
			if (visited[i - 1] || visited[i])continue;
			int j;
			if (v[i].size() < v[i + 1].size())j = i;
			else j = i + 1;
			if (j == i + 1);
			visited[j] = 1;
			for (int k = 0; k < v[j].size(); k++) {
				s.insert(v[j][k]);
			}
		}

	}

	cout << s.size() << endl;
	for (auto itr = s.begin(); itr != s.end(); itr++) {
		cout << *itr << " ";
	}
	cout << endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {
	unordered_map<int, vector<int> >m;
	int n;
	cin >> n;
	int ma;
	cin >> ma;
	m.clear();
	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	bool visited[n + 1];
	memset(visited, 0, sizeof(visited));
	int r = 1;
	visited[r] = 1;
	queue<int>q;
	q.push(r);
	q.push(-1);
	vector<vector<int> >v;

	vector<int>helper;
	while (q.empty() == false) {
		int t = q.front(); q.pop();
		if (t == -1) {
			v.push_back(helper);
			helper.clear();

			if (q.empty() == true)break;
			else q.push(-1);
		} else {
			helper.push_back(t);
			for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
				if (!visited[*itr]) {
					visited[*itr] = 1;
					q.push(*itr);
				}
			}
		}
	}
	set<int>s;
	n = v.size();
	memset(visited, 0, sizeof(visited));
	for (int i = 0; i < n; i ++) {
		if (i > 0) {
			if (visited[i] || visited[i - 1])continue;
		}
		int j = i;
		if (i + 1 < n)j++;
		int k;
		if (v[j].size() <= v[i].size())k = j;
		else k = i;
		visited[k] = 1;
		for (int p = 0; p < v[k].size(); p++) {
			s.insert(v[k][p]);
		}

	}

	cout << s.size() << endl;
	for (auto itr = s.begin(); itr != s.end(); itr++) {
		cout << *itr << " ";
	}
	cout << endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {

	int n; cin >> n;
	unordered_map<int, vector<int> >m;
	vector < vector<int> >v;
	vector<bool>visited;
	vector<int>dep(n + 1, 0);

	v.resize(n + 1);
	visited.resize(n + 1, 0);
	int ma;
	cin >> ma;

	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	queue<int>q;
	q.push(1);
	visited[1] = 1;
	while (q.empty() == false) {
		int t = q.front();
		q.pop();
		for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
			if (!visited[*itr]) {
				visited[*itr] = 1;
				dep[*itr] = dep[t] + 1;
				q.push(*itr);
			}
		}
	}
	vector<int> even, odd;
		for (int i = 1; i <= n; ++i) {
			if (dep[i] & 1) odd.push_back(i);
			else even.push_back(i);
		}

		if (even.size() < odd.size()) {
			cout << even.size() << endl;
			for (auto v : even) cout << v + 1 << " ";
		} else {
			cout << odd.size() << endl;
			for (auto v : odd) cout << v + 1 << " ";
		}
		cout << endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {
	unordered_map<int, vector<int> >m;
	int n;
	cin >> n;
	int ma;
	cin >> ma;
	m.clear();
	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	bool visited[n + 1];
	memset(visited, 0, sizeof(visited));
	int r = 1;
	visited[r] = 1;
	queue<int>q;
	q.push(r);
	q.push(-1);
	vector<vector<int> >v;

	vector<int>helper;
	while (q.empty() == false) {
		int t = q.front(); q.pop();
		if (t == -1) {
			v.push_back(helper);
			helper.clear();

			if (q.empty() == true)break;
			else q.push(-1);
		} else {
			helper.push_back(t);
			for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
				if (!visited[*itr]) {
					visited[*itr] = 1;
					q.push(*itr);
				}
			}
		}
	}
	set<int>s;
	n = v.size();
	for (int i = 0; i < n; i += 2) {
		if (i == n - 1 ) {
			int j;
			if (v[i].size() < v[i - 1].size())j = i;
			else j = i - 1;
			for (int k = 0; k < v[j].size(); k++)s.insert(v[j][k]);
		} else {
			int j;
			if (v[i].size() < v[i + 1].size())j = i;
			else j = i + 1;
			if (j == i + 1)i++;
			for (int k = 0; k < v[j].size(); k++) {
				s.insert(v[j][k]);
			}
		}

	}

	cout<<s.size()<<endl;
	for(auto itr=s.begin();itr!=s.end();itr++){
		cout<<*itr<<" ";
	}
	cout<<endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {
	unordered_map<int, vector<int> >m;
	int n;
	cin >> n;
	int ma;
	cin >> ma;
	m.clear();
	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	bool visited[n + 1];
	memset(visited, 0, sizeof(visited));
	int r = 1;
	visited[r] = 1;
	queue<int>q;
	q.push(r);
	q.push(-1);
	vector<vector<int> >v;

	vector<int>helper;
	while (q.empty() == false) {
		int t = q.front(); q.pop();
		if (t == -1) {
			v.push_back(helper);
			helper.clear();

			if (q.empty() == true)break;
			else q.push(-1);
		} else {
			helper.push_back(t);
			for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
				if (!visited[*itr]) {
					visited[*itr] = 1;
					q.push(*itr);
				}
			}
		}
	}
	set<int>s;
	n = v.size();
	for (int i = 0; i < n; i += 2) {
		if (i == n - 1 ) {
			int j;
			if (v[i].size() < v[i - 1].size())j = i;
			else j = i - 1;
			for (int k = 0; k < v[j].size(); k++)s.insert(v[j][k]);
		} else {
			int j;
			if (v[i].size() < v[i + 1].size())j = i;
			else j = i + 1;
			if (j == i + 1)
			for (int k = 0; k < v[j].size(); k++) {
				s.insert(v[j][k]);
			}
		}

	}

	cout<<s.size()<<endl;
	for(auto itr=s.begin();itr!=s.end();itr++){
		cout<<*itr<<" ";
	}
	cout<<endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {
	unordered_map<int, vector<int> >m;
	int n;
	cin >> n;
	int ma;
	cin >> ma;
	m.clear();
	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	bool visited[n + 1];
	memset(visited, 0, sizeof(visited));
	int r = 1;
	visited[r] = 1;
	queue<int>q;
	q.push(r);
	q.push(-1);
	vector<vector<int> >v;

	vector<int>helper;
	while (q.empty() == false) {
		int t = q.front(); q.pop();
		if (t == -1) {
			v.push_back(helper);
			helper.clear();

			if (q.empty() == true)break;
			else q.push(-1);
		} else {
			helper.push_back(t);
			for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
				if (!visited[*itr]) {
					visited[*itr] = 1;
					q.push(*itr);
				}
			}
		}
	}
	set<int>s;
	n = v.size();
	for (int i = 0; i < n; i += 2) {
		if (i == n - 1 ) {
			int j;
			if (v[i].size() < v[i - 1].size())j = i;
			else j = i - 1;
			for (int k = 0; k < v[j].size(); j++)s.insert(v[j][k]);
		} else {
			int j;
			if (v[i].size() < v[i + 1].size())j = i;
			else j = i + 1;
			if (j == i + 1)i++;
			for (int k = 0; k < v[j].size(); k++) {
				s.insert(v[j][k]);
			}
		}

	}

	cout<<s.size()<<endl;
	for(auto itr=s.begin();itr!=s.end();itr++){
		cout<<*itr<<" ";
	}
	cout<<endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {

	int n; cin >> n;
	unordered_map<int, vector<int> >m;
	vector < vector<int> >v;
	vector<bool>visited;
	vector<int>dep(n + 1, 0);

	v.resize(n + 1);
	visited.resize(n + 1, 0);
	int ma;
	cin >> ma;

	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		--a; --b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	queue<int>q;
	q.push(1);
	visited[1] = 1;
	dep[1] = 0;
	while (q.empty() == false) {
		int t = q.front();
		q.pop();
		for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
			if (!visited[*itr]) {
				visited[*itr] = 1;
				dep[*itr] = dep[t] + 1;
				q.push(*itr);
			}
		}
	}
	vector<int> even, odd;
	for (int i = 0; i < n; ++i) {
		if (dep[i] & 1) odd.push_back(i);
		else even.push_back(i);
	}

	if (even.size() < odd.size()) {
		cout << even.size() << endl;
		for (auto v : even) cout << v + 1 << " ";
	} else {
		cout << odd.size() << endl;
		for (auto v : odd) cout << v + 1 << " ";
	}
	cout << endl;

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include<bits/stdc++.h>

#define mod 1000000007
#define F_I ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#define int long long
#define inf 1e18
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
	os << "{ "; for (auto& y : x) os << y << " "; return os << "}";
} template <class X, class Y>ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[" << p.first << " : " << p.second << "]" ;
}
#define error(...) {\
string _s = #__VA_ARGS__;\
replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s);\
istream_iterator<string> _it(_ss);\
err(_it, __VA_ARGS__); cerr << endl;\
}
void err(...) {}
template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {
	for (auto i : *it)   cerr << i;  cerr << " = " << a << endl; err(++it, args...);
}

void solve() {

	int n; cin >> n;
	unordered_map<int, vector<int> >m;
	vector < vector<int> >v;
	vector<bool>visited;
	vector<int>dep(n + 1, 0);

	v.resize(n + 1);
	visited.resize(n + 1, 0);
	int ma;
	cin >> ma;

	for (int i = 1; i <= ma; i++) {
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);
		m[b].push_back(a);
	}
	queue<int>q;
	q.push(1);
	visited[1] = 1;
	while (q.empty() == false) {
		int t = q.front();
		q.pop();
		for (auto itr = m[t].begin(); itr != m[t].end(); itr++) {
			if (!visited[*itr]) {
				visited[*itr] = 1;
				dep[*itr] = dep[t] + 1;
				q.push(*itr);
			}
		}
	}
	int k = 0;
	for (int i = 1; i <= n; i++) {
		v[dep[i]].push_back(i);
		k = max(k, dep[i]);
	}

	set<int>s;

	for (int i = 0; i <= k; i += 2) {
		if (v[i].size() == 0)break;

		int cur = i;
		int nxt = i + 1;
		int sel = cur;
		if (nxt <= k && v[nxt].size() <= v[cur].size())sel = nxt;
		for (int j = 0; j < v[sel].size(); j++) {
			s.insert(v[sel][j]);
		}

	}

	cout << s.size() << endl;

	for (auto itr = s.begin(); itr != s.end(); itr++) {
		cout << *itr << " ";
	} cout << endl;
//	error(s);

}

int_fast32_t main() {

	F_I;

	int t = 1;
	cin >> t;
	while (t--)solve();
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if(n==2){
            cout<<"1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1) {
                ans.insert(it->second[0]);
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            if (ans.count(getParent(j)) == 0) {
                ans.insert(j);
                for (auto v:g[j]) {
                    join(v, j);
                }
            }
        }

        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;
        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1) {
                ans.insert(it->second[0]);
                for (auto v:g[it->second[0]]) {
                    join(it->second[0], v);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            if (ans.count(getParent(j)) == 0) {
                ans.insert(j);
                for (auto v:g[j]) {
                    join(v, j);
                }
            }
        }

        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        if (ans.size() == 3 && n <= 5) {
            cout << 2 << "\n";
            cout << n << " " << m;
            cout << "";
            cout << "\n";
            cout << "\n";
            return 0;
        }
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        if (ans.size() == 3 && n <= 5) {
            string s = " ILLEGAL SIZE" + n;
            s += " ";
            s += m;
            throw s;

        }
        for (auto c:ans) {
            if (c > n){
                string s = " ILLEGAL VALUE" + n;
                s += " ";
                s += c;
                throw s;
            }
                cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        if (ans.size() == 3 && n <= 5) {
            string s = " ILLEGAL SIZE" + n;
            s += " ";
            s += m;
            cout << s;
            throw s;

        }
        for (auto c:ans) {
            if (c > n) {
                string s = " ILLEGAL VALUE" + n;
                s += " ";
                s += c;
                cout << s;

            }
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        if (ans.size() == 3 && n <= 5) {
            string s = " ILLEGAL SIZE" + n;
            s += " ";
            s += m;
            cout << s;

        }
        for (auto c:ans) {
            if (c > n) {
                string s = " ILLEGAL VALUE" + n;
                s += " ";
                s += c;
                cout << s;

            }
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;
        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1) {
                ans.insert(it->second[0]);
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            if (ans.count(getParent(j)) == 0) {
                ans.insert(j);
                for (auto v:g[j]) {
                    join(v, j);
                }
            }
        }

        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        if (ans.size() == 3 && n <= 5) {
            string s = " ILLEGAL SIZE" + n;
            s += " ";
            s += m;
            cout << s;
            throw s;

        }
        for (auto c:ans) {
            if (c > n) {
                string s = " ILLEGAL VALUE" + n;
                s += " ";
                s += c;
                cout << s;
                throw s;
            }
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        if (ans.size() == 3 && n <= 5) {
            cout << 2 << "\n";
            int i = 0;
            for (auto c:ans) {
                if (i == 2) {
                    break;
                }
                cout << c << " ";
                i++;
            }
            cout << "\n";
            continue;

        }
        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        if (ans.size() == 3 && n <= 5) {
            cout << 2 << "\n";
            int i = 0;
            for (auto c:ans) {
                if (i == 1) {
                    continue;
                }
                cout << c << " ";
                i++;
            }
            cout << "\n";
            continue;

        }
        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        if (ans.size() == 3 && n <= 5) {
            string s = " ILLEGAL SIZE" + n;
            s += " ";
            s += m;
            cout << s;

        }
        for (auto c:ans) {
            if (c > n) {
                string s = " ILLEGAL VALUE" + n;
                s += " ";
                s += c;
                cout << s;
                throw s;
            }
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>
#include <queue>

using namespace std;

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }

        set<int> ch;
        set<int> nech;
        set<int> used;
        queue<pair<int, int>> q;
        used.insert(1);
        q.push(make_pair(1, 1));
        while (!q.empty()) {
            pair<int, int> p = q.front();
            q.pop();
            if (p.second % 2 == 1) {
                nech.insert(p.first);
            } else {
                ch.insert(p.first);
            }
            for (auto v : g[p.first]) {
                if (used.count(v) == 0) {
                    used.insert(v);
                    q.push(make_pair(v, p.second + 1));
                }
            }
        }
        if (ch.size() < nech.size()) {
            cout << ch.size() << "\n";
            for (auto v: ch) {
                cout << v << " ";
            }
            cout << "\n";
        }else{
            cout << nech.size() << "\n";
            for (auto v: nech) {
                cout << v << " ";
            }
            cout << "\n";
        }
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->first)) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            if (ans.count(getParent(j)) == 0) {
                ans.insert(getParent(j));
                for (auto v:g[j]) {
                    join(v, j);
                }
            }
        }

        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        if (ans.size() == 3 && n <= 5) {
            cout << 2 << "\n";
            int i = 0;
            for (auto c:ans) {
                if (i == 0) {
                    continue;
                }
                cout << c << " ";
                i++;
            }
            cout << "\n";
            continue;

        }
        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->second[0])) == 0) {
                ans.insert(getParent(it->second[0]));
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            int pJ = getParent(j);
            if (ans.count(pJ) == 0) {
                ans.insert(pJ);
                for (auto v:g[j]) {
                    join(v, pJ);
                }
            }
        }

        cout << ans.size() << "\n";
        if (ans.size() == 3 && n / 2 == 2) {
            cout << 2 << "\n";
            for (auto c:ans) {
                cout << c << " ";
            }
            cout << "\n";
            cout << "\n";
            cout << "\n";
            return 0;
        }
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

void setParent(int a, int newP) {
    int p = getParent(a);
    parent[p] = newP;
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;
        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        int ma = 0;
        int ve = 0;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (ma < it->second.size()) {
                ma = it->second.size();
                ve = it->first;
            }
        }
        set<int> ans;
        ans.insert(ve);
        for (auto v:g[ve]) {
            join(v, ve);
        }
        for (int j = 1; j <= n; ++j) {
            if (getParent(ve) != getParent(j)) {
                join(j, ve);
                ans.insert(j);
                for (auto v:g[j]) {
                    join(v, ve);
                }
            }
        }

        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;
int parent[200005] = {0};

void init(int t) {
    for (int i = 0; i < t + 2; ++i) {
        parent[i] = 0;
    }
}

int getParent(int v) {
    if (parent[v] == 0) {
        return v;
    }
    parent[v] = getParent(parent[v]);
    return parent[v];
}

void join(int a, int b) {
    int pa = getParent(a);
    int pb = getParent(b);
    if (pa != pb) {
        parent[pa] = pb;
    }
}

int main() {

    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int n, m;
    for (int i = 0; i < t; ++i) {
        cin >> n >> m;

        init(n);
        map<int, vector<int>> g;
        int a, b;
        for (int j = 0; j < m; ++j) {
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (n == 2) {
            cout << "1\n1\n";
            continue;
        }

        set<int> ans;
        for (auto it = g.begin(); it != g.end(); ++it) {
            if (it->second.size() == 1 && ans.count(getParent(it->first)) == 0) {
                ans.insert(it->second[0]);
                for (auto v:g[it->second[0]]) {
                    join(v, it->second[0]);
                }
            }
        }

        for (int j = 1; j <= n; ++j) {
            if (ans.count(getParent(j)) == 0) {
                ans.insert(j);
                for (auto v:g[j]) {
                    join(v, j);
                }
            }
        }

        cout << ans.size() << "\n";
        for (auto c:ans) {
            cout << c << " ";
        }
        cout << "\n";
    }
}


// Author : ps41
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, u, v, c = 0;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) a[i] = -1, g[i].clear();
        for (int i = 0; i < m; i++) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
        for (int i = 1; i <= n; i++)
        {
            if (a[i] != -1) continue;
            a[i] = 1, c++;
            for (auto j: g[i]) a[j] = 0;
        }
        if (c <= n / 2) cout << c << "\n";
        if (c <= n / 2) for (int i = 1; i <= n; i++) if (a[i]) cout << i << " ";
        if (c > n / 2) cout << n - c << "\n";
        if (c > n / 2) for (int i = 1; i <= n; i++) if (!a[i]) cout << i << " ";
        cout << "\n";
    }
}


// Author : ps41

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const int M = 1e9 + 7, N = 2e5 + 5;
int deg[N], ans[N];
pair<int, int> p[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) ans[i] = -1, deg[i] = 0, g[i].clear();
        for (int i = 0; i < m; i++)
        {
            int u, v;
            cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
            deg[u]++, deg[v]++;
        }
        for (int i = 0; i < n; i++) p[i] = {deg[i + 1], i + 1};
        sort(p, p + n);
        reverse(p, p + n);
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            int u = p[i].second;
            if (ans[u] != -1) continue;
            ans[u] = 1, cnt++;
            for (auto j: g[u]) ans[j] = 0;
        }
        if (cnt <= n / 2)
        {
            cout << cnt << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] == 1) cout << i << " ";
        }
        else
        {
            cout << n - cnt << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] == 0) cout << i << " ";
        }
        cout << "\n";
    }
}


// Author : ps41

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const int M = 1e9 + 7, N = 1e5 + 5;
int deg[N], ans[N];
pair<int, int> p[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) ans[i] = -1, deg[i] = 0, g[i].clear();
        for (int i = 0; i < m; i++)
        {
            int u, v;
            cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
            deg[u]++, deg[v]++;
        }
        for (int i = 0; i < n; i++) p[i] = {deg[i + 1], i + 1};
        sort(p, p + n);
        reverse(p, p + n);
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            int u = p[i].second;
            if (ans[u] != -1) continue;
            ans[u] = 1, cnt++;
            for (auto j: g[u]) ans[j] = 0;
        }
        if (cnt <= n / 2)
        {
            cout << cnt << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] == 1) cout << i << " ";
        }
        else
        {
            cout << n - cnt;
            for (int i = 1; i <= n; i++) if (ans[i] == 0) cout << i << " ";
        }
        cout << "\n";
    }
}


// Author : ps41

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const int M = 1e9 + 7, N = 1e5 + 5;
int deg[N], ans[N];
pair<int, int> p[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) ans[i] = -1, deg[i] = 0, g[i].clear();
        for (int i = 0; i < m; i++)
        {
            int u, v;
            cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
            deg[u]++, deg[v]++;
        }
        for (int i = 0; i < n; i++) p[i] = {deg[i + 1], i + 1};
        sort(p, p + n);
        reverse(p, p + n);
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            int u = p[i].second;
            if (ans[u] != -1) continue;
            ans[u] = 1, cnt++;
            for (auto j: g[u]) ans[j] = 0;
        }
        if (cnt <= n / 2)
        {
            cout << cnt << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] == 1) cout << i << " ";
        }
        else
        {
            cout << n - cnt << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] == 0) cout << i << " ";
        }
        cout << "\n";
    }
}


// Author : ps41
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, u, v, c = 0;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) a[i] = -1, g[i].clear();
        for (int i = 0; i < m; i++) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
        for (int i = 1; i <= n; i++)
        {
            if (a[i] != -1) continue;
            a[i] = 1, c++;
            for (auto j: g[i]) a[j] = 0;
        }
        if (c <= n / 2)
        {
            cout << c << "\n";
            for (int i = 1; i <= n; i++) if (a[i]) cout << i << " ";
        }
        else
        {
            cout << n - c << "\n";
            for (int i = 1; i <= n; i++) if (!a[i]) cout << i << " ";
        }
        cout << "\n";
    }
}


// Author : ps41
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7, N = 2e5 + 5;
int vis[N], ans[N], odd, even, cnt;
vector<vector<int>> g(N);
void dfs(int node)
{
    if (cnt % 2) odd++;
    else even++;
    vis[node] = 1, ans[node] = cnt++;
    for (auto i: g[node]) if(!vis[i]) dfs(i);
    cnt--;
}
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) odd = 0, even = 0, cnt = 0, vis[i] = 0, ans[i] = 0, g[i].clear();
        for (int i = 0; i < m; i++)
        {
            int u, v;
            cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
        }
        dfs(1);
        if (odd < even)
        {
            cout << odd << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] % 2) cout << i << " ";
        }
        else
        {
            cout << even << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] % 2 == 0) cout << i << " ";
        }
        cout << "\n";
    }
}


// Author : ps41

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const int M = 1e9 + 7, N = 1e5 + 5;
int deg[N], ans[N];
pair<int, int> p[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) ans[i] = -1, deg[i] = 0, g[i].clear();
        for (int i = 0; i < m; i++)
        {
            int u, v;
            cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
            deg[u]++, deg[v]++;
        }
        for (int i = 0; i < n; i++) p[i] = {deg[i + 1], i + 1};
        sort(p, p + n);
        reverse(p, p + n);
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            int u = p[i].second;
            if (ans[u] != -1) continue;
            ans[u] = 1, cnt++;
            for (auto j: g[u]) ans[j] = 0;
        }
        cout << cnt << "\n";
        for (int i = 1; i <= n; i++) if (ans[i] == 1) cout << i << " ";
        cout << "\n";
    }
}


// Author : ps41
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7, N = 1e5 + 5;
int vis[N], ans[N], odd, even, cnt;
vector<vector<int>> g(N);
void dfs(int node)
{
    if (cnt % 2) odd++;
    else even++;
    vis[node] = 1, ans[node] = cnt++;
    for (auto i: g[node]) if(!vis[i]) dfs(i);
    cnt--;
}
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) odd = 0, even = 0, cnt = 0, vis[i] = 0, ans[i] = 0, g[i].clear();
        for (int i = 0; i < m; i++)
        {
            int u, v;
            cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
        }
        dfs(1);
        if (odd < even)
        {
            cout << odd << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] % 2) cout << i << " ";
        }
        else
        {
            cout << even << "\n";
            for (int i = 1; i <= n; i++) if (ans[i] % 2 == 0) cout << i << " ";
        }
        cout << "\n";
    }
}


// Author : ps41

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const int M = 1e9 + 7, N = 2e5 + 5;
int ans[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, u, v, cnt = 0;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) ans[i] = -1, g[i].clear();
        for (int i = 0; i < m; i++) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
        for (int i = 1; i <= n; i++)
        {
            if (ans[i] != -1) continue;
            ans[i] = 1, cnt++;
            for (auto j: g[i]) ans[j] = 0;
        }
        if (cnt <= n / 2) cout << cnt << "\n";
        if (cnt <= n / 2) for (int i = 1; i <= n; i++) if (ans[i] == 1) cout << i << " ";
        if (cnt > n / 2) cout << n - cnt << "\n";
        if (cnt > n / 2) for (int i = 1; i <= n; i++) if (ans[i] == 0) cout << i << " ";
        cout << "\n";
    }
}


// Author : ps41
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N];
vector<vector<int>> g(N);
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, u, v, c = 0;
        cin >> n >> m;
        for (int i = 0; i <= n; i++) a[i] = -1, g[i].clear();
        for (int i = 0; i < m; i++) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
        for (int i = 1; i <= n; i++)
        {
            if (a[i] != -1) continue;
            a[i] = 1, c++;
            for (auto j: g[i]) a[j] = 0;
        }
        if (c <= n / 2) cout << c << "\n";
        if (c <= n / 2) for (int i = 1; i <= n; i++) if (a[i] == 1) cout << i << " ";
        if (c > n / 2) cout << n - c << "\n";
        if (c > n / 2) for (int i = 1; i <= n; i++) if (a[i] == 0) cout << i << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
#define sci(a) scanf("%d",&a)
#define scii(a,b) scanf("%d %d",&a,&b)
#define pri(a) printf("%d",a)
#define prn() printf("\n");
#define pb(a) push_back(a)
using namespace std;
const int maxn=500005;

int t,n,m,x,y;
vector<int> p[maxn];
int c[maxn];

void dfs(int i,int color)
{
    if(c[i]==t||c[i]==t*2+2)
        return;
    c[i]=color;
    for(int j=0;j<p[i].size();++j)
        dfs(p[i][j],t*3+2-color);
}

void init()
{
    for(int i=1;i<=n;++i)
        p[i].clear();
}

void out()
{
    int ct=0;
    for(int i=1;i<=n;++i)
        if(c[i]==t)
            ct++;
    if(ct<=n/2){
        pri(ct);
        prn();
        for(int i=1;i<=n;++i){
            if(c[i]==t)
                printf("%d ",i);
        }
        prn();
    }
    else{
        pri(n-ct);
        prn();
        for(int i=1;i<=n;++i){
            if(c[i]==t*2+2)
                printf("%d ",i);
        }
        prn();
    }
}

void debug()
{
    cout<<t<<endl;
    for(int i=1;i<=n;++i)
        cout<<c[i]<<" ";
    cout<<endl;
}

int main()
{
    sci(t);
    while(t--){
        scii(n,m);
        init();
        while(m--){
            scii(x,y);
            p[x].pb(y);
            p[y].pb(x);
        }
        dfs(1,t);
        out();
        //debug();
    }
    return 0;
}


#include <bits/stdc++.h>
#define re(i,a,b) for(int i=a;i<=b;++i)
#define fo(i,a,b) for(int i=a;i<b;++i)
#define scd(a) scanf("%lf",&a)
#define scc(a) scanf("%c",&a)
#define scs(a) scanf("%s",a)
#define scii(a,b) scanf("%d%d",&a,&b)
#define sci(a) scanf("%d",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define scl(a) scanf("%lld",&a)
#define prd(a) printf("%lf",a)
#define prc(a) printf("%c",a)
#define prs(a) printf("%s",a)
#define pri(a) printf("%d",a)
#define prl(a) printf("%lld",a)
#define prn() printf("\n")
#define pub(a) push_back(a)
#define pob() pop_back()
#define puf(a) push_front(a)
#define pof() pop_front()
#define rre(i,a,b) for(int i=a;i>=b;--i)
#define rfo(i,a,b) for(int i=a;i>b;--i)
#define endl "\n"
#define debug(a) cout<<a<<endl
using namespace std;
typedef long long ll;
const ll maxn=200005;
const ll inf=0x3f3f3f3f;
const double pi=3.1415926535;
const double esp=1e-10;
const int mod=10007;
template <typename t>
t mmax(t a,t b,t c){return max(a,max(b,c));}
template <typename t>
t mmin(t a,t b,t c){return min(a,min(b,c));}

ll t,n,m,x,y;
bool vis[maxn];
vector<ll> p[maxn];
vector<ll> as;
ll cnt[maxn];
struct edge
{
    ll x,v;
}e[maxn];

bool cmp(edge a,edge b)
{
    return a.v>b.v;
}

int main()
{
    cin>>t;
    while(t--){
        cin>>n>>m;
        memset(vis,0,sizeof(vis));
        memset(e,0,sizeof(e));
        memset(cnt,0,sizeof(cnt));
        re(i,1,n)
            p[i].clear();
        as.clear();
        while(m--){
            cin>>x>>y;
            p[x].pub(y);
            p[y].pub(x);
            cnt[x]++;
            cnt[y]++;
        }
        re(i,1,n)
        {
            e[i].x=i;
            e[i].v=cnt[i];
        }
        sort(e+1,e+1+n,cmp);
        re(i,1,n)
            if(p[i].size()==0)
                as.pub(i),vis[i]=true;
        re(i,1,n)
        {
            if(vis[e[i].x])
                continue;
            vis[e[i].x]=true;
            as.pub(e[i].x);
            if(as.size()==n/2)
                break;
            fo(j,0,p[e[i].x].size())
                vis[p[e[i].x][j]]=true;
        }
        cout<<as.size()<<endl;
        fo(i,0,as.size())
            cout<<as[i]<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define re(i,a,b) for(int i=a;i<=b;++i)
#define fo(i,a,b) for(int i=a;i<b;++i)
#define scd(a) scanf("%lf",&a)
#define scc(a) scanf("%c",&a)
#define scs(a) scanf("%s",a)
#define scii(a,b) scanf("%d%d",&a,&b)
#define sci(a) scanf("%d",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define scl(a) scanf("%lld",&a)
#define prd(a) printf("%lf",a)
#define prc(a) printf("%c",a)
#define prs(a) printf("%s",a)
#define pri(a) printf("%d",a)
#define prl(a) printf("%lld",a)
#define prn() printf("\n")
#define pub(a) push_back(a)
#define pob() pop_back()
#define puf(a) push_front(a)
#define pof() pop_front()
#define rre(i,a,b) for(int i=a;i>=b;--i)
#define rfo(i,a,b) for(int i=a;i>b;--i)
#define endl "\n"
#define debug(a) cout<<a<<endl
using namespace std;
typedef long long ll;
const ll maxn=200005;
const ll inf=0x3f3f3f3f;
const double pi=3.1415926535;
const double esp=1e-10;
const int mod=10007;
template <typename t>
t mmax(t a,t b,t c){return max(a,max(b,c));}
template <typename t>
t mmin(t a,t b,t c){return min(a,min(b,c));}

ll t,n,m,x,y;
ll cnt[maxn],as[maxn];

int main()
{
    scl(t);
    while(t--){
        scll(n,m);
        memset(cnt,0,sizeof(cnt));
        memset(as,0,sizeof(as));
        while(m--){
            scll(x,y);
            cnt[x]++;
            cnt[y]++;
        }
        ll ix=0;
        re(i,1,n)
        {
            if(ix>=n/4+1)
                break;
            as[ix]=i;
            ix++;
        }
        rre(i,n,1)
        {
            if(ix>=n/2)
                break;
            as[ix]=i;
            ix++;
        }
        printf("%lld\n",ix);
        fo(i,0,ix)
            printf("%lld ",as[i]);
        prn();
    }
    return 0;
}


#include <bits/stdc++.h>
#define re(i,a,b) for(int i=a;i<=b;++i)
#define fo(i,a,b) for(int i=a;i<b;++i)
#define scd(a) scanf("%lf",&a)
#define scc(a) scanf("%c",&a)
#define scs(a) scanf("%s",a)
#define scii(a,b) scanf("%d%d",&a,&b)
#define sci(a) scanf("%d",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define scl(a) scanf("%lld",&a)
#define prd(a) printf("%lf",a)
#define prc(a) printf("%c",a)
#define prs(a) printf("%s",a)
#define pri(a) printf("%d",a)
#define prl(a) printf("%lld",a)
#define prn() printf("\n")
#define pub(a) push_back(a)
#define pob() pop_back()
#define puf(a) push_front(a)
#define pof() pop_front()
#define rre(i,a,b) for(int i=a;i>=b;--i)
#define rfo(i,a,b) for(int i=a;i>b;--i)
#define endl "\n"
#define debug(a) cout<<a<<endl
using namespace std;
typedef long long ll;
const ll maxn=500005;
const ll inf=0x3f3f3f3f;
const double pi=3.1415926535;
const double esp=1e-10;
const int mod=10007;
template <typename t>
t mmax(t a,t b,t c){return max(a,max(b,c));}
template <typename t>
t mmin(t a,t b,t c){return min(a,min(b,c));}

ll t,n,m,x,y;
bool vis[maxn];
vector<ll> p[maxn];
vector<ll> as;
ll cnt[maxn];
struct edge
{
    ll x,v;
}e[maxn];

bool cmp(edge a,edge b)
{
    return a.v>b.v;
}

int main()
{
    scl(t);
    while(t--){
        scll(n,m);
        memset(vis,0,sizeof(vis));
        memset(e,0,sizeof(e));
        memset(cnt,0,sizeof(cnt));
        re(i,1,n)
            p[i].clear();
        as.clear();
        while(m--){
            scll(x,y)
            p[x].pub(y);
            p[y].pub(x);
            cnt[x]++;
            cnt[y]++;
        }
        re(i,1,n)
        {
            e[i].x=i;
            e[i].v=cnt[i];
        }
        sort(e+1,e+1+n,cmp);
        re(i,1,n)
            if(p[i].size()==0)
                as.pub(i),vis[i]=true;
        re(i,1,n)
        {
            if(vis[e[i].x])
                continue;
            vis[e[i].x]=true;
            as.pub(e[i].x);
            if(as.size()==n/2)
                break;
            fo(j,0,p[e[i].x].size())
                vis[p[e[i].x][j]]=true;
        }
        printf("%d\n",as.size());
        fo(i,0,as.size())
            printf("%lld ",as[i]);
        prn();
    }
    return 0;
}