#include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)

int dx[]={0,0,1,0,-1,0};
int dy[]={0,-1,0,1,0,0};
int dz[]={-1,0,0,0,0,1};
int arc[]={5,3,4,1,2,0};

int dice[24][6]={
  {1,2,3,5,4,6},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {2,1,4,6,3,5},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {3,1,2,6,5,4},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {4,1,5,6,2,3},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {5,1,3,6,4,2},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {6,2,4,5,3,1},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
};
void init(){
  for(int i=0;i<24;i+=4){
    for(int j=1;j<4;j++){
      dice[i+j][0]=dice[i][0];
      dice[i+j][5]=dice[i][5];
      dice[i+j][4]=dice[i+j-1][1];
      for(int k=1;k<4;k++){
        dice[i+j][k]=dice[i+j-1][k+1];
      }
    }
  }
}

int a[5][5][5];   // a(x,y,z);

void input(){
  int x;
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][3][1]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][3][2]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][3][3]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][2][1]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][2][2]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][2][3]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][1][1]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][1][2]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][5]+x==7){a[0][1][3]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[1][0][1]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[1][0][2]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[1][0][3]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[2][0][1]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[2][0][2]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[2][0][3]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[3][0][1]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[3][0][2]=i;break;}
  cin>>x;
  if(x)r(i,24)if(dice[i][3]+x==7){a[3][0][3]=i;break;}
}
vector<int>v;
void dfs(int d){
  if(d==27){
    int sum=0;
    sum+=dice[a[1][1][3]][2];
    sum+=dice[a[2][1][3]][2];
    sum+=dice[a[3][1][3]][2];
    sum+=dice[a[1][2][3]][2];
    sum+=dice[a[2][2][3]][2];
    sum+=dice[a[3][2][3]][2];
    sum+=dice[a[1][3][3]][2];
    sum+=dice[a[2][3][3]][2];
    sum+=dice[a[3][3][3]][2];
    v.push_back(sum);
    return;
  }
  int x=(d%3)+1;
  int y=((d%9)/3)+1;
  int z=(d/9)+1;
  int match[6];
  memset(match,-1,sizeof(match));
  r(i,6){
    int nx=x+dx[i];
    int ny=y+dy[i];
    int nz=z+dz[i];
    if(a[nz][ny][nx]!=-1){
      match[i]=7-dice[a[nz][ny][nx]][arc[i]];
    }
  }
  r(i,24){
    int flag=0;
    r(j,6){
      if(match[j]!=-1&&dice[i][j]!=match[j])flag=1;
    }
    if(!flag){
      a[z][y][x]=i;
      dfs(d+1);
      a[z][y][x]=-1;
    }
  }
}
signed main(){
  init();
  int test;
  cin>>test;
  while(test--){
    memset(a,-1,sizeof(a));
    input();
    dfs(0);
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    if(v.size())r(i,v.size()){
      if(i)cout<<' ';
      cout<<v[i];
    }
    else cout<<0;
    cout<<endl;
    v.clear();
  }
}


Measured memory_bytes: 3236