// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

 // } Driver Code Ends


class Solution 
{
    public:
	//Function to return a list containing the DFS traversal of the graph.
	vector<int>dfsOfGraph(int V, vector<int> adj[])
	{
	    vector<int> res;
	    bool to_visit[V];
	    for(int i=0;i<V;i++){
	        to_visit[i]=true;
	    }
	    stack<int> stack1,stack2;
	    stack1.push(0);
	    to_visit[0]=false;
	    while(!(stack1.empty())){
	        int x=stack1.top();
	        res.push_back(x);
	        stack1.pop();
	        for(auto iter:adj[x]){
	            if(to_visit[iter]){
	                stack2.push(iter);
	                to_visit[iter]=false;
	            }
	            while(!stack2.empty()){
	                x=stack2.top();
	                stack1.push(x);
	                stack2.pop();
	            }
	        }
	    }
	    return res;
	}
};

// { Driver Code Starts.
int main(){
	int tc;
	cin >> tc;
	while(tc--){
		int V, E;
    	cin >> V >> E;

    	vector<int> adj[V];

    	for(int i = 0; i < E; i++)
    	{
    		int u, v;
    		cin >> u >> v;
    		adj[u].push_back(v);
    		adj[v].push_back(u);
    	}
        // string s1;
        // cin>>s1;
        Solution obj;
        vector<int>ans=obj.dfsOfGraph(V, adj);
        for(int i=0;i<ans.size();i++){
        	cout<<ans[i]<<" ";
        }
        cout<<endl;
	}
	return 0;
}  // } Driver Code Ends