#include <bits/stdc++.h>
    #include<ext/pb_ds/assoc_container.hpp>
    #include<ext/pb_ds/tree_policy.hpp>
    using namespace __gnu_pbds;
    using namespace std;
    template <typename T>
    using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
    #define int long long
    typedef long double ld;
    #define pb push_back
    #define f(i,a,b) for(int i=a;i<b;i++)
    #define fd(i,a,b) for(int i=a-1;i>=b;i--)
    #define pf push_front
    #define fi first
    #define se second
     int mn=1e9+7;
    int cnt=0;
    int vis[200000+10]={0};
    void dfs(int i,vector<vector<int >> &v)
    {
        vis[i]=1;
        for(auto j:v[i])
        {
            if(vis[j]==0)
            {
                dfs(j,v);
            }
        }
    }
    vector<pair<int ,int >> v1,v2;
    signed main() {
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("INPUT.txt", "r", stdin);
    // for writing output to output.txt
    freopen("OUTPUT.txt", "w", stdout);
    #endif

    // -------------------------------------Code starts here---------------------------------------------------------------------
    int t=1;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        vector<vector<int >> v;
        v.resize(n+1);
        vector<int > e;
        fill_n(vis,200000+10,0);
        while(m--)
        {
            int x,y;
            cin>>x>>y;
            v[x].pb(y);
            v[y].pb(x);
        }
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                e.pb(i);
                cnt++;
                dfs(i,v);
            }
        }
        cout<<cnt<<endl;
        for(auto i:e)
        {
            cout<<i<<" ";
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
    #include<ext/pb_ds/assoc_container.hpp>
    #include<ext/pb_ds/tree_policy.hpp>
    using namespace __gnu_pbds;
    using namespace std;
    template <typename T>
    using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
    #define int long long
    typedef long double ld;
    #define pb push_back
    #define f(i,a,b) for(int i=a;i<b;i++)
    #define fd(i,a,b) for(int i=a-1;i>=b;i--)
    #define pf push_front
    #define fi first
    #define se second
     int mn=1e9+7;
    int cnt=0;
    int vis[200000+10]={0};
    int r=0;

    void dfs(int i,vector<vector<int >> &v,vector<int > &e,vector<int > &f)
    {
        vis[i]=1;
        if(r==0)
        {
            e.pb(i);
            r=1;
        }
        else
        {
            f.pb(i);
            r=0;
        }
        for(auto j:v[i])
        {
            if(vis[j]==0)
            {
                dfs(j,v,e,f);
            }
        }
    }
    vector<pair<int ,int >> v1,v2;
    signed main() {
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("INPUT.txt", "r", stdin);
    // for writing output to output.txt
    freopen("OUTPUT.txt", "w", stdout);
    #endif

    // -------------------------------------Code starts here---------------------------------------------------------------------
    int t=1;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        vector<vector<int >> v;
        v.resize(n+1);
        vector<int > e,f;
        r=0;
        while(m--)
        {
            int x,y;
            cin>>x>>y;
            v[x].pb(y);
            v[y].pb(x);
        }
        int cnt=0;
        dfs(1,v,e,f);
        if(e.size()>f.size())
        {
            cout<<f.size()<<endl;
            for(auto i:f)
            {
                cout<<i<<" ";
            }
            cout<<endl;
        }
        else
        {
            cout<<e.size()<<endl;
            for(auto i:e)
            {
                cout<<i<<" ";
            }
            cout<<endl;
        }
        fill_n(vis,n+1,0);
        }
    }


#include <bits/stdc++.h>
    #include<ext/pb_ds/assoc_container.hpp>
    #include<ext/pb_ds/tree_policy.hpp>
    using namespace __gnu_pbds;
    using namespace std;
    template <typename T>
    using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
    #define int long long
    typedef long double ld;
    #define pb push_back
    #define f(i,a,b) for(int i=a;i<b;i++)
    #define fd(i,a,b) for(int i=a-1;i>=b;i--)
    #define pf push_front
    #define fi first
    #define se second
     int mn=1e9+7;
    int cnt=0;
    int vis[200000+10]={0};
    int ans[200000+10]={0};
    vector<vector<int >> v;
    void dfs(int j,int x)
    {
        vis[j]=1;
        if(x%2==1)
        {
            cnt++;
            ans[j]=1;
        }
        for(auto i:v[j])
        {
            if(vis[i]==0)
            {
                dfs(i,x+1);
            }
        }
    }
    vector<pair<int ,int >> v1,v2;
    signed main() {
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("INPUT.txt", "r", stdin);
    // for writing output to output.txt
    freopen("OUTPUT.txt", "w", stdout);
    #endif

    // -------------------------------------Code starts here---------------------------------------------------------------------
    int t=1;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        v.resize(n);
        cnt=0;
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            x--;
            y--;
            v[x].pb(y);
            v[y].pb(x);
        }

        f(i,0,n)
        {
            vis[i]=0;
            ans[i]=0;
        }
        dfs(0,0);
        if(cnt>n/2)
        {
            cout<<n-cnt<<endl;
            f(i,0,n)
            {
                if(ans[i]==0)
                {
                    cout<<i+1<<" ";
                }
            }
        }
        else
        {
            cout<<cnt<<endl;
            f(i,0,n)
            {
                if(ans[i]==1)
                    cout<<i+1<<" ";
            }
        }
        cout<<endl;

    }
}


