#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<=n;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>=0&&r<n;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const int N = 1e5+5;
int n, o, z, c=1, ans;
char s[N];
vector<pair<int,int> > v;

int main() {
	scanf("%d%s",&n,s);
	for(int i=0;i<n;i++){
		if(s[i]=='0')z++;
		else o++;
		if(i%2)v.push_back({o,z});
	}
	for(int i=v.size()*2;i>0;i-=2){
		int one = v[v.size()-c].first;
		int zero = v[v.size()-c].second;
		//cout<<one << " " << zero << " "<<v.size()-c<<endl;
		if(one==zero)return printf("%d",i),0;
		for(int j=1;j+i-1<n;j++){
			if(s[j-1]=='0')zero--;
			else one--;
			if(s[j+i-1]=='0')zero++;
			else one++;
			//cout<<one<<" "<<j-1<<" "<<j+i-1<<" "<< zero<<endl;
			if(one==zero)return printf("%d",i),0;
		}
		c++;
	}
	printf("0");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const int N = 1e5+5;
int n, o, z, c, ans;
char s[N];
vector<pair<int,int> > v;

int main() {
	scanf("%d%s",&n,s);
	for(int i=0;i<n;i++){
		if(s[i]=='0')z++;
		else o++;
		if(i%2)v.push_back({o,z});
	}
	for(int i=2;i<=n;i+=2){
		int one = v[c].first;
		int zero = v[c].second;
		//cout<<one << " " << zero<<endl;
		if(one==zero)ans=i;
		for(int j=1;j+i-1<n;j++){
			if(s[j-1]=='0')zero--;
			else one--;
			if(s[j+i-1]=='0')zero++;
			else one++;
			//cout<<one<<" "<<j-1<<" "<<j+i-1<<" "<< zero<<endl;
			if(one==zero)ans=i;
		}
		c++;
	}
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2, c;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    idx1=-1;
    idx2=n;
    if(o==0)return printf("%d",n),0;
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }
    for(int i=idx1+1;i<idx2;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        one=0;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[r]=='0')one--;
            else one++;
            if(one==0)ans=max(ans,r-j+1);
            if(s[j]=='1')one++;
            else one--;
            if(one==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#define _GLIBCXX_DEBUG 1
#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<int> dp(n+1, 0);
	int ans=-1;
	vector<int> cnts(n,0);
	for (int i=1; i<=n;i++)
	{
		if (s[i-1]-'0'==0)
			dp[i] = dp[i-1]-1;
		else
			dp[i] = dp[i-1]+1;
		if (dp[i]==0)
			ans = max(ans, i);
	}
	for (int i=1; i<=n; i++)
	{
		if(n-i>ans)
		{
			for (int j=i; j<=n; j++)
			{
				if(s[i-1]-'0'==0)
					dp[j]= dp[j] + 1;
				else
					dp[j]= dp[j] - 1;

				if (dp[j]==0)
				 	ans = max(ans, j-i);
			}
		}
	}
	if (ans<0)
		cout << 0;
	else
		cout << ans;
}


#define _GLIBCXX_DEBUG 1
#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<int> dp(n+1, 0);
	int ans=-1;
	unordered_map <int, int> ump;
	ump[0] = 0;
	for (int i=1; i<=n;i++)
	{
		if (s[i-1]-'0'==0)
			dp[i] = dp[i-1]-1;
		else
			dp[i] = dp[i-1]+1;

		auto pos = ump.find(dp[i]);
		if (pos!=ump.end())
		{
			ump[dp[i]] = min(ump[dp[i]], i);
		}
		else
			ump[dp[i]] = i;
		ans = max(ans, i - ump[dp[i]]);
	}

	if (ans<0)
		cout << 0;
	else
		cout << ans;
}


#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<vector<int>> dp(n+1, vector<int> (n+1));
	int ans=INT_MIN;
	vector<int> cnts(n,0);
	for (int i=0; i<n;i++)
	{
		if (s[i]-'0'==0)
			dp[0][i] = dp[0][i-1]-1;
		else
			dp[0][i] = dp[0][i-1]+1;
		if (dp[0][i]==0)
			ans = max(ans, i+1);
	}
	for (int i=1; i<n; i++)
	{
		for (int j=i; j<n; j++)
		{
			if(s[i-1]-'0'==0)
				dp[i][j] = dp[i-1][j] + 1;
			else
				dp[i][j] = dp[i-1][j] - 1;

			if (dp[i][j]==0)
			 	ans = max(ans, j-i+1);
		}
	}
	if (ans==INT_MIN)
		cout << 0;
	else
		cout << ans;
}


#define _GLIBCXX_DEBUG 1
#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<int> dp(n+1, 0);
	int ans=-1;
	vector<int> cnts(n,0);
	for (int i=1; i<=n;i++)
	{
		if (s[i-1]-'0'==0)
			dp[i] = dp[i-1]-1;
		else
			dp[i] = dp[i-1]+1;
		if (dp[i]==0)
			ans = max(ans, i);
	}
	for (int i=1; i<=n; i++)
	{
		for (int j=i; j<=n; j++)
		{
			if(s[i-1]-'0'==0)
				dp[j]= dp[j] + 1;
			else
				dp[j]= dp[j] - 1;

			if (dp[j]==0)
			 	ans = max(ans, j-i);
		}
	}
	if (ans<0)
		cout << 0;
	else
		cout << ans;
}


#define _GLIBCXX_DEBUG 1
#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<int> dp(n+1, 0);
	int ans=-1;
	unordered_map <int, int> ump;
	for (int i=1; i<=n;i++)
	{
		if (s[i-1]-'0'==0)
			dp[i] = dp[i-1]-1;
		else
			dp[i] = dp[i-1]+1;

		auto pos = ump.find(i);
		if (pos!=ump.end())
		{
			ump[i] = min(ump[i], dp[i]);
		}
		else
			ump[i] = dp[i];

		ans = max(ans, i - ump[i]);
	}

	if (ans<0)
		cout << 0;
	else
		cout << ans;
}


#define _GLIBCXX_DEBUG 1
#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<vector<int>> dp(n+1, vector<int> (n+1));
	int ans=-1;
	vector<int> cnts(n,0);
	for (int i=1; i<=n;i++)
	{
		if (s[i-1]-'0'==0)
			dp[0][i] = dp[0][i-1]-1;
		else
			dp[0][i] = dp[0][i-1]+1;
		if (dp[0][i]==0)
			ans = max(ans, i);
	}
	for (int i=1; i<=n; i++)
	{
		for (int j=i; j<=n; j++)
		{
			if(s[i-1]-'0'==0)
				dp[i][j] = dp[i-1][j] + 1;
			else
				dp[i][j] = dp[i-1][j] - 1;

			if (dp[i][j]==0)
			 	ans = max(ans, j-i);
		}
	}
	if (ans<0)
		cout << 0;
	else
		cout << ans;
}


#define _GLIBCXX_DEBUG 1
#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<int> dp(n+1, 0);
	int ans=-1;
	unordered_map <int, int> ump;
	for (int i=1; i<=n;i++)
	{
		if (s[i-1]-'0'==0)
			dp[i] = dp[i-1]-1;
		else
			dp[i] = dp[i-1]+1;

		auto pos = ump.find(dp[i]);
		if (pos!=ump.end())
		{
			ump[dp[i]] = min(ump[dp[i]], i);
			ans = max(ans, i - ump[dp[i]]);
		}
		else
			ump[dp[i]] = i;

	}

	if (ans<0)
		cout << 0;
	else
		cout << ans;
}


#include <bits/stdc++.h>
#define mod 1e9+7;
typedef long long ll;
using namespace std;

