1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<string> 5 #include<cstdio> 6 #include<vector> 7 #include<stack> 8 using namespace std; 9 struct node 10 { 11 int w,num; 12 }; 13 vector<node>v[27]; 14 stack<int>stk; 15 int n,f[27],ru[1001],chu[1001]; 16 string s[1001]; 17 bool b[1001]; 18 int fnd(int x) 19 { 20 return f[x]==x?x:f[x]=fnd(f[x]); 21 } 22 void fluery(int u,int eid) 23 { 24 for(int i=0;i<v[u].size();i++) 25 if(!b[v[u][i].num]) 26 { 27 b[v[u][i].num]=1; 28 fluery(v[u][i].w,v[u][i].num); 29 } 30 if(eid>0) 31 stk.push(eid); 32 } 33 int main() 34 { 35 int t; 36 scanf("%d",&t); 37 while(t--) 38 { 39 memset(ru,0,sizeof(ru)); 40 memset(chu,0,sizeof(chu)); 41 for(int i=1;i<=26;i++) 42 v[i].clear(); 43 scanf("%d",&n); 44 for(int i=1;i<=n;i++) 45 cin>>s[i]; 46 sort(s+1,s+n+1); 47 for(int i=1;i<=26;i++) 48 f[i]=i; 49 int p; 50 for(int i=1;i<=n;i++) 51 { 52 int x=s[i][0]-'a'+1,y=s[i][s[i].length()-1]-'a'+1; 53 chu[x]++; 54 ru[y]++; 55 node tmp; 56 tmp.w=y; 57 tmp.num=i; 58 v[x].push_back(tmp); 59 if(i==1) 60 p=x; 61 f[fnd(y)]=fnd(x); 62 } 63 bool flg=0; 64 int fa=fnd(p); 65 for(int i=1;i<=26;i++) 66 { 67 if(!ru[i]&&!chu[i]) 68 continue; 69 if(fnd(i)!=fa) 70 { 71 flg=1; 72 break; 73 } 74 } 75 if(flg) 76 { 77 printf("*** "); 78 continue; 79 } 80 int st=-1,en=-1,tot=0; 81 for(int i=1;i<=26;i++) 82 { 83 if(!ru[i]&&!chu[i]) 84 continue; 85 if(ru[i]!=chu[i]) 86 tot++; 87 if(ru[i]==chu[i]-1) 88 st=i; 89 if(ru[i]==chu[i]+1) 90 en=i; 91 } 92 if(tot&&(tot!=2||en<0||st<0)) 93 printf("*** "); 94 else 95 { 96 if(tot==0) 97 st=p; 98 memset(b,0,sizeof(b)); 99 fluery(st,0); 100 while(!stk.empty()) 101 { 102 int tmp=stk.top(); 103 stk.pop(); 104 cout<<s[tmp]; 105 if(!stk.empty()) 106 printf("."); 107 } 108 printf(" "); 109 } 110 } 111 return 0; 112 } 113