/*
ID: k.corne1
PROG: msquare
LANG:C++
*/

#define PROG "msquare"

#include <stdlib.h> /* exit */
#include <string.h> /* strcmp, memset */
#include <stdio.h> /* fprintf, fputc, fgets */
#include <limits.h> /* INT_MAX ... */
#include <ctype.h> /* isspace, isalpha, isupper, islower, isdigit */
#include <set>
#include <queue>
#include <string>
using namespace std;

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) < (b) ? (b) : (a))

int N,L;

int conn[1001][1001];
int diff[1001][1001];
int dist[1001][1001];

int DetermineOverlap(string s1, string s2)
{
	int ret=0;
	int i,pos=0;
	
	sort(s1.begin(),s1.end());
	sort(s2.begin(),s2.end());
	
	for(i=0;i<L;i++)
	{
		while(pos<L&&s2[pos]<s1[i])
		{
			pos++;
		}
		if(pos==L) return ret;
		if(s1[i]==s2[pos])
		{
			ret++;
			pos++;
		}
	}
	
	return ret;
}




int run()
{
	int i,j,k;
	
	string s;
		
	char buff[30];
	vector<string> words;
	
	int bestd=2000,beststart=1000;
	
	scanf("%d %d\n",&N,&L);
	
	//printf("N = %d\n",N);
	
	memset(conn,0,sizeof(conn));
	memset(diff,0,sizeof(diff));
	
	for(i=0;i<1001;i++)
	{
		for(j=0;j<1001;j++)
		{
			dist[i][j]=2000;
		}
	}
	
	
	
	for(i=0;i<N;i++)
	{
		scanf("%s\n",&buff);
		s=buff;
		words.push_back(s);
	}
	
	sort(words.begin(),words.end());
	
	/*for(i=0;i<N;i++)
	{
		printf("%s\n",words[i].c_str());
	}*/
	
	for(i=0;i<N;i++)
	{
		for(j=i+1;j<N;j++)
		{
			int ov = DetermineOverlap(words[i],words[j]);
			if(ov==L-1)
			{
				conn[i][j]=1;
				conn[j][i]=1;
				dist[i][j]=1;
				dist[j][i]=1;
				
			}
			if(ov==0)
			{
				diff[i][j]=1;
				diff[j][i]=1;
				//printf("%s %s\n",words[i].c_str(),words[j].c_str());
			}
		}
	}
				
	for(k=0;k<N;k++)
	{
		for(i=0;i<N;i++)
		{
			for(j=0;j<N;j++)
			{
				dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
			}
		}
	}
	
	for(i=0;i<N;i++)
	{
		for(j=i+1;j<N;j++)
		{
			if(diff[i][j]&&dist[i][j]<bestd)
			{
				bestd=dist[i][j];
				beststart=i;
			}
			
		}
	}
	
	for(j=0;j<N;j++)
	{
		if(diff[beststart][j]&&dist[beststart][j]==bestd)
		{
			dist[j][1000]=1;
		}
		
	}
	
	//printf("N = %d\n",N);
	
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			//if(i==1&&j==4) printf("%d\n",dist[1][1000]);
			dist[i][1000]=min(dist[i][1000],dist[i][j]+dist[j][1000]);
			//if(i==1&&j==4) printf("%d\n",dist[1][1000]);
		}
	}
	
	
	printf("%s", words[beststart].c_str());
	//printf("%d\n", bestd);
	
	//printf("%d\n", dist[0][1]);
	
	//printf("%d\n", diff[0][4]);
	//printf("dist[1][4] = %d\n", dist[1][4]);
	//printf("dist[4][1000] = %d\n", dist[4][1000]);
	//printf("dist[1][1000] = %d\n", dist[1][1000]);
	//printf("%d\n", dist[0][4]);
	
	int cur=beststart;
	
	for(i=bestd;i>=1;i--)
	{
		//printf("%d: ",i);
		for(j=0;j<N;j++)
		{
			if(dist[cur][j]==1&&dist[j][1000]==i)
			{
				printf(" %s",words[j].c_str());
				cur=j;
				break;
			}
		}
		//if(i==1) printf("Hall\n");
	}
	
	printf("\n");
	
	
	
	return 0;
}


int main ()
{
    int i,T;
    
    scanf("%d\n",&T);
    
    for(i=0;i<T;i++)
    {
    	run();
    } 
    
    //printf("Hallo!\n");
        
    exit (0);
}