int main()
{
	int n;cin>>n;
	string s; cin>>s;
	vector<vector<int>> dp(n+1, vector<int> (n+1));
	int ans=-1;
	vector<int> cnts(n,0);
	for (int i=0; i<n;i++)
	{
		if (s[i]-'0'==0)
			dp[0][i] = dp[0][i-1]-1;
		else
			dp[0][i] = dp[0][i-1]+1;
		if (dp[0][i]==0)
			ans = max(ans, i+1);
	}
	for (int i=1; i<n; i++)
	{
		for (int j=i; j<n; j++)
		{
			if(s[i-1]-'0'==0)
				dp[i][j] = dp[i-1][j] + 1;
			else
				dp[i][j] = dp[i-1][j] - 1;

			if (dp[i][j]==0)
			 	ans = max(ans, j-i+1);
		}
	}
	if (ans<0)
		cout << 0;
	else
		cout << ans;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int sum0=0,sum3=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='0') sum0++;
            else sum3++;
        }
        for(int i=0;i<len;i++)
        {
            sum1=sum2=0;
            for(int j=i;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
        }
        if(sum0==0 || sum3==0 ) cout<<"0"<<endl;
        else cout<<maxx<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int sum0=0,sum3=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='0') sum0++;
            else sum3++;
        }
        if(sum0==0 || sum3==0 )
        {
            cout<<"0"<<endl;
            return 0;
        }
        for(int i=0;i<len;i++)
        {
            sum1=sum2=0;
            for(int j=i;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
        }
        cout<<maxx<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int sum0=0,sum3=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='0') sum0++;
            else sum3++;
        }
        for(int i=0;i<len;i++)
        {
            sum1=sum2=0;
            for(int j=i+1;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
        }
        if(sum0==0 || sum3==0 ) cout<<"0"<<endl;
        else cout<<maxx<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            sum1=sum2=0;
            for(int j=i+1;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
        }
        if(maxx<0) cout<<"0"<<endl;
        else cout<<maxx<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
#include <cstring>
#define MAXN 1005
#define max(a,b) (a>b?a:b)
using namespace std;
int vis[110000],pre[110000];
int main()
{
   int n;
   cin>>n;
   int s=100000;
   char c;
   int ans=0;
   memset(vis,-1,sizeof(vis));
   vis[100000]=0;
   memset(pre,0,sizeof(pre));
   pre[0]=s;
   for(int i=1;i<=n;i++)
   {
       scanf("%c",&c);
       pre[i]=pre[i-1]+(c=='0'?-1:1);

       if(vis[pre[i]]!=-1)
       {
           ans=max(ans,i-vis[pre[i]]);
       }
       else vis[pre[i]]=i;
   }
   printf("%d\n",ans);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int sum0=0,sum3=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='0') sum0++;
            else sum3++;
        }
        if(sum0==0 || sum3==0 )
        {
            printf("0\n");
            return 0;
        }
        if((sum0==n/2 || sum3==n/2) && n%2==0)
        {
            printf("%d\n",n);
            return 0;
        }
        for(int i=0;i<len;i++)
            {
            sum1=sum2=0;
            for(int j=i;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
    }
    printf("%d\n",maxx);
}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int vis[110],pre[110];
int main()
{
    int ans;
    int n,maxx;
    char c;
    while(cin>>n)
    {
        ans=0;
        memset(vis,-1,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            cin>>c;
            pre[i]=pre[i-1]+(c=='0'?-1:1);
            if(vis[pre[i]]!=-1)
            {
                ans=max(ans,i-vis[pre[i]]);
            }
            else vis[pre[i]]=i;
        }
        cout<<ans<<endl;

    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int sum0=0,sum3=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='0') sum0++;
            else sum3++;
        }
        if(sum0==0 || sum3==0 )
        {
            cout<<"0"<<endl;
            return 0;
        }
        if((sum0==n/2 || sum3==n/2) && n%2==0)
        {
            cout<<n/2<<endl;
            return 0;
        }
        for(int i=0;i<len;i++)
            {
            sum1=sum2=0;
            for(int j=i;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
    }
    cout<<maxx<<endl;
}
}


#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
#include <cstring>
#define MAXN 1005
#define max(a,b) (a>b?a:b)
using namespace std;
int vis[110000],pre[110000];
int main()
{
   int n;
   cin>>n;
   int s=100000;
   char c;
   int ans=0;
   memset(vis,-1,sizeof(vis));
   vis[100000]=0;
   memset(pre,0,sizeof(pre));
   pre[0]=s;
   for(int i=1;i<=n;i++)
   {
       cin>>c;
       pre[i]=pre[i-1]+(c=='0'?-1:1);

       if(vis[pre[i]]!=-1)
       {
           ans=max(ans,i-vis[pre[i]]);
       }
       else vis[pre[i]]=i;
   }
   cout<<ans<<endl;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int sum0=0,sum3=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='0') sum0++;
            else sum3++;
        }
        if(sum0==0 || sum3==0 )
        {
            cout<<"0"<<endl;
            return 0;
        }
        if(sum0==n/2 || sum1==n/2)
        {
            cout<<n/2<<endl;
            return 0;
        }
        for(int i=0;i<len;i++)
            {
            sum1=sum2=0;
            for(int j=i;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
    }
    cout<<maxx<<endl;
}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n,maxx;
    string s;
    while(cin>>n)
    {
        maxx=-10000000;
        cin>>s;
        int sum1=0,sum2=0;
        int sum0=0,sum3=0;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='0') sum0++;
            else sum3++;
        }
        if(sum0==0 || sum3==0 )
        {
            cout<<"0"<<endl;
            return 0;
        }
        for(int i=0;i<len;i++)
        {
            sum1=sum2=0;
            for(int j=i-1;j<len;j++)
                {
                    if(s[j]=='0') sum1++;
                    else sum2++;
                    if(sum1==sum2)
                    {
                        if(2*sum1>maxx) maxx=2*sum1;
                    }
//                    cout<<maxx<<endl;
//                    cout<<sum1<<" "<<sum2<<endl;
                }
        }
        cout<<maxx<<endl;
    }
}


//#include <iostream>
//#include <cstdio>
//#include <cstring>
//#include <algorithm>
//#include <queue>
//using namespace std;
//queue<int>q1,q2;
//int main()
//{
//    int n,maxx;
//    string s;
//    while(cin>>n)
//    {
//        cin>>s;
//        for(int i=0;i<n;i++)
//        {
//            if(s[i]=='0') a[i]=-1;
//            else a[i]=1;
//        }
//        for(int i=0;i<n;i++)
//        {
//            sum+=a[i];
//            if(sum==0) ans=max(ans,i);
//
//        }
//    }
//}

#include <bits/stdc++.h>

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int MAX = 2e5 + 5;
const int ROOT = 100000;
int app[MAX];
int pref[MAX];
int n;

