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

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

enum{FRONT,BACK,RIGHT,LEFT,TOP,BOTTOM};
struct Die{
	int face[6]; // front,back,right,left,top,bottom
	Die():face{2,5,3,4,1,6}{}
	void Rotate(int a,int b,int c,int d){
		tie(face[a],face[b],face[c],face[d])=mt(face[b],face[c],face[d],face[a]);
	}
	void TurnF(){Rotate(2,4,3,5);}
	void TurnR(){Rotate(0,5,1,4);}
	void TurnU(){Rotate(0,2,1,3);}
};

typedef vector<vector<vector<Die>>> Cube;

bool ok(Cube cube,int x,int y,int z,const vvi& ts,const vvi& fs)
{
	if(x>0 && cube[x][y][z].face[BACK]+cube[x-1][y][z].face[FRONT]!=7) return false;
	if(y>0 && cube[x][y][z].face[LEFT]+cube[x][y-1][z].face[RIGHT]!=7) return false;
	if(z>0 && cube[x][y][z].face[BOTTOM]+cube[x][y][z-1].face[TOP]!=7) return false;
	if(z==2 && cube[x][y][z].face[TOP]!=ts[x][y] && ts[x][y]) return false;
	if(x==2 && cube[x][y][z].face[FRONT]!=fs[2-z][y] && fs[2-z][y]) return false;
	return true;
}

void dfs(Cube& cube,int x,int y,int z,const vvi& ts,const vvi& fs,vi& res)
{
	if(x==3) x=0,y++;
	if(y==3) y=0,z++;
	if(z==3){
		int sum=0;
		rep(i,3) rep(j,3)
			sum+=cube[i][2][j].face[RIGHT];
		res.push_back(sum);
		return;
	}
	
	Die d;
	rep(i,6){
		rep(j,4){
			cube[x][y][z]=d;
			if(ok(cube,x,y,z,ts,fs))
				dfs(cube,x+1,y,z,ts,fs,res);
			d.TurnU();
		}
		i&1?d.TurnF():d.TurnR();
	}
}

void solve(const vvi& ts,const vvi& fs)
{
	Cube cube(3,vector<vector<Die>>(3,vector<Die>(3)));
	vi res;
	dfs(cube,0,0,0,ts,fs,res);
	sort(all(res));
	res.erase(unique(all(res)),end(res));
	if(res.empty())
		cout<<0<<endl;
	else{
		rep(i,res.size())
			cout<<(i?" ":"")<<res[i];
		cout<<endl;
	}
}

int main()
{
	int tc; cin>>tc;
	rep(_,tc){
		vvi ts(3,vi(3)),fs(3,vi(3));
		rep(i,3) rep(j,3) cin>>ts[i][j];
		rep(i,3) rep(j,3) cin>>fs[i][j];
		solve(ts,fs);
	}
}