#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

struct Dice
{
	int top, right, front;
	Dice() :top(1), right(2), front(3) {};
	Dice(int t, int r, int f) :top(t), right(r), front(f) {};
	void set(int t, int r, int f) { top = t, right = r, front = f; }
	int get_top() { return top; }
	int get_bottom() { return 7 - top; }
	int get_right() { return right; }
	int get_left() { return 7 - right; }
	int get_front() { return front; }
	int get_back() { return 7 - front; }
	void to_front() { swap(front, top); top = 7 - top; }
	void to_back() { REP(i, 3) to_front(); }
	void to_right() { swap(top, right); top = 7 - top; }
	void to_left() { REP(i, 3) to_right(); }
	void to_clock() { swap(front, right); right = 7 - right; }
	void to_counter() { REP(i, 3) to_clock(); }
};


bool operator<(const Dice& a, const Dice& b)
{
	int at = a.top, bt = b.top;
	int af = a.front, bf = b.front;
	int ar = a.right, br = b.right;
	if (at != bt) return at < bt;
	if (af != bf) return ar < br;
	if (ar != br) return af < bf;
	return false;
}

struct Cube
{
	Dice dice[3][3][3];
	Cube()
	{
		REP(i, 3)REP(j, 3)REP(k, 3) dice[i][j][k].set(-1, -1, -1);
	}
};

const int dx[] = { -1,0,0,0,0,1 };
const int dy[] = { 0,-1,0,1,0,0 };
const int dz[] = { 0,0,-1,0,1,0 };
int state(int x, int y, int z)
{ 
	if (z == 3) return 1;
	else if (x == -1) return 2;
	else if(x >= 0 && y >= 0 && z >= 0 && x < 3 && y < 3 && z < 3) return 0; 
	else return 3;
}

set<Dice> pat;

void init()
{
	Dice d(1, 3, 2);
	vector<Dice> v;
	REP(i, 4)
	{
		d.to_front();
		v.push_back(d);
	}
	REP(i, v.size())
	{
		Dice tmp = v[i];
		REP(j, 4)
		{
			tmp.to_right();
			pat.insert(tmp);
		}
		REP(k, 4)
		{
			tmp.to_clock();
			pat.insert(tmp);
		}
	}
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	init();
	int n; cin >> n;
	REP(cs, n)
	{
		vector<vi> top(3, vi(3)), front(3, vi(3));
		REP(i, 3)REP(j, 3) cin >> top[i][j];
		REP(i, 3)REP(j, 3) cin >> front[i][j];
		Cube cube;
		set<int> ans;
		queue<tuple<Cube, int, int, int>> que;
		que.emplace(cube, 0, 0, 0);
		while (!que.empty())
		{
			Cube tmp; int x, y, z; 
			tie(tmp, x, y, z) = que.front();
			que.pop();
			int tt = -1, tf = -1, tr = -1;
			bool flag = true;
			REP(i, 6)
			{
				int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
				int st = state(nx, ny, nz);
				if (st == 1)
				{
					if (front[x][y] != 0)
					{
						if(tf != -1 && tf != front[x][y]) flag = false;
						tf = front[x][y];
					}
				}
				else if (st == 2)
				{
					if (top[z][y] != 0)
					{
						if (tt != -1 && tt != top[z][y]) flag = false;
						tt = top[z][y];
					}
				}
				else if (st == 0 && tmp.dice[nx][ny][nz].get_top() != -1)
				{
					if (i == 0)
					{
						if (tt != -1 && tt != tmp.dice[nx][ny][nz].get_top()) flag = false;
						tt = tmp.dice[nx][ny][nz].get_top();
					}
					if (i == 1) tr = tmp.dice[nx][ny][nz].get_right();
					if (i == 2)
					{
						if (tf != -1 && tf != tmp.dice[nx][ny][nz].get_front()) flag = false;
						tf = tmp.dice[nx][ny][nz].get_front();
					}
					if (i == 3) tr = tmp.dice[nx][ny][nz].get_right();
					if (i == 4) tt = tmp.dice[nx][ny][nz].get_front();
					if (i == 5) tt = tmp.dice[nx][ny][nz].get_top();
				}
			}
			if (!flag) continue;
			int nx = x, ny = y, nz = z;
			nz++;
			if (nz == 3) { nz = 0, ny++; }
			if (ny == 3) { ny = 0, nx++; }
			for (auto d : pat)
			{
				if (tt != -1 && tt != d.get_top()) continue;
				if (tf != -1 && tf != d.get_front()) continue;
				if (tr != -1 && tr != d.get_right()) continue;
				Cube nc = tmp;
				nc.dice[x][y][z] = d;
				if (x == 3)
				{
					int sum = 0;
					REP(i, 3)REP(j, 3) sum += nc.dice[i][2][j].get_right();
					ans.insert(sum);
				}
				else
				{
					que.emplace(nc, nx, ny, nz);
				}
			}
		}
		vi ansv;
		for (auto i : ans) ansv.push_back(i);
		if (ansv.size() == 0) cout << 0 << endl;
		else REP(i, ansv.size()) cout << ansv[i] << (i == ansv.size() - 1 ? "\n" : " ");
	}
	return 0;
}

Measured memory_bytes: 11560