#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Dice{
  int s[6];
  void roll(char c){
    //the view from above
    // N
    //W E
    // S
    //s[0]:top
    //s[1]:south (front)
    //s[2]:east
    //s[3]:west
    //s[4]:north
    //s[5]:bottom
    int b;
    if(c=='E'){
      b=s[0];
      s[0]=s[3];
      s[3]=s[5];
      s[5]=s[2];
      s[2]=b;
    }
    if(c=='W'){
      b=s[0];
      s[0]=s[2];
      s[2]=s[5];
      s[5]=s[3];
      s[3]=b;
    }
    if(c=='N'){
      b=s[0];
      s[0]=s[1];
      s[1]=s[5];
      s[5]=s[4];
      s[4]=b;
    }
    if(c=='S'){
      b=s[0];
      s[0]=s[4];
      s[4]=s[5];
      s[5]=s[1];
      s[1]=b;
    }
    // migi neji
    if(c=='R'){
      b=s[1];
      s[1]=s[3];
      s[3]=s[4];
      s[4]=s[2];
      s[2]=b;
    }
    if(c=='L'){
      b=s[1];
      s[1]=s[2];
      s[2]=s[4];
      s[4]=s[3];
      s[3]=b;
    }
  }
  int top() {
    return s[0];
  }
};

int used[5][5][5];
Dice ds[5][5][5];
vector<Dice> cd;
vector<int> ans;
int cx[]={1,2,3,1,1,2,3,1};
int cy[]={2,2,2,3,1,1,1,1};
int cz[]={1,1,1,1,2,2,2,3};

int dx[]={0,0,1,-1,0,0};
int dy[]={-1,0,0,0,0,1};
int dz[]={0,-1,0,0,1,0};

void dfs(){
  
  for(int k=0;k<8;k++){
    int x=cx[k],y=cy[k],z=cz[k];
    if(used[x][y][z]) continue;
    //cout<<"k:"<<k<<endl;
    for(int i=0;i<(int)cd.size();i++){
      bool f=1;
      for(int j=0;j<6;j++){
	int nx=x+dx[j],ny=y+dy[j],nz=z+dz[j];
	if(!used[nx][ny][nz]) continue;
	f&=cd[i].s[j]==ds[nx][ny][nz].s[j];
      }
      if(f){
	used[x][y][z]=1;
	ds[x][y][z]=cd[i];
	dfs();
	used[x][y][z]=0;
      }
    }
    return;
  }
  
  for(int x=1;x<=3;x++){
    for(int y=1;y<=3;y++){
      for(int z=1;z<=3;z++){
	if(used[x][y][z]) continue;
	//cout<<x<<" "<<y<<" "<<z<<endl;
	for(int i=0;i<(int)cd.size();i++){
	  bool f=1;
	  for(int j=0;j<6;j++){
	    int nx=x+dx[j],ny=y+dy[j],nz=z+dz[j];
	    if(!used[nx][ny][nz]) continue;
	    f&=cd[i].s[j]==ds[nx][ny][nz].s[j];
	  }
	  if(f){
	    used[x][y][z]=1;
	    ds[x][y][z]=cd[i];
	    dfs();
	    used[x][y][z]=0;
	  }
	}
	return;
      }
    }
  }
  int tmp=0;
  for(int y=1;y<=3;y++)
    for(int z=1;z<=3;z++)
      tmp+=ds[3][y][z].s[2];
  ans.push_back(tmp);
}

signed main(){
  Dice d;
  for(int i=0;i<6;i++) d.s[i]=i+1;
  {
    for(int i=0;i<6;i++){
      Dice t(d);
      if(i==1) t.roll('N'); 
      if(i==2) t.roll('S'); 
      if(i==3) t.roll('S'),t.roll('S');
      if(i==4) t.roll('L'); 
      if(i==5) t.roll('R'); 
      for(int k=0;k<4;k++){
	cd.push_back(t);
	t.roll('E');
      }
    }
    /*
    for(int i=0;i<(int)cd.size();i++){
      cout<<i<<":";
      for(int j=0;j<6;j++) cout<<cd[i].s[j]<<" ";
      cout<<endl;
    }
    */
  }
  int T;
  cin>>T;
  while(T--){
    memset(used,0,sizeof(used));
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
	cin>>ds[j+1][0][3-i].s[0];
	if(ds[j+1][0][3-i].s[0]) used[j+1][0][3-i]=1;
      }
    }
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
	cin>>ds[j+1][i+1][0].s[1];
	if(ds[j+1][i+1][0].s[1]) used[j+1][i+1][0]=1;
      }
    }
    ans.clear();
    dfs();
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    for(int i=0;i<(int)ans.size();i++){
      if(i) cout<<" ";
      cout<<ans[i];
    }
    if(ans.empty()) cout<<0;
    cout<<endl;
  }
  return 0;
}

Measured memory_bytes: 3228