#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { 1, -1, 0, 0,0,0 }; const int dy[] = { 0,0, 1, -1, 0,0 };const int dz[] = { 0,0,0, 0, 1, -1 };

const int side[6][4] = {
	{ 2, 4, 5, 3 },{ 1, 3, 6, 4 },{ 1, 5, 6, 2 },{ 1, 2, 6, 5 },{ 1, 4, 6, 3 },{ 2, 3, 5, 4 }
};
const int dice_right[6][6] = {
	{0,3,5,2,4,0},
	{ 4,0,1,6,0,3 },
	{ 2,6,0,0,1,5 },
	{ 5,1,0,0,6,2 },
	{ 3,0,6,1,0,4 },
	{ 0,4,2,5,3,0 }
};
struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
vi ans;
vector<vvi> dices(3, vvi(3, vi(3)));//men tate yoko
bool kakutei() {
	bool change = true;
	while (change) {
		change = false;
		REP(i, 3) {
			REP(j, 3) {
				if (dices[0][i][j] == 0) {
					REP(k, 3) {
						if (dices[1][k][j] > 0 && dices[2][k][2 - i] > 0) {
							dices[0][i][j] = dice_right[dices[1][k][j] - 1][dices[2][k][2 - i] - 1];
							if (dices[0][i][j] == 0)
								return false;
							change = true;
						}
					}
				}
				if (dices[1][i][j] == 0) {
					REP(k, 3) {
						if (dices[0][k][j] > 0 && dices[2][i][2 - k] > 0) {
							dices[1][i][j] = dice_right[dices[2][i][2 - k] - 1][dices[0][k][j] - 1];
							if (dices[1][i][j] == 0)
								return false;
							change = true;
						}
					}
				}
				if (dices[2][i][j] == -1) {
					REP(k, 3) {
						if (dices[0][2 - j][k] > 0 && dices[1][i][k] > 0) {
							dices[2][i][j] = dice_right[dices[0][2 - j][k] - 1][dices[1][i][k] - 1];
							if (dices[2][i][j] == 0)
								return false;
							change = true;
						}
					}
				}
			}
		}
	}
	return true;
}
void dfs() {
	vector<vvi> copy_dices = dices;
	REP(i, 3) {
		REP(j, 3) {
			if (dices[0][i][j] == 0) {
				REP(k, 6) {
					bool clear = true;
					REP(l, 3) {
						if (dices[1][l][j] > 0)
							if (dices[1][l][j] == k + 1 || dices[1][l][j] + k + 1 == 7)
								clear = false;
						if (dices[2][l][2 - i] > 0)
							if (dices[2][l][2 - i] == k + 1 || dices[2][l][2 - i] + k + 1 == 7)
								clear = false;
					}
					if (clear) {
						dices[0][i][j] = k + 1;
						if (!kakutei()) {
							dices = copy_dices;
							continue;
						}
						int ex[3] = { 0 };
						REP(l,3) {
							REP(x, 3) {
								REP(y, 3) {
									if (dices[l][x][y] <= 0)
										continue;
									int a = dices[l][x][y];
									if (a == 4)
										a = 3;
									if (a == 5)
										a = 2;
									if (a == 6)
										a = 1;
									ex[a - 1]++;
									if (ex[a - 1] > 9)
										clear = false;
								}
							}
						}
						if (!clear)
							continue;
						dfs();
						dices = copy_dices;
					}
				}
				return;
			}
			if (dices[1][i][j] == 0) {
				REP(k, 6) {
					bool clear = true;
					REP(l, 3)
						if (dices[0][l][j] == k + 1 || dices[0][l][j] + k + 1 == 7)
							clear = false;
					if (clear) {
						dices[1][i][j] = k + 1;
						if (!kakutei()) {
							dices = copy_dices;
							continue;
						}
						int ex[3] = { 0 };
						REP(l, 3) {
							REP(x, 3) {
								REP(y, 3) {
									if (dices[l][x][y] <= 0)
										continue;
									int a = dices[l][x][y];
									if (a == 4)
										a = 3;
									if (a == 5)
										a = 2;
									if (a == 6)
										a = 1;
									ex[a - 1]++;
									if (ex[a - 1] > 9)
										clear = false;
								}
							}
						}
						if (!clear)
							continue;
						dfs();
						dices = copy_dices;
					}
				}
				return;
			}
		}
	}
	int sum = 0;
	REP(i, 3) {
		REP(j, 3) {
			sum += dice_right[dices[0][2 - j][2] - 1][dices[1][i][2] - 1];
		}
	}
	ans.push_back(sum);
}
int main() {
	int kurikaesu;
	cin >> kurikaesu;
	while (kurikaesu--) {
		ans.clear();
		REP(i, 3) {
			REP(j, 3) {
				REP(k, 3) {
					dices[i][j][k] = -1;
				}
			}
		}
		REP(i, 3) {
			REP(j, 3) {
				cin >> dices[0][i][j];
			}
		}
		REP(i, 3) {
			REP(j, 3) {
				cin >> dices[1][i][j];
			}
		}
		if (kakutei())
			dfs();
		else
			goto emp;
		SORT(ans);
		ans.erase(unique(ans.begin(), ans.end()), ans.end());
		REP(i, ans.size()) {
			cout << ans[i];
			if (i != ans.size() - 1)
				cout << ' ';
		}
emp:
		if (ans.empty())
			cout << 0;
		cout << endl;
	}
}

Measured memory_bytes: 3156