int main() {
	memset(app, -1, sizeof app);
	app[ROOT] = 0;
	scanf("%d%*c", &n);
	int ans = 0;
	pref[0] = ROOT;
	fori(i, 1, n + 1) {
		char c;
		scanf("%c", &c);
		pref[i] = pref[i - 1] + (c == '1' ? 1 : -1);   //表示这个数是0 1
		if(app[pref[i]] != -1) {
			ans = max(ans, i - app[pref[i]]);
		}
		else {
			app[pref[i]] = i;
		}
	}
	printf("%d\n", ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  vector<int> a[200001];
  int x,y;
  for(int i=0;i<m;i++){
    cin>>x>>y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  int visit[200001]={0};
  for(int i=1;i<200001;i++){
    for(int j=0;j<a[i].size();j++){
      if(visit[i]==0 || visit[a[i][j]]==0){
        cout<<i<<" "<<a[i][j]<<"\n";
      }
      visit[i]=1;
      visit[a[i][j]]=1;
    }
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
int main(){

  ll n,m;
  cin>>n>>m;
  vector<ll> a[200001];
  ll x,y;
  ll c[200001]={0};
  for(int i=0;i<m;i++){
    cin>>x>>y;
    a[x].push_back(y);
    a[y].push_back(x);
    c[x]++;
    c[y]++;
  }
  vector<pair<ll,ll> > v;
  for(int i=1;i<200001;i++){
    if(c[i]>0){
      v.push_back({c[i],i});
    }
  }
  sort(v.begin(),v.end(),sortinrev);

  ll visit[200001]={0};

  for(int k=0;k<v.size();k++){
     int i=v[k].second;
    for(int j=0;j<a[i].size();j++){
      if(visit[i]==0 || visit[a[i][j]]==0){
        cout<<i<<" "<<a[i][j]<<"\n";
      }
      visit[i]=1;
      visit[a[i][j]]=1;
    }
  }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  vector<int> a[200001];
  int x,y;
  int c[200001]={0};
  for(int i=0;i<m;i++){
    cin>>x>>y;
    a[x].push_back(y);
    a[y].push_back(x);
    c[x]++;
    c[y]++;
  }
  vector<pair<int,int> > v;
  for(int i=1;i<200001;i++){
    if(c[i]>0){
      v.push_back({c[i],i});
    }
  }
  sort(v.begin(),v.end());
  reverse(v.begin(),v.end());
  int visit[200001]={0};
int t=0;
  for(int k=0;k<v.size();k++){
     int i=v[k].second;
    for(int j=0;j<a[i].size();j++){
      if(visit[i]==0 || visit[a[i][j]]==0){
        cout<<i<<" "<<a[i][j]<<"\n";
        t++;
      }
      visit[i]=1;
      visit[a[i][j]]=1;
    }
  }
  if(n>100) cout<<t;
}


#include<bits/stdc++.h>
using namespace std;
vector<int> a[200001];
int c[200001]={0};
int id[200001];
int sz[200001];
int root(int x){
  while(id[x]!=x){
   x=id[x];
  }
  return x;
}
void combine(int x,int y){
   int a1=root(x);
   int a2=root(y);
   if(sz[a2]>sz[a1]){
     sz[a2]++;
     id[a1]=a2;
   }
   else{
     sz[a1]++;
     id[a2]=a1;
   }
}
void connect(int x,int y){
  if(root(x)!=root(y)){
    cout<<x<<" "<<y<<"\n";
    combine(x,y);
  }
}
void pre(){
  for(int i=1;i<200001;i++) {
    id[i]=i; sz[i]=1;
  }
}
int main(){
  pre();
  int n,m;
  cin>>n>>m;
  int x,y;
  for(int i=0;i<m;i++){
    cin>>x>>y;
    a[x].push_back(y);
    a[y].push_back(x);
    c[x]++;c[y]++;
  }
  vector<pair<int,int> > v;
  for(int i=1;i<200001;i++){
    if(c[i]) v.push_back({c[i],i});
  }
  sort(v.begin(),v.end(),greater<pair<int,int> >());
  int f=v[0].second;
  for(int i=0;i<a[f].size();i++){
    connect(f,a[f][i]);
  }
  for(int i=1;i<200001;i++){
    for(int j=0;j<a[i].size();j++){
      connect(i,a[i][j]);
    }
  }

}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

std::vector<int> adj[N];
map<int,set<int>> m;

void solve()
{
	int n,m1 ;
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	bool found[n+2] ;
	fill(found,false) ;
	int cnt = 0;
	for(auto it : m[id]){
		cout << id << " " << it << endl ;
		found[it] = true ;
		found[id] = true ;
		m[it].erase(it) ;
		cnt++ ;
	}
	rep(i,m1){
		if(cnt == n-1)
			return ;
		if(found[edges[i].ff] == false || found[edges[i].ss] == false){
			found[edges[i].ff] = found[edges[i].ss] = true ;
			cout << edges[i].ff << " " << edges[i].ss << endl ;
			cnt++ ;
		}
	}
	int i = 0 ;
	if(cnt < n-1)
		while(cnt < n-1 && i++ < m1){
		if(cnt == n-1)
			break ;
		if(found[edges[i].ff] == false || found[edges[i].ss] == false){
			found[edges[i].ff] = found[edges[i].ss] = true ;
			cout << edges[i].ff << " " << edges[i].ss << endl ;
			cnt++ ;
		}
	}
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

std::vector<int> adj[N];
map<int,set<int>> m;

void solve()
{
	int n,m1 ;
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	bool found[n+2] ;
	fill(found,false) ;
	int cnt = 0;
	for(auto it : m[id]){
		cout << id << " " << it << endl ;
		found[it] = true ;
		found[id] = true ;
		m[it].erase(it) ;
		cnt++ ;
	}
	rep(i,m1){
		if(cnt == n-1)
			break ;
		if(found[edges[i].ff] == false || found[edges[i].ss] == false){
			found[edges[i].ff] = found[edges[i].ss] = true ;
			cout << edges[i].ff << " " << edges[i].ss << endl ;
			cnt++ ;
		}
	}
	assert(cnt == n-1) ;

}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

	int cnt = 0, n , m1;
std::vector<int> adj[N];
bool found[N+2] ;
map<int,set<int>> m;

void dfs(int u){
	if(found[u] || cnt == n-1)
		return ;
	found[u] = true ;
	for(auto it : m[u]){
		if(found[it] == false){
			cout << it << " " << u << endl ;
			//found[it] = true ;
			cnt++ ;
			if(cnt == n-1)
				return ;
		}
	}
	for(auto it : m[u])
		dfs(it) ;
}
void solve()
{
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	dfs(id) ;
	//assert(cnt == n-1) ;
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

std::vector<int> adj[N];
map<int,set<int>> m;

void solve()
{
	int n,m1 ;
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg <= deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg <= deg[y])
			maxdeg = deg[y], id= y;
	}
	bool found[n+2] ;
	fill(found,false) ;
	int cnt = 0;
	for(auto it : m[id]){
		cout << id << " " << it << endl ;
		found[it] = true ;
		found[id] = true ;
		cnt++ ;
	}
	rep(i,m1){
		if(cnt == n-1)
			return ;
		if(found[edges[i].ff] == false || found[edges[i].ss] == false){
			found[edges[i].ff] = found[edges[i].ss] = true ;
			cout << edges[i].ff << " " << edges[i].ss << endl ;
			cnt++ ;
		}
	}

}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2e5+5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

int n,m1 ;
std::vector<int> m[N];
bool vis[N], ans[N] ;

void dfs(int u){
	if(vis[u])
		return ;
	vis[u] = true ;
	for(auto it : m[u]){
		if(!ans[it]){
			ans[it] |= true ;
			cout << u << " " << it << endl ;
		}
	}
	for(auto it : m[u])
		dfs(it) ;
}
void solve()
{
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].pb(y) ;
		m[y].pb(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	ans[id] = true ;
	dfs(id) ;
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

	int cnt = 0, n , m1;
std::vector<int> adj[N];
bool found[N+2] ;
map<int,set<int>> m;

void bfs(int u){
	if(found[u] == true)
		return ;
	found[u] = true ;
	stack<int> q ;
	for(auto it : m[u]){
		if(found[it] == false){
			q.push(it);
			cout << u << " " << it << endl ;
			cnt++ ;
			if(cnt == n-1)
				exit(0) ;
		}
	}
	while(q.size()){
		bfs(q.top()), q.pop();
	}
}
void solve()
{
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	bfs(id) ;
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

	int cnt = 0, n , m1;
std::vector<int> adj[N];
bool found[N+2] ;
map<int,set<int>> m;

void dfs(int u){
	if(found[u])
		return ;
	found[u] = true ;
	for(auto it : m[u]){
		if(found[it] == false){
			cout << it << " " << u << endl ;
			//found[it] = true ;
		}
	}
	for(auto it : m[u])
		dfs(it) ;
}
void solve()
{
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	dfs(id) ;
	//assert(cnt == n-1) ;
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

	int cnt = 0, n , m1;
std::vector<int> adj[N];
bool found[N+2], answermein[N+2] ;
map<int,set<int>> m;

void dfs(int u){

	if(found[u] || cnt == n-1)
		return ;
	found[u] = true ;
	for(auto it : m[u]){
		if(answermein[it] == false){
			cout << it << " " << u << endl ;
			answermein[it] = true ;
			cnt++ ;
			if(cnt == n-1)
				return ;
		}
	}
	for(auto it : m[u]){
		dfs(it) ;
	}
}
void solve()
{
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	dfs(id) ;
	//assert(cnt == n-1) ;
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2505 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

int n,m1 ;
std::vector<int> m[N];
bool vis[N], ans[N] ;

void dfs(int u){
	if(vis[u])
		return ;
	vis[u] = true ;
	for(auto it : m[u]){
		if(!ans[it]){
			ans[it] |= true ;
			cout << u << " " << it << endl ;
		}
	}
	for(auto it : m[u])
		dfs(it) ;
}
void solve()
{
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].pb(y) ;
		m[y].pb(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	ans[id] = true ;
	dfs(id) ;
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

std::vector<int> adj[N];
bool connected[N], vis[N] ;
void dfs(int u){
	if(vis[u])
		return ;
	vis[u] = true ;
	for(auto it : adj[u]){
		if(!vis[it] && //not yet visited. FOR my traversal
				!connected[it]){ //not reachable yet. (DSU)
		// Now pakka has to be added.
			cout << it << " " << u  << endl ;
			connected[it] = true ;
		}
	}
	for(auto it : adj[u])
		dfs(it) ;
}
void solve()
{
	int n,m ;
	cin >> n >> m ;
	rep(i,m){
		int x,y ;
		cin >> x >> y ;
		adj[x].pb(y) ;
		adj[y].pb(x) ;
	}
	int root_idx = 1 ;
	repA(i,1,n)
		if((int)adj[root_idx].size() < (int)adj[i].size())
			root_idx = i ;
	dfs(root_idx) ;
}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5+ 5 ;
const int mod = 1e9 + 5 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

std::vector<int> adj[N];
map<int,set<int>> m;

void solve()
{
	int n,m1 ;
	cin >> n >> m1;
	vector<pii> edges ;
	int deg[n+1] ;
	fill(deg,0);
	int id = 1, maxdeg = -inf ;
	rep(i,m1){
		int x,y ;
		cin >> x >> y ;
		deg[x]++, deg[y]++ ;
		edges.pb({x,y}) ;
		m[x].insert(y) ;
		m[y].insert(x) ;
		if(maxdeg < deg[x])
			maxdeg = deg[x], id= x;
		if(maxdeg < deg[y])
			maxdeg = deg[y], id= y;
	}
	bool found[n+2] ;
	fill(found,false) ;
	int cnt = 0;
	for(auto it : m[id]){
		cout << id << " " << it << endl ;
		found[it] = true ;
		found[id] = true ;
		m[it].erase(it) ;
		cnt++ ;
	}
	rep(i,m1){
		if(cnt == n-1)
			return ;
		if(found[edges[i].ff] == false || found[edges[i].ss] == false){
			found[edges[i].ff] = found[edges[i].ss] = true ;
			cout << edges[i].ff << " " << edges[i].ss << endl ;
			cnt++ ;
		}
	}

}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adjlist;
vector<pair<int, int>> aristas; //arbol_de_expansion
vector<bool> visited;
void bfs(int s)
{
    queue<int> q;
    q.push(s);
    visited[s] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < adjlist[u].size(); i++)
        {
            int v = adjlist[u][i];
            if (!visited[v])
            {

                q.push(v);
                visited[v] = true;
                aristas.push_back(make_pair(u, v));
            }
        }
    }
}
using namespace std;
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    adjlist.resize(n + 1);
    int u, v;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d", &u, &v);
        adjlist[u].push_back(v);
        adjlist[v].push_back(u);
    }
    int max_grado = 0;
    int max_v = 0;
    for (int v = 1; v < n; v++)
    {
        if (max_grado < adjlist[v].size())
        {
            max_grado = adjlist[v].size();
            max_v = v;
        }
    }
    visited.resize(n + 1);
    bfs(v);
    for (int i = 0; i < aristas.size(); i++)
    {
        printf("%d %d\n", aristas[i].first, aristas[i].second);
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adjlist;
vector<pair<int, int>> aristas; //arbol_de_expansion
vector<bool> visited;
void bfs(int s)
{
    queue<int> q;
    q.push(s);
    visited[s] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i <= adjlist[u].size(); i++)
        {
            int v = adjlist[u][i];
            if (!visited[v])
            {

                q.push(v);
                visited[v] = true;
                aristas.push_back(make_pair(u, v));
            }
        }
    }
}
using namespace std;
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    adjlist.resize(n + 1);
    int u, v;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d", &u, &v);
        adjlist[u].push_back(v);
        adjlist[v].push_back(u);
    }
    int max_grado = 0;
    int max_v = 0;
    for (int v = 1; v < n; v++)
    {
        if (max_grado < adjlist[v].size())
        {
            max_grado = adjlist[v].size();
            max_v = v;
        }
    }
    visited.resize(n + 1);
    bfs(v);
    for (int i = 0; i < aristas.size(); i++)
    {
        printf("%d %d\n", aristas[i].first, aristas[i].second);
    }

    return 0;
}


#include<iostream>
#include<vector>

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

vector<vector<int>> adjlist;
vector<pair<int,int>> spanning_tree;
vector<bool> visited;
void bfs(int s){
    queue<int> q;
    q.push(s);
    visited[s] = true;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = 0; i < adjlist[u].size();i++){
            int v = adjlist[u][i];
            if(!visited[v]){
                visited[v] = true;
                spanning_tree.push_back(make_pair(u,v));
                q.push(v);
            }
        }
    }

}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    adjlist.resize(n+1);
    int u,v;
    for(int i = 0; i < m;i++){
        scanf("%d %d",&u,&v);
        adjlist[u].push_back(v);
        adjlist[v].push_back(u);
    }
    int max_degree = 0;
    int max_vertex = 0;
    for(int i = 1; i <= n; i++){
        if(adjlist[i].size()> max_degree){
            max_degree = adjlist[i].size();
            max_vertex = i;
        }
    }

    visited.resize(n+1);
    for(int i = 1;i <= n; i++)
        visited[i] = false;
    bfs(max_vertex);
    for(int i = 0; i < spanning_tree.size();i++){
        printf("%d %d\n",spanning_tree[i].first,spanning_tree[i].second);
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adjlist;
vector<pair<int, int>> aristas; //arbol_de_expansion
vector<bool> visited;
void bfs(int s)
{
    queue<int> q;
    q.push(s);
    visited[s] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < adjlist[u].size(); i++)
        {
            int v = adjlist[u][i];
            if (!visited[v])
            {

                q.push(v);
                visited[v] = true;
                aristas.push_back(make_pair(u, v));
            }
        }
    }
}
using namespace std;
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    adjlist.resize(n + 1);
    int u, v;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d", &u, &v);
        adjlist[u].push_back(v);
        adjlist[v].push_back(u);
    }
    int max_grado = 0;
    int max_v = 0;
    for (int v = 1; v <= n; v++)
    {
        if (max_grado < adjlist[v].size())
        {
            max_grado = adjlist[v].size();
            max_v = v;
        }
    }
    visited.resize(n + 1);
    bfs(max_v);
    for (int i = 0; i < aristas.size(); i++)
    {
        printf("%d %d\n", aristas[i].first, aristas[i].second);
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adjlist;
vector<pair<int, int>> aristas; //arbol_de_expansion
vector<bool> visited;
void bfs(int s)
{
    queue<int> q;
    q.push(s);
    visited[s] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < adjlist[u].size(); i++)
        {
            int v = adjlist[u][i];
            if (!visited[v])
            {

                q.push(v);
                visited[v] = true;
                aristas.push_back(make_pair(u, v));
            }
        }
    }
}
using namespace std;
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    adjlist.resize(n + 1);
    int u, v;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d", &u, &v);
        adjlist[u].push_back(v);
        adjlist[v].push_back(u);
    }
    int max_grado = 0;
    int max_v = 0;
    for (int v = 1; v <= n; v++)
    {
        if (max_grado < adjlist[v].size())
        {
            max_grado = adjlist[v].size();
            max_v = v;
        }
    }
    visited.resize(n + 1);
    bfs(v);
    for (int i = 0; i < aristas.size(); i++)
    {
        printf("%d %d\n", aristas[i].first, aristas[i].second);
    }

    return 0;
}


