#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
using namespace std;
using Int = long long;
using ll = long long;
using Double = long double;
const Int INF = (1LL<<55)+1e9; // ~ 3.6 * 1e16
const Int mod = (1e9)+7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int,Int>;
using T = tuple<Int,Int,Int>;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
ostream& operator<<(ostream& o,T t){return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){Int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){Int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

const Int TOP = 0,FRONT = 1,LEFT = 3, RIGHT = 2,BACK = 4,BOTTOM = 5;
class Dice{
public:
  vector<Int> val;
  Dice():val(6){for(Int i=0;i<6;i++) val[i] = i+1;}
  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 getDice(Int top){
  Int front = top == 6 || top == 1? 2:1;
  Dice d; d.init(top, front);
  return d;
}


vector<vector<Int> > Top;
vector<vector<Int> > Front;
vector<vector<vector<Dice> > > dice;
vector<Int>  num;
void dfs(Int i,Int j,Int k,Int sum){
  if(j == 3) i++, j = 0;
  if(i == 3) i = 0, k++;
  if(k == 3) {num.push_back(sum);return;}

  for(Int top = 1; top<=6; top++){
    if(k == 0 && Top[i][j] != 0 && top != Top[i][j]) continue;
    if(k != 0 && top + dice[i][j][k-1][BOTTOM] != 7) continue;
    
    auto check= [&](Dice &d){
      if(i - 1 >= 0 && d[BACK] + dice[i-1][j][k][FRONT] != 7) return 0;
      if(j - 1 >= 0 && d[LEFT] + dice[i][j-1][k][RIGHT] != 7) return 0;
      if(i==2 && Front[k][j] != 0 && d[FRONT] + Front[k][j] != 7) return 0;
      return 1;
    };
    Dice d = getDice(top);
    for(Int s=0; s < 4; s++, d.rotCCW()){
      if(!check(d)) continue;
      dice[i][j][k] = d;
      Int nsum = sum + (j==0? d[LEFT]:0);
      dfs(i, j+1, k, nsum);
    }
  }
}

signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);
  Int N;
  cin>>N;
  while(N--){
    Top = vector<vector<Int> > (3,vector<Int>(3));
    Front = vector<vector<Int> > (3,vector<Int>(3));
    cin>>Top;
    cin>>Front;
  
    num.clear();
    dice = vector<vector<vector<Dice> > > (3, vector<vector<Dice> > (3,vector<Dice>(3)));
    dfs(0, 0 ,0, 0);
    sort(num.begin(),num.end());
    num.erase(unique(num.begin(), num.end()), num.end());
    if(num.size() == 0) cout<<0<<endl;
    else cout<<num<<endl;
  }
  return 0;
}



Measured memory_bytes: 3272