#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define rep(i,a,b) for(ll i=a;i<=b;++i)
typedef pair<ll,ll> pll;
const ll N=4e5+5;
vector<ll>vec[N],vcc[N];
ll t,n,m,k,tt,tp;
ll vis[N];
ll B;
void dfs(ll u,ll pre)
{
if(vis[u])return ;
vis[u]=1;
if(pre!=0)
{
vcc[u].pb(pre);
vcc[pre].pb(u);
}
for(auto v:vec[u])if(v!=pre)dfs(v,u);
}
vector<ll>v;
ll dep[N],vc[N];
ll dfss(ll u,ll pre)
{
if(vis[u])continue;
vis[u]=1;
ll dp=0;
for(auto v:vec[u])if(v!=pre)
{
dp=max(dp,dfss(v,u)+1);
}
if(dp==B)
{
v.pb(u);
return -1;
}
return dp;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int T=1;
cin>>T;
while(T--)
{
cin>>n>>m;
v.clear();
rep(i,1,n)
{
vis[i]=vc[i]=0;
vec[i].clear(),vcc[i].clear();
}
rep(i,1,m)
{
ll u,v;
cin>>u>>v;
vec[u].pb(v);
vec[v].pb(u);
}
// dfs(1,0);
B=(ll)sqrt(n-1.0)+1;
dfss(1,0);
sort(v.begin(),v.end());
if(v.empty()||v[0]!=1)v.pb(1);
assert(v.size()<=B);
cout<<v.size()<<"\n";
for(auto x:v)cout<<x<<" ";
cout<<"\n";
}
}
/*
2
4 3
1 2
2 3
3 4
6 7
1 2
2 3
3 1
1 4
4 5
5 6
6 4
*/