#include<bits/stdc++.h>
using namespace std;

enum FACE { TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM };

struct dice{
  int var[6];
  int id[6];
  
  dice() {
    id[0] = TOP;
    id[1] = FRONT;
    id[2] = RIGHT;
    id[3] = LEFT;
    id[4] = BACK;
    id[5] = BOTTOM;
    for(int i=0;i<6;i++) var[i] = 0;      
  }
  void set(){
    id[0] = TOP;
    id[1] = FRONT;
    id[2] = RIGHT;
    id[3] = LEFT;
    id[4] = BACK;
    id[5] = BOTTOM;
    for(int i=0;i<6;i++) var[i] = i+1;      
  }

  int& operator[]( FACE f ) { return var[id[f]]; }
  const int& operator[] (FACE f) const { return var[id[f]]; }
  
  bool operator==(const dice &a) const {
    const dice &b = *this;
    for(int i=0;i<6;i++){
      if( a[(FACE)i] != b[(FACE)i] ) return false;
    }
    return true;
  }

  void roll_x(){ roll(TOP,BACK,BOTTOM,FRONT); }
  void roll_y(){ roll(TOP,LEFT,BOTTOM,RIGHT); }
  void roll_z(){ roll(FRONT,RIGHT,BACK,LEFT); }

  vector<dice> all_rolls(){
    vector<dice> res;
    for(int k=0;k<6;(k&1 ? roll_y() : roll_x()), k++ ){
      for(int i=0;i<4; roll_z(),i++)
        res.push_back( *this );
    }
    return res;
  }

  void roll(FACE a,FACE b,FACE c,FACE d){
    int tmp = id[a];
    id[a] = id[b];
    id[b] = id[c];
    id[c] = id[d];
    id[d] = tmp;
  }

  void view(){
    cout << " TOP " << id[TOP] << " "<< (*this)[TOP] << endl;
    cout << " FRONT " << id[FRONT] << " " << (*this)[FRONT] << endl;
    cout << " RIGHT " << id[RIGHT] << " " << (*this)[RIGHT] << endl;
    cout << " LEFT " << id[LEFT] << " "<< (*this)[LEFT] << endl;
    cout << " BACK " << id[BACK] << " " << (*this)[BACK] << endl;
    cout << " BOTTOM " << id[BOTTOM] << " " << (*this)[BOTTOM] << endl;
  }

};

dice D[4][4][4];//x,y,z
vector<int> ans;
void solve(int x,int y,int z){
  if( z == 4 ){
    int res=0;
    for(int i=1;i<=3;i++){
      for(int j=1;j<=3;j++){
        res += D[3][i][j][RIGHT];
      }
    }
    ans.push_back( res );
  } else if( y == 4 ) solve(1,1,z+1);
  else if( x == 4 ) solve(1,y+1,z);
  else {
    dice d; d.set();
    vector<dice> all = d.all_rolls();
    for(int i=0;i<(int)all.size();i++){
      //cout << x << " "<< y << " "<< z << endl;
      //cout << all[i][TOP] << endl;
      //all[i].view();
      if( D[x-1][y][z][RIGHT] && D[x-1][y][z][RIGHT] + all[i][LEFT] != 7 ) continue;
      if( D[x][y-1][z][BOTTOM] && D[x][y-1][z][BOTTOM] + all[i][TOP] != 7 ) continue;
      if( D[x][y][z-1][BACK] && D[x][y][z-1][BACK] + all[i][FRONT] != 7 ) continue;
      D[x][y][z] = all[i];
      // cout << "ok" << endl;
      // cout << x << " "<< y << " "<< z << endl;
      //d.view();
      solve(x+1,y,z);
    }
  }
}

int main(){
  int N;
  cin >> N;
  for(int i=0;i<N;i++){

    for(int i=0;i<=3;i++)
      for(int j=0;j<=3;j++)
        for(int k=0;k<=3;k++)
          D[i][j][k] = dice();

    for(int j=3;j>=1;j--){
      for(int k=1;k<=3;k++){
        int a;
        cin >>a;
        if( a != 0 ){
          D[k][0][j][BOTTOM] = 7-a;
        } else {
          D[k][0][j][BOTTOM] = 0;
        }
      }
    }
    
    for(int j=1;j<=3;j++){
      for(int k=1;k<=3;k++){
        int a;
        cin >>a;
        if( a != 0 ){
          D[k][j][0][BACK] = 7-a;
        } else {
          D[k][j][0][FRONT] = 0;
        }
      }
    }
    ans.clear();
    solve(1,1,1);
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    if( ans.empty() ) cout << 0 << endl;
    else {
      for(int i=0;i<(int)ans.size();i++){
        if( i ) cout << " ";
        cout << ans[i];
      }
      cout << endl;
    }
  }
}

Measured memory_bytes: 1356