/*
　　　  ∧＿∧　Hi.
　　 （´・ω・｀)　　　　　/　　　　Welcome to Bourbon House. This tequila is on the house.
　　 ／∇y:::::＼　　　 [￣]        First, I'd like you to drink this and calm down.
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|  I'm sorry to bother you over and over.
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　    It's said, "to try the patience of a saint", so
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|          I don't expect that you forgive me.
　　　　∇　∇　∇　∇　　　／.／|
　　　　┴　┴　┴　┴　／ ／　  |　　　However, when you saw this submittion, you felt that
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　  it was funny, I think. In the tense atmosphere of the contest,
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　    I'd like you to have a sense of humor. That's why I posted this submittion.
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 So, may I take your order?
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
// const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

// Graph Templates
using Weight = lint;
// using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    // Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
const Weight INF = numeric_limits<Weight>::max() / 8;

// add edge a <--(weight = w)--> b
void add_edge(Graph &g, int a, int b, Weight w) {
    g[a].push_back(Edge(a, b, w));
    g[b].push_back(Edge(b, a, w));
}

// add edge a --(weight = w)--> b
void add_arc(Graph &g, int a, int b, Weight w) { g[a].push_back(Edge(a, b, w)); }

struct uf_tree {
    std::vector<int> parent;
    int __size;
    uf_tree(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

// Kruskal
pair<Weight, Edges> kruskal(Graph const &g) {
    uf_tree uf(g.size());
    Edges es;
    for (auto &adj : g)
        for (auto &e : adj) es.emplace_back(e);
    sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; });
    Weight total = 0;
    Edges T;
    for (auto &e : es)
        if (!uf.is_same(e.src, e.dst)) {
            T.push_back(e);
            total += e.weight;
            uf.unite(e.src, e.dst);
        }
    return make_pair(total, T);
}

int main(){
    int n, m, s, t;
    cin >> n >> m;
    Graph g;
    g.resize(n);
    for (int i=0; i<m; ++i){
        cin >> s >> t;
        s--;
        t--;
        add_edge(g, s, t, 1);
    }
    int max_d = 0;
    for (int i=0; i<n; ++i){
        max_d = max(max_d, (int)g[i].size());
    }
    //debug(max_d);
    for (int i=0; i<n; ++i){
        if((int)g[i].size() == max_d){
            continue;
        }
        else {
            for (int j=0; j<(int)g[i].size(); ++j){
                g[i][j].weight = (lint)(n + 1);
            }
        }
    }
    pair<Weight, Edges> ans = kruskal(g);
    for (int i=0; i<n-1; ++i){
        cout << ans.snd[i].src + 1 << " " << ans.snd[i].dst + 1 << endl;
    }
    return 0;
}


/*
　　　  ∧＿∧　Hi.
　　 （´・ω・｀)　　　　　/　　　　Welcome to Bourbon House. This tequila is on the house.
　　 ／∇y:::::＼　　　 [￣]        First, I'd like you to drink this and calm down.
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|  I'm sorry to bother you over and over.
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　    It's said, "to try the patience of a saint", so
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|          I don't expect that you forgive me.
　　　　∇　∇　∇　∇　　　／.／|
　　　　┴　┴　┴　┴　／ ／　  |　　　However, when you saw this submittion, you felt that
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　  it was funny, I think. In the tense atmosphere of the contest,
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　    I'd like you to have a sense of humor. That's why I posted this submittion.
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 So, may I take your order?
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
// const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

// Graph Templates
using Weight = lint;
// using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    // Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
const Weight INF = numeric_limits<Weight>::max() / 8;

// add edge a <--(weight = w)--> b
void add_edge(Graph &g, int a, int b, Weight w) {
    g[a].push_back(Edge(a, b, w));
    g[b].push_back(Edge(b, a, w));
}

// add edge a --(weight = w)--> b
void add_arc(Graph &g, int a, int b, Weight w) { g[a].push_back(Edge(a, b, w)); }

struct uf_tree {
    std::vector<int> parent;
    int __size;
    uf_tree(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

// Kruskal
pair<Weight, Edges> kruskal(Graph const &g) {
    uf_tree uf(g.size());
    Edges es;
    for (auto &adj : g)
        for (auto &e : adj) es.emplace_back(e);
    sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; });
    Weight total = 0;
    Edges T;
    for (auto &e : es)
        if (!uf.is_same(e.src, e.dst)) {
            T.push_back(e);
            total += e.weight;
            uf.unite(e.src, e.dst);
        }
    return make_pair(total, T);
}

int main(){
    int n, m, s, t;
    cin >> n >> m;
    Graph g;
    g.resize(n);
    for (int i=0; i<m; ++i){
        cin >> s >> t;
        s--;
        t--;
        add_edge(g, s, t, 1);
    }
    int max_d = 0;
    for (int i=0; i<n; ++i){
        max_d = max(max_d, (int)g[i].size());
    }
    //debug(max_d);
    for (int i=0; i<n; ++i){
        if((int)g[i].size() == max_d){
            continue;
        }
        else {
            for (int j=0; j<(int)g[i].size(); ++j){
                g[i][j].weight = 2;
            }
        }
    }
    pair<Weight, Edges> ans = kruskal(g);
    for (int i=0; i<n-1; ++i){
        cout << ans.snd[i].src + 1 << " " << ans.snd[i].dst + 1 << endl;
    }
    return 0;
}


/*
　　　  ∧＿∧　Hi.
　　 （´・ω・｀)　　　　　/　　　　Welcome to Bourbon House. This tequila is on the house.
　　 ／∇y:::::＼　　　 [￣]        First, I'd like you to drink this and calm down.
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|  I'm sorry to bother you over and over.
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　    It's said, "to try the patience of a saint", so
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|          I don't expect that you forgive me.
　　　　∇　∇　∇　∇　　　／.／|
　　　　┴　┴　┴　┴　／ ／　  |　　　However, when you saw this submittion, you felt that
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　  it was funny, I think. In the tense atmosphere of the contest,
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　    I'd like you to have a sense of humor. That's why I posted this submittion.
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 So, may I take your order?
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
// const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

// Graph Templates
using Weight = lint;
// using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    // Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
const Weight INF = numeric_limits<Weight>::max() / 8;

// add edge a <--(weight = w)--> b
void add_edge(Graph &g, int a, int b, Weight w) {
    g[a].push_back(Edge(a, b, w));
    g[b].push_back(Edge(b, a, w));
}

// add edge a --(weight = w)--> b
void add_arc(Graph &g, int a, int b, Weight w) { g[a].push_back(Edge(a, b, w)); }

struct uf_tree {
    std::vector<int> parent;
    int __size;
    uf_tree(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

// Kruskal
pair<Weight, Edges> kruskal(Graph const &g) {
    uf_tree uf(g.size());
    Edges es;
    for (auto &adj : g)
        for (auto &e : adj) es.emplace_back(e);
    sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; });
    Weight total = 0;
    Edges T;
    for (auto &e : es)
        if (!uf.is_same(e.src, e.dst)) {
            T.push_back(e);
            total += e.weight;
            uf.unite(e.src, e.dst);
        }
    return make_pair(total, T);
}

int main(){
    int n, m, s, t;
    cin >> n >> m;
    Graph g;
    vector<pair<int, int>> edges(m);
    int counter[n];
    for (int i=0; i<n; ++i){
        counter[i] = 0;
    }
    g.resize(n);
    for (int i=0; i<m; ++i){
        cin >> s >> t;
        s--;
        t--;
        edges[i].fst = s;
        edges[i].snd = t;
        counter[s]++;
        counter[t]++;
    }
    int max_d = 0;
    for (int i=0; i<n; ++i){
        max_d = max(max_d, counter[i]);
    }
    set<int> max_d_v;
    for (int i=0; i<n; ++i){
        if(max_d == counter[i]){
            max_d_v.insert(i);
        }
    }
    //debug(max_d);
    for (int i=0; i<m; ++i){
        s = edges[i].fst;
        t = edges[i].snd;
        if(max_d_v.find(s) == max_d_v.end() && max_d_v.find(t) == max_d_v.end()){
            add_edge(g, s, t, m);
        }
        else {
            add_edge(g, s, t, 1);
        }
    }
    pair<Weight, Edges> ans = kruskal(g);
    for (int i=0; i<n-1; ++i){
        cout << ans.snd[i].src + 1 << " " << ans.snd[i].dst + 1 << endl;
    }
    return 0;
}


/*
　　　  ∧＿∧　Hi.
　　 （´・ω・｀)　　　　　/　　　　Welcome to Bourbon House. This tequila is on the house.
　　 ／∇y:::::＼　　　 [￣]        First, I'd like you to drink this and calm down.
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|  I'm sorry to bother you over and over.
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　    It's said, "to try the patience of a saint", so
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|          I don't expect that you forgive me.
　　　　∇　∇　∇　∇　　　／.／|
　　　　┴　┴　┴　┴　／ ／　  |　　　However, when you saw this submittion, you felt that
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　  it was funny, I think. In the tense atmosphere of the contest,
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　    I'd like you to have a sense of humor. That's why I posted this submittion.
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 So, may I take your order?
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
// const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

// Graph Templates
using Weight = lint;
// using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    // Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
const Weight INF = numeric_limits<Weight>::max() / 8;

// add edge a <--(weight = w)--> b
void add_edge(Graph &g, int a, int b, Weight w) {
    g[a].push_back(Edge(a, b, w));
    g[b].push_back(Edge(b, a, w));
}

// add edge a --(weight = w)--> b
void add_arc(Graph &g, int a, int b, Weight w) { g[a].push_back(Edge(a, b, w)); }

template<class Abel> struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    // n -> number of nodes
    // enherit -> enherit of Abel group
    UnionFind(int n, Abel enherit = 0) {
        init(n, enherit);
    }

    void init(int n, Abel enherit = 0) {
        par.resize(n);
        rank.resize(n);
        diff_weight.resize(n);
        for (int i = 0; i < n; ++i){
            par[i] = i;
            rank[i] = 0;
            diff_weight[i] = enherit;
        }
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x) {
        root(x);
        return diff_weight[x];
    }

    bool is_same(int x, int y) {
        return root(x) == root(y);
    }

    bool unite_set(int x, int y, Abel w) {
        w += weight(x);
        w -= weight(y);
        x = root(x);
        y = root(y);
        if (x == y){
            return false;
        }
        if (rank[x] < rank[y]){
            swap(x, y);
            w = -w;
        }
        if (rank[x] == rank[y]){
            ++rank[x];
        }
        par[y] = x;
        diff_weight[y] = w;
        return true;
    }

    Abel diff(int x, int y) {
        return weight(y) - weight(x);
    }
};

int main(){
    int n, m, s, t;
    cin >> n >> m;
    Graph g;
    set<pair<int, int>> ans;
    int counter[n];
    UnionFind<int> uf(n);
    priority_queue<pair<int, int>> que;
    for (int i=0; i<n; ++i){
        counter[i] = 0;
    }
    g.resize(n);
    for (int i=0; i<m; ++i){
        cin >> s >> t;
        s--;
        t--;
        counter[s]++;
        counter[t]++;
        add_edge(g, s, t, 1);
    }
    for (int i=0; i<n; ++i){
        que.push(make_pair(counter[i], i));
    }
    while(!que.empty() && ans.size() < n-1){
        auto p = que.top();
        que.pop();
        //debugpair(p.fst, p.snd + 1);
        for (int i=0; i<(int)g[p.snd].size(); ++i){
            if(!uf.is_same(g[p.snd][i].src, g[p.snd][i].dst)){
                uf.unite_set(g[p.snd][i].src, g[p.snd][i].dst, 1);
                ans.insert(make_pair(g[p.snd][i].src, g[p.snd][i].dst));
            }
        }
    }
    for (auto itr=ans.begin(); itr != ans.end(); ++itr){
        auto p = *itr;
        cout << p.fst + 1 << " " << p.snd + 1 << endl;
    }
    return 0;
}


/*
　　　  ∧＿∧　Hi.
　　 （´・ω・｀)　　　　　/　　　　Welcome to Bourbon House. This tequila is on the house.
　　 ／∇y:::::＼　　　 [￣]        First, I'd like you to drink this and calm down.
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|  I'm sorry to bother you over and over.
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　    It's said, "to try the patience of a saint", so
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|          I don't expect that you forgive me.
　　　　∇　∇　∇　∇　　　／.／|
　　　　┴　┴　┴　┴　／ ／　  |　　　However, when you saw this submittion, you felt that
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　  it was funny, I think. In the tense atmosphere of the contest,
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　    I'd like you to have a sense of humor. That's why I posted this submittion.
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 So, may I take your order?
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
// const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

// Graph Templates
using Weight = lint;
// using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    // Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
const Weight INF = numeric_limits<Weight>::max() / 8;

// add edge a <--(weight = w)--> b
void add_edge(Graph &g, int a, int b, Weight w) {
    g[a].push_back(Edge(a, b, w));
    g[b].push_back(Edge(b, a, w));
}

// add edge a --(weight = w)--> b
void add_arc(Graph &g, int a, int b, Weight w) { g[a].push_back(Edge(a, b, w)); }

struct uf_tree {
    std::vector<int> parent;
    int __size;
    uf_tree(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

// Kruskal
pair<Weight, Edges> kruskal(Graph const &g) {
    uf_tree uf(g.size());
    Edges es;
    for (auto &adj : g)
        for (auto &e : adj) es.emplace_back(e);
    sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; });
    Weight total = 0;
    Edges T;
    for (auto &e : es)
        if (!uf.is_same(e.src, e.dst)) {
            T.push_back(e);
            total += e.weight;
            uf.unite(e.src, e.dst);
        }
    return make_pair(total, T);
}

int main(){
    int n, m, s, t;
    cin >> n >> m;
    Graph g;
    vector<pair<int, int>> edges(m);
    multiset<int> d_count;
    g.resize(n);
    for (int i=0; i<m; ++i){
        cin >> s >> t;
        s--;
        t--;
        edges[i].fst = s;
        edges[i].snd = t;
        d_count.insert(s);
        d_count.insert(t);
    }
    int max_d = 0;
    for (int i=0; i<n; ++i){
        max_d = max(max_d, (int)d_count.count(i));
    }
    set<int> max_d_v;
    for (int i=0; i<n; ++i){
        if(max_d == d_count.count(i)){
            max_d_v.insert(i);
            debug(i);
        }
    }
    debug(max_d);
    for (int i=0; i<m; ++i){
        s = edges[i].fst;
        t = edges[i].snd;
        if(max_d_v.find(s) == max_d_v.end() && max_d_v.find(t) == max_d_v.end()){
            add_edge(g, s, t, m);
        }
        else {
            add_edge(g, s, t, 1);
        }
    }
    pair<Weight, Edges> ans = kruskal(g);
    for (int i=0; i<n-1; ++i){
        cout << ans.snd[i].src + 1 << " " << ans.snd[i].dst + 1 << endl;
    }
    return 0;
}


/*
　　　  ∧＿∧　Hi.
　　 （´・ω・｀)　　　　　/　　　　Welcome to Bourbon House. This tequila is on the house.
　　 ／∇y:::::＼　　　 [￣]        First, I'd like you to drink this and calm down.
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|  I'm sorry to bother you over and over.
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　    It's said, "to try the patience of a saint", so
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|          I don't expect that you forgive me.
　　　　∇　∇　∇　∇　　　／.／|
　　　　┴　┴　┴　┴　／ ／　  |　　　However, when you saw this submittion, you felt that
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　  it was funny, I think. In the tense atmosphere of the contest,
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　    I'd like you to have a sense of humor. That's why I posted this submittion.
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 So, may I take your order?
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
// const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

// Graph Templates
using Weight = lint;
// using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    // Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
const Weight INF = numeric_limits<Weight>::max() / 8;

// add edge a <--(weight = w)--> b
void add_edge(Graph &g, int a, int b, Weight w) {
    g[a].push_back(Edge(a, b, w));
    g[b].push_back(Edge(b, a, w));
}

// add edge a --(weight = w)--> b
void add_arc(Graph &g, int a, int b, Weight w) { g[a].push_back(Edge(a, b, w)); }

struct uf_tree {
    std::vector<int> parent;
    int __size;
    uf_tree(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

// Kruskal
pair<Weight, Edges> kruskal(Graph const &g) {
    uf_tree uf(g.size());
    Edges es;
    for (auto &adj : g)
        for (auto &e : adj) es.emplace_back(e);
    sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; });
    Weight total = 0;
    Edges T;
    for (auto &e : es)
        if (!uf.is_same(e.src, e.dst)) {
            T.push_back(e);
            total += e.weight;
            uf.unite(e.src, e.dst);
        }
    return make_pair(total, T);
}

int main(){
    int n, m, s, t;
    cin >> n >> m;
    Graph g;
    g.resize(n);
    for (int i=0; i<m; ++i){
        cin >> s >> t;
        s--;
        t--;
        add_edge(g, s, t, 1);
    }
    int max_d = 0;
    for (int i=0; i<n; ++i){
        max_d = max(max_d, (int)g[i].size());
    }
    //debug(max_d);
    for (int i=0; i<n; ++i){
        if((int)g[i].size() == max_d){
            continue;
        }
        else {
            for (int j=0; j<(int)g[i].size(); ++j){
                g[i][j].weight = (lint)(m - max_d);
            }
        }
    }
    pair<Weight, Edges> ans = kruskal(g);
    for (int i=0; i<n-1; ++i){
        cout << ans.snd[i].src + 1 << " " << ans.snd[i].dst + 1 << endl;
    }
    return 0;
}


#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std;

inline int read()
{
	int n = 0, ch = getchar();
	for (; !isdigit(ch); ch = getchar());
	for (; isdigit(ch); ch = getchar())
		n = (n << 3) + (n << 1) + (ch ^ 48);
	return n;
}

int DEG[200005], p[200005];

struct EDGE
{
	int u, v;
	bool operator < (const EDGE &rhs) const {
		return max(DEG[u], DEG[v]) > max(DEG[rhs.u], DEG[rhs.v]);
	}
};

EDGE G[200005];

int find(int x) { return ((p[x] == x) ? x : (p[x] = find(p[x]))); }

int main()
{
	int n = read();
	int m = read();
	for (int i = 0; i < m; ++i)
	{
		++DEG[G[i].u = read()];
		++DEG[G[i].v = read()];
		p[i] = i;
	}

	sort(G, G + m);
	int found = 1;
	for (int i = 0; found < n; ++i)
	{
		if (find(G[i].u) == find(G[i].v))
			continue;
		printf("%d %d\n", G[i].u, G[i].v);
		++found;
		p[G[i].u] = find(G[i].v);
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std;

inline int read()
{
	int n = 0, ch = getchar();
	for (; !isdigit(ch); ch = getchar());
	for (; isdigit(ch); ch = getchar())
		n = (n << 3) + (n << 1) + (ch ^ 48);
	return n;
}

int DEG[200005], p[200005];

struct EDGE
{
	int u, v;
	bool operator < (const EDGE &rhs) const {
		if (max(DEG[u], DEG[v]) != max(DEG[rhs.u], DEG[rhs.v]))
			return max(DEG[u], DEG[v]) > max(DEG[rhs.u], DEG[rhs.v]);
		return min(u, v) < min(rhs.u, rhs.v);
	}
};

EDGE G[200005];

int find(int x) { return ((p[x] == x) ? x : (p[x] = find(p[x]))); }

int main()
{
	int n = read();
	int m = read();
	for (int i = 0; i < m; ++i)
	{
		++DEG[G[i].u = read()];
		++DEG[G[i].v = read()];
		p[i] = i;
	}

	sort(G, G + m);
	int found = 1;
	for (int i = 0; found < n; ++i)
	{
		int x = find(G[i].u);
		int y = find(G[i].v);
		if (x != y)
		{
			++found;
			printf("%d %d\n", G[i].u, G[i].v);
			p[x] = y;
		}
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std;

inline int read()
{
	int n = 0, ch = getchar();
	for (; !isdigit(ch); ch = getchar());
	for (; isdigit(ch); ch = getchar())
		n = (n << 3) + (n << 1) + (ch ^ 48);
	return n;
}

int DEG[200005], p[200005];

struct EDGE
{
	int u, v;
	bool operator < (const EDGE &rhs) const {
		return max(DEG[u], DEG[v]) > max(DEG[rhs.u], DEG[rhs.v]);
	}
};

EDGE G[200005];

int find(int x) { return ((p[x] == x) ? x : (p[x] = find(p[x]))); }

int main()
{
	int n = read();
	int m = read();
	for (int i = 0; i < m; ++i)
	{
		++DEG[G[i].u = read()];
		++DEG[G[i].v = read()];
		p[i] = i;
	}

	if (n == 632 && m == 199396)
	{
		printf("DEG[1] = %d\n", DEG[1]);
		printf("DEG[331] = %d\n", DEG[331]);
		printf("DEG[321] = %d\n", DEG[321]);
		printf("DEG[443] = %d\n", DEG[443]);
	}
	sort(G, G + m);
	int found = 1;
	for (int i = 0; found < n; ++i)
	{
		int x = find(G[i].u);
		int y = find(G[i].v);
		if (x != y)
		{
			++found;
			printf("%d %d\n", G[i].u, G[i].v);
			p[x] = y;
		}
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std;

inline int read()
{
	int n = 0, ch = getchar();
	for (; !isdigit(ch); ch = getchar());
	for (; isdigit(ch); ch = getchar())
		n = (n << 3) + (n << 1) + (ch ^ 48);
	return n;
}

int DEG[200005], p[200005];

struct EDGE
{
	int u, v;
	bool operator < (const EDGE &rhs) const {
		return max(DEG[u], DEG[v]) > max(DEG[rhs.u], DEG[rhs.v]);
	}
};

EDGE G[200005];

int find(int x) { return ((p[x] == x) ? x : (p[x] = find(p[x]))); }

int main()
{
	int n = read();
	int m = read();
	for (int i = 0; i < m; ++i)
	{
		++DEG[G[i].u = read()];
		++DEG[G[i].v = read()];
		p[i] = i;
	}

	sort(G, G + m);
	int found = 1;
	for (int i = 0; found < n; ++i)
	{
		int x = find(G[i].u);
		int y = find(G[i].v);
		if (x != y)
		{
			++found;
			printf("%d %d\n", G[i].u, G[i].v);
			p[x] = y;
		}
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std;

inline int read()
{
	int n = 0, ch = getchar();
	for (; !isdigit(ch); ch = getchar());
	for (; isdigit(ch); ch = getchar())
		n = (n << 3) + (n << 1) + (ch ^ 48);
	return n;
}

int DEG[2000005], p[2000005];

struct EDGE
{
	int u, v;
	bool operator < (const EDGE &rhs) const {
		if (max(DEG[u], DEG[v]) != max(DEG[rhs.u], DEG[rhs.v]))
			return max(DEG[u], DEG[v]) > max(DEG[rhs.u], DEG[rhs.v]);
		return min(u, v) < min(rhs.u, rhs.v);
	}
};

EDGE G[2000005];

int find(int x) { return ((p[x] == x) ? x : (p[x] = find(p[x]))); }

int main()
{
	int n = read();
	int m = read();
	for (int i = 0; i < m; ++i)
	{
		++DEG[G[i].u = read()];
		++DEG[G[i].v = read()];
		p[i] = i;
	}

	sort(G, G + m);
	int found = 1;
	for (int i = 0; found < n; ++i)
	{
		int x = find(G[i].u);
		int y = find(G[i].v);
		if (x != y)
		{
			++found;
			printf("%d %d\n", G[i].u, G[i].v);
			p[x] = y;
		}
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std;

inline int read()
{
	int n = 0, ch = getchar();
	for (; !isdigit(ch); ch = getchar());
	for (; isdigit(ch); ch = getchar())
		n = (n << 3) + (n << 1) + (ch ^ 48);
	return n;
}

int DEG[2000005], p[2000005];

struct EDGE
{
	int u, v;
	bool operator < (const EDGE &rhs) const {
		if (max(DEG[u], DEG[v]) != max(DEG[rhs.u], DEG[rhs.v]))
			return max(DEG[u], DEG[v]) > max(DEG[rhs.u], DEG[rhs.v]);
		return min(u, v) < min(rhs.u, rhs.v);
	}
};

EDGE G[2000005];

int find(int x) { return ((p[x] == x) ? x : (p[x] = find(p[x]))); }

int main()
{
	int n = read();
	int m = read();

	// n = m + 1

	if (n == m + 1)
	{
		for (int i = 0; i < m; ++i)
			printf("%d %d\n", read(), read());
		return 0;
	}

	// END n = m + 1

	for (int i = 0; i < m; ++i)
	{
		++DEG[G[i].u = read()];
		++DEG[G[i].v = read()];
		p[i] = i;
	}

	sort(G, G + m);
	int found = 1;
	for (int i = 0; found < n; ++i)
	{
		int x = find(G[i].u);
		int y = find(G[i].v);
		if (x != y)
		{
			++found;
			printf("%d %d\n", G[i].u, G[i].v);
			p[x] = y;
		}
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

using Graph = vector<vector<int>>;

class DSU {
    vector<int> leaders;

public:
    DSU(int n) {
        leaders.resize(n);
        iota(leaders.begin(), leaders.end(), 0);
    }
    int find(int q) {
        if (q == leaders[q]) return q;
        return leaders[q] = find(leaders[q]);
    }
    bool join(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            return false;
        }
        if (rand() & 1) {
            swap(u, v);
        }
        leaders[u] = v;
        return true;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    DSU d(n);
    vector<vector<pair<int,int>>> gr(n);
    for (int i = 0, u, v; i < m; ++i) {
        cin >> u >> v;
        --u;
        --v;
        gr[u].push_back(pair(u, v));
        gr[v].push_back(pair(v, u));
    }
    sort(gr.begin(), gr.end(), [] (const vector<pair<int, int>>& lhs, const vector<pair<int, int>>& rhs){
        return lhs.size() > rhs.size();
    });
    for (auto& it: gr) {
        for (auto [u, v] : it) {
            if (d.join(u, v)) {
                cout << u + 1 << ' ' << v + 1 << '\n';
            }
        }
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

using Graph = vector<vector<int>>;

class DSU {
    vector<int> leaders;

public:
    DSU(int n) {
        leaders.resize(n);
        iota(leaders.begin(), leaders.end(), 0);
    }
    int find(int q) {
        if (q == leaders[q])
            return q;
        return leaders[q] = find(leaders[q]);
    }
    bool join(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            return false;
        }
        if (rand() & 1) {
            swap(u, v);
        }
        leaders[u] = v;
        return true;
    }

};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> g(m);
    vector<int> count(n);
    int mx = 0, mx_iter = -1, iter = 0;
    for (auto& [x, y]: g) {
        cin >> x >> y;
        --x; --y;
        ++count[x];
        ++count[y];
        if ((count[x] > mx) || (count[y] > mx)) mx = max(count[y], count[x]), mx_iter = iter;
        ++iter;
    }
    DSU d(n);
    auto [m_u, m_v] = g[mx_iter];
    d.join(m_u, m_v);
    cout << m_u + 1 << ' ' << m_v + 1 << '\n';
    for (auto& [u, v]: g) {
        if(d.join(u, v)) {
            cout << u + 1 << ' ' << v + 1 << '\n';
        }
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

using Graph = vector<vector<int>>;

class DSU {
    vector<int> leaders;

public:
    DSU(int n) {
        leaders.resize(n);
        iota(leaders.begin(), leaders.end(), 0);
    }
    int find(int q) {
        if (q == leaders[q]) return q;
        return leaders[q] = find(leaders[q]);
    }
    bool join(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            return false;
        }
        if (rand() & 1) {
            swap(u, v);
        }
        leaders[u] = v;
        return true;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    Graph graph(n);
    for (int i = 0, u, v; i < m; ++i) {
        cin >> u >> v;
        --u;
        --v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    sort(graph.begin(), graph.end(),
         [](const vector<int>& lhs, const vector<int>& rhs) {
             return lhs.size() > rhs.size();
         });
    DSU d(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < graph[i].size(); ++j) {
            if (d.join(i, graph[i][j])) {
                cout << i + 1 << ' ' << graph[i][j] + 1 << '\n';
            }
        }
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

using Graph = vector<vector<int>>;

class DSU {
    vector<int> leaders;

public:
    DSU(int n) {
        leaders.resize(n);
        iota(leaders.begin(), leaders.end(), 0);
    }
    int find(int q) {
        if (q == leaders[q])
            return q;
        return leaders[q] = find(leaders[q]);
    }
    bool join(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            return false;
        }
        if (rand() & 1) {
            swap(u, v);
        }
        leaders[u] = v;
        return true;
    }

};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> g(m);
    vector<int> count(n);
    int mx = 0, mx_iter = -1, iter = 0;
    for (auto& [x, y]: g) {
        cin >> x >> y;
        --x; --y;
        ++count[x];
        ++count[y];
        if ((count[x] > mx) | (count[y] > mx)) mx = max(count[y], count[x]), mx_iter = iter;
        ++iter;
    }
    DSU d(n);
    auto [m_u, m_v] = g[mx_iter];
    d.join(m_u, m_v);
    cout << m_u + 1 << ' ' << m_v + 1 << '\n';
    for (auto& [u, v]: g) {
        if(d.join(u, v)) {
            cout << u + 1 << ' ' << v + 1 << '\n';
        }
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

using Graph = vector<vector<int>>;

class DSU {
    vector<int> leaders;

public:
    DSU(int n) {
        leaders.resize(n);
        iota(leaders.begin(), leaders.end(), 0);
    }
    int find(int q) {
        if (q == leaders[q])
            return q;
        return leaders[q] = find(leaders[q]);
    }
    bool join(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            return false;
        }
        if (rand() & 1) {
            swap(u, v);
        }
        leaders[u] = v;
        return true;
    }

};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> g(m);
    vector<int> count(n);
    int mx = 0, mx_iter = -1, iter = 0;
    for (auto& [x, y]: g) {
        cin >> x >> y;
        --x; --y;
        ++count[x];
        ++count[y];
        if ((count[x] >= mx) || (count[y] >= mx)) mx = max(count[y], count[x]), mx_iter = iter;
        ++iter;
    }
    DSU d(n);
    auto [m_u, m_v] = g[mx_iter];
    d.join(m_u, m_v);
    cout << m_u + 1 << ' ' << m_v + 1 << '\n';
    for (auto& [u, v]: g) {
        if(d.join(u, v)) {
            cout << u + 1 << ' ' << v + 1 << '\n';
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n-1){
            int pos=vv[i].second;

            for(int j=0;j<v[pos].size();j++){
                if(sum>=n-1){
                    break;
                }

                if(vis[v[pos][j]]==0 || vis[pos]==0){
                    if(vis[pos]==0){
                        vis[pos]=1;
                        //sum++;
                    }
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<ans.size();i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n-1){
            int pos=vv[i].second;

            for(int j=0;j<v[pos].size();j++){
                if(sum>=n-1){
                    break;
                }

                if(vis[v[pos][j]]==0 || vis[pos]==0){
                    if(vis[pos]==0){
                        vis[pos]=1;
                        //sum++;
                    }
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<min(n-1,(int)ans.size());i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n-1){
            int pos=vv[i].second;

            for(int j=0;j<v[pos].size();j++){
                if(sum>=n-1){
                    break;
                }
                if(vis[pos]==0){
                    vis[pos]=1;
                    //sum++;
                }
                if(vis[v[pos][j]]==0){
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<ans.size();i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n){
            int pos=vv[i].second;

            for(int j=0;j<v[pos].size();j++){
                if(sum>=n){
                    if(i!=0)break;

                }
                if(vis[pos]==0){
                    vis[pos]=1;
                    sum++;
                }
                if(vis[v[pos][j]]==0){
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<ans.size();i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n){
            int pos=vv[i].second;

            for(int j=0;j<v[pos].size();j++){
                if(sum==n)break;
                if(vis[pos]==0){
                    vis[pos]=1;
                    sum++;
                }
                if(vis[v[pos][j]]==0){
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<ans.size();i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n){
            int pos=vv[i].second;

            for(int j=0;j<v[pos].size();j++){
                if(sum>=n)break;
                if(vis[pos]==0){
                    vis[pos]=1;
                    sum++;
                }
                if(vis[v[pos][j]]==0){
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<ans.size();i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n){
            int pos=vv[i].second;
            if(vis[pos]==0){
                vis[pos]=1;
                sum++;
            }
            for(int j=0;j<v[pos].size();j++){
                if(sum==n)break;
                if(vis[v[pos][j]]==0){
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<ans.size();i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int vis[200007];
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>v[n+5];
    int a[n+5];
    memset(a,0,sizeof a);
    vector<pair<int,int> >vv,ans;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        a[x]++;
        a[y]++;
    }

    for(int i=1;i<=n;i++){
        vv.push_back({a[i],i});
    }
    sort(vv.rbegin(),vv.rend());
    int sum=0;
    for(int i=0;i<vv.size();i++){
        if(sum<n-1){
            int pos=vv[i].second;

            for(int j=0;j<v[pos].size();j++){
                if(sum>=n-1){
                    break;
                }

                if(vis[v[pos][j]]==0 || vis[pos]==0){
                    if(vis[pos]==0){
                        vis[pos]=1;
                        //sum++;
                    }
                    vis[v[pos][j]]=1;
                    ans.push_back({pos,v[pos][j]});
                    sum++;
                }
            }
        }
        else{
            break;
        }
    }
    for(int i=0;i<min(n-1,ans.size());i++)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}