#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
bool exist[16];
bool vis[16];
int can[16][16];
int t_path[212345][10];
int path[16];
int n;
int sum;
void dfs(int u,int now){
int i;
if(now>n){
for(i=1;i<=9;++i){
t_path[sum][i]=path[i];
}
++sum;
return;
}
for(i=1;i<=9;++i){
if(exist[i]&&!vis[i]){
if( (u==1&&(i==3||i==9||i==7)) || (u==3&&(i==1||i==7||i==9)) ||
(u==9&&(i==7||i==1||i==3)) || (u==7&&(i==9||i==3||i==1)) ){
if(vis[can[u][i]]){
path[now]=i;
vis[i]=true;
dfs(i,now+1);
vis[i]=false;
}
else{
}
}
else if( (u==2&&i==8) || (u==8&&i==2) ||
(u==4&&i==6) || (u==6&&i==4) ){
if(vis[can[u][i]]){
path[now]=i;
vis[i]=true;
dfs(i,now+1);
vis[i]=false;
}
else{
}
}
else{
path[now]=i;
vis[i]=true;
dfs(i,now+1);
vis[i]=false;
}
}
}
}
void init(){
memset(exist,false,sizeof(exist));
}
int main(){
memset(can,0,sizeof(can));
can[1][3]=2;
can[1][9]=5;
can[1][7]=4;
can[3][1]=2;
can[3][7]=5;
can[3][9]=6;
can[9][7]=8;
can[9][1]=5;
can[9][3]=6;
can[7][9]=8;
can[7][3]=5;
can[7][1]=4;
//---
can[2][8]=5;
can[8][2]=5;
can[4][6]=5;
can[6][4]=5;
int T;
int i;
int t;
int j;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
for(i=0;i<n;++i){
scanf("%d",&t);
exist[t]=true;
}
sum=0;
for(i=1;i<=9;++i){
memset(vis,false,sizeof(vis));
if(exist[i]){
path[1]=i;
vis[i]=true;
dfs(i,2);
vis[i]=false;
}
}
printf("%d\n",sum);
for(i=0;i<sum;++i){
for(j=1;j<n;++j){
printf("%d ",t_path[i][j]);
}
printf("%d\n",t_path[i][n]);
}
}
return 0;
}