#include <bits/stdc++.h>
    #include<ext/pb_ds/assoc_container.hpp>
    #include<ext/pb_ds/tree_policy.hpp>
    using namespace __gnu_pbds;
    using namespace std;
    template <typename T>
    using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  //  #define int long long
    typedef long double ld;
    #define pb push_back
    #define f(i,a,b) for(int i=a;i<b;i++)
    #define fd(i,a,b) for(int i=a-1;i>=b;i--)
    #define pf push_front
    #define fi first
    #define se second
     int mn=1e9+7;
    int cnt=0;
    int vis[200000+10]={0};
    int ans[200000+10]={0};
    vector<vector<int >> v;
    void dfs(int j,int x)
    {
        vis[j]=1;
        if(x%2==1)
        {
            cnt++;
            ans[j]=1;
        }
        for(auto i:v[j])
        {
            if(vis[i]==0)
            {
                dfs(i,x+1);
            }
        }
    }
    vector<pair<int ,int >> v1,v2;
    signed main() {
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("INPUT.txt", "r", stdin);
    // for writing output to output.txt
    freopen("OUTPUT.txt", "w", stdout);
    #endif

    // -------------------------------------Code starts here---------------------------------------------------------------------
    int t=1;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        v.resize(n);
        cnt=0;
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            x--;
            y--;
            v[x].pb(y);
            v[y].pb(x);
        }

        f(i,0,n)
        {
            vis[i]=0;
            ans[i]=0;
        }
        dfs(0,0);
        if(cnt>n/2)
        {
            cout<<n-cnt<<endl;
            f(i,0,n)
            {
                if(ans[i]==0)
                {
                    cout<<i+1<<" ";
                }
            }
        }
        else
        {
            cout<<cnt<<endl;
            f(i,0,n)
            {
                if(ans[i]==1)
                    cout<<i+1<<" ";
            }
        }
        cout<<endl;

    }
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail;
inline void bfs(int S)
{
    col[S] = 1;
    top = tail = 0;
    Q[top] = S;
    while(top <= tail)
    {
        int u = Q[top++];
        for(int i = head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(col[v]) continue;
            col[v] = 3 - col[u];
            Q[++tail] = v;
        }
    }
}
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(!col[v])
        {
            col[v] = 3 - col[u];
            dfs(v);
        }
    }
}
int out[maxN];
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        bool flag = false;
        for(int i=1; i<=N; i++)
        {
            if(du[i] == 1 && !col[i])
            {
                flag = true;
                bfs(i);
            }
        }
        if(!flag) { col[1] = 1; dfs(1); }
        int ans = 0;
        for(int i=1; i<=N; i++)
        {
            if(col[i] == 1)
            {
                out[++ans] = i;
            }
        }
        printf("%d\n", ans);
        for(int i=1; i<=ans; i++) printf("%d%c", out[i], i == ans ? '\n' : ' ');
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail, ans[3];
int out[maxN];
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(col[v]) continue;
        col[v] = 3 - col[u];
        out[++ans[col[v]]] = v;
        dfs(v);
    }
}
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        ans[0] = ans[1] = 0;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        out[++ans[1]] = 1; col[1] = 1; dfs(1);
        int team = ans[1] > ans[2] ? 2 : 1;
        printf("%d\n", ans[team]);
        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail, ans[3];
int out[maxN];
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(col[v]) continue;
        col[v] = 3 - col[u];
        out[++ans[col[v]]] = v;
        dfs(v);
    }
}
int deep[maxN];
bool vis[maxN];
inline void bfs(int S)
{
    queue<int> Q;
    Q.push(S); deep[S] = 0;
    vis[S] = true;
    out[++ans[deep[S]]] = S;
    while(!Q.empty())
    {
        int u = Q.front();  Q.pop();
        for(int i=head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(vis[v]) continue;
            vis[v] = true;
            deep[v] = deep[u] ^ 1;
            out[++ans[deep[v]]] = v;
            Q.push(v);
        }
    }
}
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = deep[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        ans[1] = ans[2] = 0;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1; i<=N; i++) vis[i] = false;
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        //out[++ans[1]] = 1; col[1] = 1; dfs(1);
        //int team = ans[1] > ans[2] ? 2 : 1;
        //printf("%d\n", ans[team]);
        //for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
        bfs(1);
        int team = ans[0] > ans[1] ? 1 : 0;
        printf("%d\n", ans[team]);
        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
    }
    return 0;
}
/*
3
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

4 4
2 3
1 3
4 2
1 2

3 3
2 1
3 1
2 3
*/


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail, ans[3];
int out[maxN];
inline void bfs(int S)
{
    col[S] = 1;
    top = tail = 0;
    Q[top] = S;
    while(top <= tail)
    {
        int u = Q[top++];
        for(int i = head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(col[v]) continue;
            col[v] = 3 - col[u];
            Q[++tail] = v;
        }
    }
}
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(!col[v])
        {
            col[v] = 3 - col[u];
            out[++ans[col[v]]] = v;
            dfs(v);
        }
    }
}
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        ans[1] = ans[2] = 0;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        bool flag = false;
        for(int i=1; i<=N; i++)
        {
            if(du[i] == 1 && !col[i])
            {
                flag = true;
                col[i] = 1;
                dfs(i);
            }
        }
        if(!flag) { col[1] = 1; dfs(1); }
        int team = ans[1] > ans[2] ? 2 : 1;
        printf("%d\n", ans[team]);
        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail, ans[3];
