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

const int TOP = 0,FRONT = 1,LEFT = 3, RIGHT = 2,BACK = 4,BOTTOM = 5;

class Dice{
public:
  int TOP = 0,FRONT = 1,LEFT = 3, RIGHT = 2,BACK = 4,BOTTOM = 5;
  vector<int> val;
  Dice():val(6){for(int i=0;i<6;i++) val[i] = i;}
  Dice(int val[6]){for(int i=0;i<6;i++) this->val[i] = val[i];}
  Dice(vector<int> val):val(val){assert(val.size() == 6);}
  int& operator [](int a){return val[a];}
  bool operator == (Dice a)const{
    for(int i=0;i<6;(i%2? a.rotN():a.rotE()),i++)
      for(int j=0;j<4;j++){
        if(val == a.val) return 1;
        a.rotCW();
      }
    return 0;
  }

  void rot(string s){
    if(s == "N" || s == "B") rotN();
    else if(s == "S" || s == "F") rotS();
    else if(s == "E" || s == "R") rotE();
    else if(s == "W" || s == "L") rotW();
    else if(s == "CW") rotCW();
    else if(s == "CCW") rotCCW();
    else assert(!"rot error"); /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  }

  void rot(char ch){rot(string(1,ch));}
  
  //奥に転がす
  void rotN(){
    swap(val[TOP],val[FRONT]);
    swap(val[FRONT],val[BOTTOM]);
    swap(val[BOTTOM],val[BACK]);
  }

  //手前に転がす
  void rotS(){
    //rotN(),rotN(),rotN();
    swap(val[TOP],val[BACK]);
    swap(val[BACK],val[BOTTOM]);
    swap(val[BOTTOM],val[FRONT]);
  }
  
  //右に転がす
  void rotE(){
    swap(val[TOP],val[LEFT]);
    swap(val[LEFT],val[BOTTOM]);
    swap(val[BOTTOM],val[RIGHT]);
  }

  //左に転がす
  void rotW(){
    //rotE(),rotE(),rotE();
    swap(val[TOP],val[RIGHT]);
    swap(val[RIGHT],val[BOTTOM]);
    swap(val[BOTTOM],val[LEFT]);
  }

  //上から見て時計回りに回す
  void rotCW(){
    swap(val[FRONT],val[RIGHT]);
    swap(val[RIGHT],val[BACK]);
    swap(val[BACK],val[LEFT]);
  }

  //上からみて反時計回りに回す。
  void rotCCW(){
    //rotCW(),rotCW(),rotCW();
    swap(val[FRONT],val[LEFT]);
    swap(val[LEFT],val[BACK]);
    swap(val[BACK],val[RIGHT]);
  }

  void init(int top,int front){
    for(int i=0;i<6;(i%2? rotN():rotE()),i++)
      for(int j=0;j<4;j++){
        if(val[TOP] == top && val[FRONT] == front) return;
        rotCW();
      }
    assert(!"init error");
  }
};

Dice dice[27];

set<int> ans;

bool check1(int x){
  
  if( x % 3 != 0 && dice[x].val[LEFT] + dice[x-1].val[RIGHT] != 5 ) return false;
  
  if( x % 9 >= 3 && dice[x].val[TOP] + dice[x-3].val[BOTTOM] != 5 ) return false;
  
  if( x >= 9 && dice[x].val[FRONT] + dice[x-9].val[BACK] != 5 ) return false;
  
  return true;
}

int T[9], F[9];

bool check2(){
  
  int t[9] = {18,19,20,9,10,11,0,1,2};
  
  for(int i=0;i<9;i++){
    if( T[i] == 0 ) continue;
    if( dice[t[i]].val[TOP] + 1 != T[i] ) return false;
  }
  
  for(int i=0;i<9;i++){
    if( F[i] == 0 ) continue;
    if( dice[i].val[FRONT] + 1 != F[i] ) return false;
  }
  
  return true;
}

int cal(){
  int res = 0;
  for(int i=2;i<27;i+=3) res += dice[i].val[RIGHT] + 1;
  return res;
}

void dfs(int x){
  
  if( x == 27 ){
    
    if( check2() == true ) ans.insert(cal());
    
    return;
  }
  
  int top[6] = {0,1,2,3,4,5}, front[6] = {1,2,4,0,0,1};

  for(int i=0;i<6;i++){
    
    dice[x].init( top[i], front[i] );
    
    for(int j=0;j<4;j++){

      dice[x].rotCW();
      
      if( check1(x) == false ) continue;
      
      dfs( x + 1 );
      
    }
    
  }
  
}

signed main(){
  
  int N;
  cin>>N;

  while(N--){
    
    for(int i=0;i<9;i++) cin>>T[i];
    
    for(int i=0;i<9;i++) cin>>F[i];
    
    ans.clear();
    
    dfs(0);
    
    if( (int)ans.size() == 0 ) cout<<0<<endl;
    else{
      
      int flag = 0;
      
      for(int p : ans){
	if( flag ) cout<<' ';
	cout<<p;
	flag = 1;
      }
      
      cout<<endl;
      
    }
    
  }
  
  return 0;
}



Measured memory_bytes: 3016