#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n) & 1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;


enum Face {TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM};
Face opposite(Face f){ return (Face)(5 - (int)f);}


class Dice{
public:
  Dice() : dice(6){
    dice[TOP] = 0; dice[BOTTOM] = 5;
    dice[FRONT] = 1; dice[BACK] = 4;
    dice[RIGHT] = 2; dice[LEFT] = 3;
  }

  const int &operator[](Face f) const { return dice[f]; }
  void rot_x(){
    shift(dice[TOP], dice[RIGHT], dice[BOTTOM], dice[LEFT]);
  }
  void rot_y(){
    shift(dice[TOP], dice[BACK], dice[BOTTOM], dice[FRONT]);
  }
  void rot_z(){
    shift(dice[FRONT], dice[LEFT], dice[BACK], dice[RIGHT]);
  }
  
  Dice normalize(){
    Dice res = *this;
    for(int i = 0; i < 6; i % 2 ? rot_y() : rot_x(), i++){
      for(int j = 0; j < 4; rot_z(), j++){
	if(this->dice < res.dice) res = *this;
      }
    }
    return res;
  }

  static vector<Dice> all_rots(){
    vector<Dice> res;
    Dice d;
    for(int i = 0; i < 6; i % 2 ? d.rot_y() : d.rot_x(), i++){
      for(int j = 0; j < 4; d.rot_z(), j++){
	res.push_back(d);
      }
    }
    return res;
  }

  friend ostream &operator<<(ostream &out, const Dice &dice){
    out << "(T: " << dice[TOP]   << ", F: " << dice[FRONT]
	<< ", R: "<< dice[RIGHT] << ", L: " << dice[LEFT]
	<< ", Ba: "<< dice[BACK]  << ", Bo: " << dice[BOTTOM] << ")";
    return out;
  }
  
private:
  void shift(int &a, int &b, int &c, int &d){
    int tmp = a;
    a = b; b = c; c = d; d = tmp;
  }
  vector<int> dice;
};

set<int> sum_set;
int T[3][3], F[3][3], R[3][3];
Dice cube[3][3][3];

bool inside(int x, int y, int z){
  return 0 <= x && x < 3 && 0 <= y && y < 3 && 0 <= z && z < 3;
}

void calc(int x, int y, int z){
  
  if(z == 3){
    int sum = 0;
    REP(i, 3)REP(j, 3) sum += R[i][j];
    sum_set.insert(sum + 9);
  }else if(y == 3){
    calc(x, 0, z + 1);
  }else if(x == 3){
    calc(0, y + 1, z);
  }else{
    vector<Dice> dices = Dice::all_rots();
    REP(i, dices.size()){
      Dice dice = dices[i];


      if(z == 0 && F[y][x] >= 0 && F[y][x] != dice[FRONT]){
	continue;
      }
      
      if(y == 0 && T[2-z][x] >= 0 && T[2-z][x] != dice[TOP]){
	continue;
      }

      
      bool ok = true;
      int dx[6] = {0, 1, 0, 0, -1, 0};
      int dy[6] = {0, 0, 1, -1, 0, 0};
      int dz[6] = {1, 0, 0, 0, 0, -1};
      Face dir[6] = {BACK, RIGHT, BOTTOM, TOP, LEFT, FRONT};
      REP(j, 6){
	int nx = x + dx[j];
	int ny = y + dy[j];
	int nz = z + dz[j];
	if(inside(nx, ny, nz)){
	  if(nz > z) continue;
	  if(nz == z && ny > y) continue;
	  if(nz == z && ny == y && nx > x) continue;

	  int f  = dice[dir[j]];
	  int nf = cube[nx][ny][nz][opposite(dir[j])];
	  if(f + nf != 5) ok = false;
	}
      }
      
      if(ok){
	if(x == 2) R[y][z] = dice[RIGHT];
	cube[x][y][z] = dice;	 
	calc(x + 1, y, z);
      }
    }
  }
}

int main(){
  int n;
  cin >> n;
  while(n--){
    REP(i, 3)REP(j, 3){
      cin >> T[i][j];
      T[i][j]--;
    }
    
    REP(i, 3)REP(j, 3){
      cin >> F[i][j];
      F[i][j]--;
    }

    sum_set.clear();
    calc(0, 0, 0);

    if(sum_set.empty()){
      cout << 0 << endl;
    }else{
      vector<int> res;
      FOREACH(it, sum_set) res.push_back(*it);
      int sz = res.size();
      REP(i, sz) cout << res[i] << (i + 1 == sz ? '\n' : ' ');
    }
  }
  vector<Dice> dices = Dice::all_rots();
  
  return 0;
}