int out[3][maxN];
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(col[v]) continue;
        col[v] = 3 - col[u];
        out[col[v]][++ans[col[v]]] = v;
        dfs(v);
    }
}
int deep[maxN];
bool vis[maxN];
inline void bfs(int S)
{
    queue<int> Q;
    Q.push(S); deep[S] = 0;
    vis[S] = true;
    out[0][++ans[0]] = S;
    while(!Q.empty())
    {
        int u = Q.front();  Q.pop();
        for(int i=head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(vis[v]) continue;
            vis[v] = true;
            deep[v] = deep[u] ^ 1;
            out[deep[v]][++ans[deep[v]]] = v;
            Q.push(v);
        }
    }
}
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        ans[0] = ans[1] = ans[2] = 0;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1; i<=N; i++) du[i] = col[i] = deep[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        for(int i=1; i<=N; i++) vis[i] = false;
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        out[1][++ans[1]] = 1; col[1] = 1; dfs(1);
        int team = ans[1] > ans[2] ? 2 : 1;
        printf("%d\n", ans[team]);
        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[team][i], i == ans[team] ? '\n' : ' ');
//        bfs(1);
//        int team = ans[0] > ans[1] ? 1 : 0;
//        printf("%d\n", ans[team]);
//        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[team][i], i == ans[team] ? '\n' : ' ');
    }
    return 0;
}
/*
3
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

4 4
2 3
1 3
4 2
1 2

3 3
2 1
3 1
2 3
*/


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail, ans[3];
int out[maxN];
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(col[v]) continue;
        col[v] = 3 - col[u];
        out[++ans[col[v]]] = v;
        dfs(v);
    }
}
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        ans[1] = ans[2] = 0;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        out[++ans[1]] = 1; col[1] = 1; dfs(1);
        int team = ans[1] > ans[2] ? 2 : 1;
        printf("%d\n", ans[team]);
        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
    }
    return 0;
}
/*
3
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

4 4
2 3
1 3
4 2
1 2

3 3
2 1
3 1
2 3
*/


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail;
inline void bfs(int S)
{
    col[S] = 1;
    top = tail = 0;
    Q[top] = S;
    while(top <= tail)
    {
        int u = Q[top++];
        for(int i = head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(col[v]) continue;
            col[v] = 3 - col[u];
            Q[++tail] = v;
        }
    }
}
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(!col[v])
        {
            col[v] = 3 - col[u];
            dfs(v);
        }
    }
}
int out[maxN];
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        bool flag = false;
        for(int i=1; i<=N; i++)
        {
            if(du[i] == 1 && !col[i])
            {
                flag = true;
                bfs(i);
            }
        }
        if(!flag) { col[1] = 1; dfs(1); }
        int ans = 0;
        for(int i=1; i<=N; i++)
        {
            if(col[i] == 2)
            {
                out[++ans] = i;
            }
        }
        printf("%d\n", ans);
        for(int i=1; i<=ans; i++) printf("%d%c", out[i], i == ans ? '\n' : ' ');
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail;
inline void bfs(int S)
{
    col[S] = 1;
    top = tail = 0;
    Q[top] = S;
    while(top <= tail)
    {
        int u = Q[top++];
        for(int i = head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(col[v]) continue;
            col[v] = 3 - col[u];
            Q[++tail] = v;
        }
    }
}
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(!col[v])
        {
            col[v] = 3 - col[u];
            dfs(v);
        }
    }
}
int out[maxN];
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        bool flag = false;
        for(int i=1; i<=N; i++)
        {
            if(du[i] == 1 && !col[i])
            {
                flag = true;
                bfs(i);
            }
        }
        if(!flag) { col[1] = 1; dfs(1); }
        int ans = 0;
        for(int i=1; i<=N; i++)
        {
            if(col[i] == 2) out[++ans] = i;
            if(!col[i]) out[++ans] = i;
        }
        printf("%d\n", ans);
        for(int i=1; i<=ans; i++) printf("%d%c", out[i], i == ans ? '\n' : ' ');
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail;
inline void bfs(int S)
{
    col[S] = 1;
    top = tail = 0;
    Q[top] = S;
    while(top <= tail)
    {
        int u = Q[top++];
        for(int i = head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(col[v]) continue;
            col[v] = 3 - col[u];
            Q[++tail] = v;
        }
    }
}
int out[maxN];
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        for(int i=1; i<=N; i++) du[i] = col[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        bool flag = false;
        for(int i=1; i<=N; i++)
        {
            if(du[i] == 1 && !col[i])
            {
                flag = true;
                bfs(i);
            }
        }
        if(!flag) bfs(1);
        int ans = 0;
        for(int i=1; i<=N; i++)
        {
            if(col[i] == 1)
            {
                out[++ans] = i;
            }
        }
        printf("%d\n", ans);
        for(int i=1; i<=ans; i++) printf("%d%c", out[i], i == ans ? '\n' : ' ');
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail, ans[3];
int out[maxN];
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(col[v]) continue;
        col[v] = 3 - col[u];
        out[++ans[col[v]]] = v;
        dfs(v);
    }
}
int deep[maxN];
bool vis[maxN];
inline void bfs(int S)
{
    queue<int> Q;
    Q.push(S); deep[S] = 0;
    vis[S] = true;
    out[++ans[deep[S]]] = S;
    while(!Q.empty())
    {
        int u = Q.front();  Q.pop();
        for(int i=head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(vis[v]) continue;
            vis[v] = true;
            deep[v] = deep[u] ^ 1;
            out[++ans[deep[v]]] = v;
            Q.push(v);
        }
    }
}
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        ans[0] = ans[1] = ans[2] = 0;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1; i<=N; i++) du[i] = col[i] = deep[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        for(int i=1; i<=N; i++) vis[i] = false;
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        //out[++ans[1]] = 1; col[1] = 1; dfs(1);
        //int team = ans[1] > ans[2] ? 2 : 1;
        //printf("%d\n", ans[team]);
        //for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
        bfs(1);
        int team = ans[0] > ans[1] ? 1 : 0;
        printf("%d\n", ans[team]);
        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
    }
    return 0;
}
/*
3
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

4 4
2 3
1 3
4 2
1 2

3 3
2 1
3 1
2 3
*/


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, head[maxN], cnt, du[maxN], col[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int Q[maxN], top, tail, ans[3];
int out[3][maxN];
inline void dfs(int u)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(col[v]) continue;
        col[v] = 3 - col[u];
        out[col[v]][++ans[col[v]]] = v;
        dfs(v);
    }
}
int deep[maxN];
bool vis[maxN];
inline void bfs(int S)
{
    queue<int> Q;
    Q.push(S); deep[S] = 0;
    vis[S] = true;
    out[0][++ans[0]] = S;
    while(!Q.empty())
    {
        int u = Q.front();  Q.pop();
        for(int i=head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(vis[v]) continue;
            vis[v] = true;
            deep[v] = deep[u] ^ 1;
            out[deep[v]][++ans[deep[v]]] = v;
            Q.push(v);
        }
    }
}
int main()
{
    int T;  scanf("%d", &T);
    memset(head, -1, sizeof(head));
    while(T--)
    {
        ans[0] = ans[1] = ans[2] = 0;
        cnt = 0;
        scanf("%d%d", &N, &M);
        for(int i=1; i<=N; i++) du[i] = col[i] = deep[i] = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
        for(int i=1; i<=N; i++) vis[i] = false;
        for(int i=1, u, v; i<=M; i++)
        {
            scanf("%d%d", &u, &v);
            _add(u, v);
            du[u]++; du[v]++;
        }
        //out[++ans[1]] = 1; col[1] = 1; dfs(1);
        //int team = ans[1] > ans[2] ? 2 : 1;
        //printf("%d\n", ans[team]);
        //for(int i=1; i<=ans[team]; i++) printf("%d%c", out[i], i == ans[team] ? '\n' : ' ');
        bfs(1);
        int team = ans[0] > ans[1] ? 1 : 0;
        printf("%d\n", ans[team]);
        for(int i=1; i<=ans[team]; i++) printf("%d%c", out[team][i], i == ans[team] ? '\n' : ' ');
    }
    return 0;
}
/*
3
5 8
2 5
2 1
5 1
4 5
1 4
2 4
3 4
3 5

4 4
2 3
1 3
4 2
1 2

3 3
2 1
3 1
2 3
*/


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2], lvl[N + 2];
    int level[N + 2] = {0};
    int u, v, maxConnected = 1;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        if (edge[maxConnected].size() < edge[u].size())
            maxConnected = u;
        if (edge[maxConnected].size() < edge[v].size())
            maxConnected = v;
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(maxConnected);
    level[maxConnected] = 1;
    lvl[maxConnected].push_back(maxConnected);
    int maxLevel = 0;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                lvl[level[v]].push_back(v);
                bfsQueue.push(v);
            }
        }
    }
    set<pair<int, int>> minExtract;
    for (int i = 1; i <= N; i++)
    {
        minExtract.insert({lvl[level[i]].size(), level[i]});
    }

    while (minExtract.size())
    {
        u = minExtract.begin()->second;
        minExtract.erase(minExtract.begin());
        minExtract.erase({lvl[level[u - 1]].size(), u - 1});
        minExtract.erase({lvl[level[u + 1]].size(), u + 1});
        for (int x : lvl[u])
            chosen.push_back(x);
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2], lvl[N + 2];
    int level[N + 2] = {0};
    int u, v, maxConnected = 1;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        if (edge[maxConnected].size() < edge[u].size())
            maxConnected = u;
        if (edge[maxConnected].size() < edge[v].size())
            maxConnected = v;
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(maxConnected);
    level[maxConnected] = 1;
    lvl[1].push_back(maxConnected);
    int maxLevel = 0;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                lvl[level[v]].push_back(v);
                bfsQueue.push(v);
            }
        }
    }
    set<pair<int, int>> minExtract;
    for (int i = 1; i <= N; i++)
    {
        minExtract.insert({lvl[level[i]].size(), level[i]});
    }

    while (minExtract.size())
    {
        u = minExtract.begin()->second;
        minExtract.erase({lvl[u].size(), u});
        minExtract.erase({lvl[u - 1].size(), u - 1});
        minExtract.erase({lvl[u + 1].size(), u + 1});
        for (int x : lvl[u])
            chosen.push_back(x);
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2], lvl[N + 2];
    int level[N + 2] = {0};
    int u, v;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(1);
    level[1] = 1;
    lvl[1].push_back(1);
    int maxLevel = 0;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                lvl[level[v]].push_back(v);
                bfsQueue.push(v);
            }
        }
    }
    set<pair<int, int>> minExtract;
    for (int i = 1; i <= N; i++)
    {
        minExtract.insert({lvl[level[i]].size(), level[i]});
    }

    while (minExtract.size())
    {
        u = minExtract.begin()->second;
        minExtract.erase(minExtract.begin());
        minExtract.erase({lvl[level[u - 1]].size(), u - 1});
        minExtract.erase({lvl[level[u + 1]].size(), u + 1});
        for (int x : lvl[u])
            chosen.push_back(x);
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2];
    int level[N + 2] = {0};
    int u, v, maxConnected = 1;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        if (edge[maxConnected].size() < edge[u].size())
            maxConnected = u;
        if (edge[maxConnected].size() < edge[v].size())
            maxConnected = v;
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(maxConnected);
    level[maxConnected] = 1;
    int maxLevel = 0;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                bfsQueue.push(v);
            }
        }
    }
    //cout << maxConnected << endl;
    for (int i = 1; i <= N; i++)
    {
        if (level[i] % 3 == 1)
            chosen.push_back(i);
        if (maxLevel % 3 == 0 && level[i] == maxLevel)
            chosen.push_back(i);
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2];
    int level[N + 2] = {0};
    int u, v;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(1);
    level[1] = 1;

    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        if (level[u] % 2 == 0)
            chosen.push_back(u);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                bfsQueue.push(v);
            }
        }
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2];
    int level[N + 2] = {0};
    int u, v;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(1);
    level[1] = 1;

    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        if (level[u] % 3 == 1)
            chosen.push_back(u);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                bfsQueue.push(v);
            }
        }
    }

    assert(chosen.size() <= N / 2);
    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2];
    int level[N + 2] = {0};
    int u, v;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(1);
    level[1] = 1;
    int maxLevel = 0;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                bfsQueue.push(v);
            }
        }
    }

    for (int i = 1; i <= N; i++)
    {
        if (level[i] % 3 == 1)
            chosen.push_back(i);
        if (maxLevel % 3 == 0 && level[i] == maxLevel)
            chosen.push_back(i);
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2], lvl[N + 2];
    int level[N + 2] = {0};
    int u, v, maxConnected = 1;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        if (edge[maxConnected].size() < edge[u].size())
            maxConnected = u;
        if (edge[maxConnected].size() < edge[v].size())
            maxConnected = v;
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(maxConnected);
    level[maxConnected] = 1;
    lvl[1].push_back(maxConnected);
    int maxLevel = 0;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                lvl[level[v]].push_back(v);
                bfsQueue.push(v);
            }
        }
    }
    set<pair<int, int>> minExtract;
    for (int i = 1; i <= N; i++)
    {
        minExtract.insert({lvl[level[i]].size(), level[i]});
    }

    while (minExtract.size())
    {
        u = minExtract.begin()->second;
        minExtract.erase({lvl[u].size(), u});
        minExtract.erase({lvl[u - 1].size(), u - 1});
        minExtract.erase({lvl[u + 1].size(), u + 1});
        for (int x : lvl[u])
            chosen.push_back(x);
    }

    if(chosen.size()>N/2) {
        cout<<N<<' '<<M<<endl;
        return;
    }
    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    //O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    long long N, M;

    set<pair<int, int>> maxConectedVertice;
    set<int> chosen;

    cin >> N >> M;
    set<int> edge[N + 2];
    int u, v;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].insert(v);
        edge[v].insert(u);
    }
    for (int i = 1; i <= N; i++)
        maxConectedVertice.insert({(int)edge[i].size() * -1, i});

    while (maxConectedVertice.size())
    {
        u = maxConectedVertice.begin()->second;
        //cout << u << endl;
        maxConectedVertice.erase(maxConectedVertice.begin());
        //cout << "Bug" << endl;
        chosen.insert(u);
        for (int v : edge[u])
        {
            //cout << v << ' ';
            maxConectedVertice.erase({(int)edge[v].size() * -1, v});
            edge[v].erase(u);
            for (int x : edge[v])
            {
                maxConectedVertice.erase({(int)edge[x].size() * -1, x});
                edge[x].erase(v);
                maxConectedVertice.insert({(int)edge[x].size() * -1, x});
            }
        }
        //cout << "Debug" << endl;
    }

    if (chosen.size() > N / 2)
    {
        cout << N - chosen.size() << endl;
        for (int i = 1; i <= N; i++)
        {
            if (chosen.find(i) == chosen.end())
                cout << i << ' ';
        }
    }
    else
    {
        cout << chosen.size() << endl;
        for (int i = 1; i <= N; i++)
        {
            if (chosen.find(i) != chosen.end())
                cout << i << ' ';
        }
    }
    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2], lvl[N + 2];
    int level[N + 2] = {0};
    int u, v, maxConnected = 1;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        if (edge[maxConnected].size() < edge[u].size())
            maxConnected = u;
        if (edge[maxConnected].size() < edge[v].size())
            maxConnected = v;
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(maxConnected);
    level[maxConnected] = 1;
    lvl[1].push_back(maxConnected);
    int maxLevel = 0;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                lvl[level[v]].push_back(v);
                bfsQueue.push(v);
            }
        }
    }
    set<pair<int, int>> minExtract;
    for (int i = 1; i <= N; i++)
    {
        minExtract.insert({lvl[level[i]].size(), level[i]});
    }

    while (minExtract.size())
    {
        u = minExtract.begin()->second;
        minExtract.erase({lvl[u].size(), u});
        minExtract.erase({lvl[u - 1].size(), u - 1});
        minExtract.erase({lvl[u + 1].size(), u + 1});
        for (int x : lvl[u])
            chosen.push_back(x);
    }

    if(chosen.size()>N/2) {
        cout<<"Here "<<N<<' '<<M<<endl;
        return;
    }
    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2];
    int level[N + 2] = {0};
    int u, v;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(1);
    level[1] = 1;

    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        if (level[u] % 3 == 1)
            chosen.push_back(u);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                bfsQueue.push(v);
            }
        }
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    O_O;
    int tst = 1;
    cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    int N, M;
    cin >> N >> M;
    vector<int> edge[N + 2];
    int level[N + 2] = {0};
    int u, v;
    for (int i = 0; i < M; i++)
    {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    queue<int> bfsQueue;
    vector<int> chosen;
    bfsQueue.push(1);
    level[1] = 1;
    int maxLevel = N;
    while (bfsQueue.empty() == false)
    {
        int u = bfsQueue.front();
        bfsQueue.pop();
        maxLevel = max(maxLevel, level[u]);
        for (int v : edge[u])
        {
            if (level[v] == 0)
            {
                level[v] = level[u] + 1;
                bfsQueue.push(v);
            }
        }
    }

    for (int i = 1; i <= N; i++)
    {
        if (level[i] % 3 == 1)
            chosen.push_back(i);
        if (maxLevel % 3 == 0 && level[i] == maxLevel)
            chosen.push_back(i);
    }

    cout << chosen.size() << endl;
    for (int u : chosen)
        cout << u << ' ';

    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifdef LOCAL_PROJECT
#include "debug.h"
#else
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args){}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define fora(it, c) for(const auto &it: c)
#define mem(a,b) memset(a,b,sizeof(a))
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int ni(){ int x; scanf("%d",&x);   return x; }
inline ll  nl() { ll  x; scanf("%lld",&x); return x; }

// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}
template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

vvi g;
vi vis;
vi col;
void _build(){

}

void bfs(int u){
    vis[u] = 1;
    queue<int> q;
    q.push(u);
    while(sz(q) > 0){
        int x = q.back();
        q.pop();
        if(col[x] == 0) col[x] = 2;
        for(int v : g[x]){
            if(col[x] == 2) mmax(col[v], 1);
            if(vis[v] == 0){
                vis[v]++;
                q.push(v);
            }
        }
    }
    /*
    for(int v: g[u]){
        if(vis[u] == 0) dfs(u);
        else if(vis[u] == 1){

        }else if(vis[u] == 2){

        }
    }
    vis[u] = 2;
    */

}

void _solve(){
    g.clear();
    int n,m;
    read(n,m);
    g.resize(n+1);
    forn(i,m){
        int u,v; read(u,v); //u--, v--
        g[u].pb(v);
        g[v].pb(u);
    }
    vis.assign(n+1, 0);
    col.assign(n+1, 0);
    bfs(1);
    vi ans;
    fornn(i,1,n+1) if(col[i] != 1) ans.pb(i);
    trace(n,m,g);
    trace(vis);
    trace(col);
    trace(ans);
    if(sz(ans) > n/2){
        vis.assign(n+1, 0);
        col.assign(n+1, 0);
        bfs(g[1][0]);
        ans.clear();
        fornn(i,1,n+1) if(col[i] != 1) ans.pb(i);
    }
    assert(sz(ans) > 0);
    //assert(sz(ans) <= n/2 );
    //fornn(i,1,n+1) assert(col[i] > 0);
    cout << sz(ans) << endl;
    outv(ans);

}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(1) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifdef LOCAL_PROJECT
#include "debug.h"
#else
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args){}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define fora(it, c) for(const auto &it: c)
#define mem(a,b) memset(a,b,sizeof(a))
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int ni(){ int x; scanf("%d",&x);   return x; }
inline ll  nl() { ll  x; scanf("%lld",&x); return x; }

// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}
template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

vvi g;
vi vis;
vi col;
void _build(){

}

void bfs(int u){
    vis[u] = 1;
    queue<int> q;
    q.push(u);
    while(sz(q)){
        int x = q.back();
        q.pop();
        if(col[x] == 0) col[x] = 2;
        for(int v : g[x]){
            if(col[x] == 2) mmax(col[v], 1);
            if(vis[v] == 0){
                vis[v]++;
                q.push(v);
            }
        }
    }
    /*
    for(int v: g[u]){
        if(vis[u] == 0) dfs(u);
        else if(vis[u] == 1){

        }else if(vis[u] == 2){

        }
    }
    vis[u] = 2;
    */

}

void _solve(){
    g.clear();
    int n,m;
    read(n,m);
    g.resize(n+1);
    forn(i,m){
        int u,v; read(u,v); //u--, v--
        g[u].pb(v);
        g[v].pb(u);
    }
    vis.assign(n+1, 0);
    col.assign(n+1, 0);
    bfs(1);
    vi ans;
    fornn(i,1,n+1) if(col[i] != 1) ans.pb(i);
    trace(n,m,g);
    trace(vis);
    trace(col);
    trace(ans);
    if(sz(ans) > n/2){
        vis.assign(n+1, 0);
        col.assign(n+1, 0);
        bfs(g[1][0]);
        ans.clear();
        fornn(i,1,n+1) if(col[i] != 1) ans.pb(i);
    }
    assert(sz(ans) > 0);
    assert(sz(ans) <= n/2 );
    fornn(i,1,n+1) assert(col[i] > 0);
    cout << sz(ans) << endl;
    outv(ans);

}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(1) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifdef LOCAL_PROJECT
#include "debug.h"
#else
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args){}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define fora(it, c) for(const auto &it: c)
#define mem(a,b) memset(a,b,sizeof(a))
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int ni(){ int x; scanf("%d",&x);   return x; }
inline ll  nl() { ll  x; scanf("%lld",&x); return x; }

// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}
template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

vvi g;
vi vis;
vi col;
void _build(){

}

void bfs(int u){
    vis[u] = 1;
    queue<int> q;
    q.push(u);
    while(sz(q)){
        int x = q.back();
        q.pop();
        if(col[x] == 0) col[x] = 2;
        for(int v : g[x]){
            if(col[x] == 2) mmax(col[v], 1);
            if(vis[v] == 0){
                vis[v]++;
                q.push(v);
            }
        }
    }
    /*
    for(int v: g[u]){
        if(vis[u] == 0) dfs(u);
        else if(vis[u] == 1){

        }else if(vis[u] == 2){

        }
    }
    vis[u] = 2;
    */

}

void _solve(){
    g.clear();
    int n,m;
    read(n,m);
    g.resize(n+1);
    forn(i,m){
        int u,v; read(u,v); //u--, v--
        g[u].pb(v);
        g[v].pb(u);
    }
    vis.assign(n+1, 0);
    col.assign(n+1, 0);
    bfs(1);
    vi ans;
    fornn(i,1,n+1) if(col[i] == 2) ans.pb(i);
    trace(n,m,g);
    trace(vis);
    trace(col);
    trace(ans);
    if(sz(ans) > n/2){
        vis.assign(n+1, 0);
        col.assign(n+1, 0);
        bfs(g[1][0]);
        ans.clear();
        fornn(i,1,n+1) if(col[i] == 2) ans.pb(i);
    }
    assert(sz(ans) > 0);
    assert(sz(ans) <= n/2 );
    fornn(i,1,n+1) assert(col[i] > 0);
    cout << sz(ans) << endl;
    outv(ans);

}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(1) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifdef LOCAL_PROJECT
#include "debug.h"
#else
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args){}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define fora(it, c) for(const auto &it: c)
#define mem(a,b) memset(a,b,sizeof(a))
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int ni(){ int x; scanf("%d",&x);   return x; }
inline ll  nl() { ll  x; scanf("%lld",&x); return x; }

// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}
template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

vvi g;
vi vis;
vi col;
void _build(){

}

void bfs(int u){
    vis[u] = 1;
    queue<int> q;
    q.push(u);
    while(sz(q) > 0){
        int x = q.front();
        q.pop();
        debug2(x, col[x])
        if(col[x] == 0) col[x] = 2;
        trace(x, col[x], col);
        for(int v : g[x]){
            debug2(x,v)
            if(col[x] == 2) mmax(col[v], 1);
            if(vis[v] == 0){
                vis[v]++;
                q.push(v);
                debug(v)
            }
        }
    }
}

void _solve(){
    g.clear();
    int n,m;
    read(n,m);
    //n = rand() % 10 + 2;
    //m =
    g.resize(n+1);
    forn(i,m){
        int u,v; read(u,v); //u--, v--
        g[u].pb(v);
        g[v].pb(u);
    }
    vis.assign(n+1, 0);
    col.assign(n+1, 0);
    bfs(1);
    vi ans;
    fornn(i,1,n+1) if(col[i] != 1) ans.pb(i);
    trace(n,m,g);
    trace(vis);
    trace(col);
    trace(ans);
    if(sz(ans) > n/2){
        vis.assign(n+1, 0);
        col.assign(n+1, 0);
        bfs(g[1][0]);
        ans.clear();
        fornn(i,1,n+1) if(col[i] != 1) ans.pb(i);
    }
    assert(sz(ans) > 0);
    fornn(i,1,n+1) assert(col[i] > 0);
    if(sz(ans) > n/2 ){
        set<int> bad(all(ans));
        ans.clear();
        fornn(i,1,n+1) if(!has(bad, i)) ans.pb(i);
    }
    cout << sz(ans) << endl;
    outv(ans);

}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(1) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifdef LOCAL_PROJECT
#include "debug.h"
#else
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args){}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define fora(it, c) for(const auto &it: c)
#define mem(a,b) memset(a,b,sizeof(a))
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int ni(){ int x; scanf("%d",&x);   return x; }
inline ll  nl() { ll  x; scanf("%lld",&x); return x; }

// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}
template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

vvi g;
vi vis;
vi col;
void _build(){

}

void bfs(int u){
    vis[u] = 1;
    queue<int> q;
    q.push(u);
    while(sz(q)){
        int x = q.back();
        q.pop();
        if(col[x] == 0) col[x] = 2;
        for(int v : g[x]){
            if(col[x] == 2) mmax(col[v], 1);
            if(vis[v] == 0){
                vis[v]++;
                q.push(v);
            }
        }
    }
    /*
    for(int v: g[u]){
        if(vis[u] == 0) dfs(u);
        else if(vis[u] == 1){

        }else if(vis[u] == 2){

        }
    }
    vis[u] = 2;
    */

}

void _solve(){
    g.clear();
    int n,m;
    read(n,m);
    g.resize(n+1);
    forn(i,m){
        int u,v; read(u,v); //u--, v--
        g[u].pb(v);
        g[v].pb(u);
    }
    vis.clear();
    vis.resize(n+1);
    col.clear();
    col.resize(n+1);
    bfs(1);
    vi ans;
    fornn(i,1,n+1) if(col[i] == 2) ans.pb(i);
    trace(n,m,g);
    trace(vis);
    trace(col);
    trace(ans);
    if(sz(ans) > n/2){
        vis.clear();
        vis.resize(n+1);
        col.clear();
        col.resize(n+1);
        bfs(g[1][0]);
        ans.clear();
        fornn(i,1,n+1) if(col[i] == 2) ans.pb(i);
    }
    assert(sz(ans) <= n/2 );
    cout << sz(ans) << endl;
    outv(ans);

}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(1) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifdef LOCAL_PROJECT
#include "debug.h"
#else
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args){}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define fora(it, c) for(const auto &it: c)
#define mem(a,b) memset(a,b,sizeof(a))
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int ni(){ int x; scanf("%d",&x);   return x; }
inline ll  nl() { ll  x; scanf("%lld",&x); return x; }

// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}
template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

vvi g;
vi vis;
vi col;
void _build(){

}

void bfs(int u){
    vis[u] = 1;
    queue<int> q;
    q.push(u);
    while(sz(q)){
        int x = q.back();
        q.pop();
        if(col[x] == 0) col[x] = 2;
        for(int v : g[x]){
            if(col[x] == 2) mmax(col[v], 1);
            if(vis[v] == 0){
                vis[v]++;
                q.push(v);
            }
        }
    }
    /*
    for(int v: g[u]){
        if(vis[u] == 0) dfs(u);
        else if(vis[u] == 1){

        }else if(vis[u] == 2){

        }
    }
    vis[u] = 2;
    */

}

void _solve(){
    g.clear();
    int n,m;
    read(n,m);
    g.resize(n+1);
    forn(i,m){
        int u,v; read(u,v); //u--, v--
        g[u].pb(v);
        g[v].pb(u);
    }
    vis.assign(n+1, 0);
    col.assign(n+1, 0);
    bfs(1);
    vi ans;
    fornn(i,1,n+1) if(col[i] != 2) ans.pb(i);
    trace(n,m,g);
    trace(vis);
    trace(col);
    trace(ans);
    if(sz(ans) > n/2){
        vis.assign(n+1, 0);
        col.assign(n+1, 0);
        bfs(g[1][0]);
        ans.clear();
        fornn(i,1,n+1) if(col[i] != 2) ans.pb(i);
    }
    assert(sz(ans) > 0);
    assert(sz(ans) <= n/2 );
    fornn(i,1,n+1) assert(col[i] > 0);
    cout << sz(ans) << endl;
    outv(ans);

}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(1) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/