#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;

enum { U, F, R, D, B, L };
struct Dice {
	int face[6];
	Dice() { face[F] = 1; face[R] = 2; face[U] = 3; face[B] = 6; face[L] = 5; face[D] = 4; }
	int find_face(int f) {
		rep(i, 6) if(face[i] == f) return i;
		return -1;
	}
	void turn(int dir) {
		switch(dir) {
		case R: rotate(U, R, D, L); break;
		case B: rotate(U, B, D, F); break;
		case L: rotate(U, L, D, R); break;
		case F: rotate(U, F, D, B); break;
		case U: rotate(F, R, B, L); break; // ??????????????????????¨???????
		case D: rotate(F, L, B, R); break; // ??????????????????????¨???????
		default: assert(false);
		}
	}
	int & operator[](int n) { return face[n]; }
	vector<Dice> all() {
		vector<Dice> res;
		rep(k, 6) {
			rep(i, 4) { res.push_back(*this); turn(R); }
			if(k & 1) turn(U); else turn(F);
		}
		return res;
	}
	void rotate(int a, int b, int c, int d) {
		int t = face[d];
		face[d] = face[c]; face[c] = face[b]; face[b] = face[a]; face[a] = t;
	}
};

int u[3][3][3];
int f[3][3][3];
int r[3][3][3];
int ut[3][3][3];
int ft[3][3][3];

vector<Dice> ds[7][7][7];
struct P {
	int x, y, z;
};

vector<P> ps;
vector<int> ans;

void dfs(int n) {
	//cout << n << endl;
	if(n == 27) {
		int sum = 0;
		for(int y = 0; y < 3; y++) {
			for(int z = 0; z < 3; z++) {
				sum += r[2][y][z];
			}
		}
		ans.push_back(sum);
		return;
	}

	int x = ps[n].x, y = ps[n].y, z = ps[n].z;
	// u
	int uu = ut[x][y][z] ? u[x][y][z] : 0;
	for(int i = 0; i < 3; i++) {
		if(y == i) continue;
		if(u[x][i][z] == 0) continue;
		if(uu != 0 && uu != u[x][i][z]) return;
		uu = u[x][i][z];
	}
	// f
	int ff = ft[x][y][z] ? f[x][y][z] : 0;
	for(int i = 0; i < 3; i++) {
		if(z == i) continue;
		if(f[x][y][i] == 0) continue;
		if(ff != 0 && ff != f[x][y][i]) return;
		ff = f[x][y][i];
	}
	// r
	int rr = 0;
	for(int i = 0; i < 3; i++) {
		if(x == i) continue;
		if(r[i][y][z] == 0) continue;
		if(rr != 0 && rr != r[i][y][z]) return;
		rr = r[i][y][z];
	}
	for(Dice d : ds[uu][ff][rr]) {
		u[x][y][z] = d[U];
		f[x][y][z] = d[F];
		r[x][y][z] = d[R];
		dfs(n + 1);
		if(!ut[x][y][z]) u[x][y][z] = 0;
		if(!ft[x][y][z]) f[x][y][z] = 0;
		r[x][y][z] = 0;
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	{
		Dice d;
		vector<Dice> all = d.all();
		for(Dice a : all) {
			for(int i = 0; i < 7; i++) {
				for(int j = 0; j < 7; j++) {
					for(int k = 0; k < 7; k++) {
						if(i != 0 && i != a.face[U]) continue;
						if(j != 0 && j != a.face[F]) continue;
						if(k != 0 && k != a.face[R]) continue;
						ds[i][j][k].push_back(a);
					}
				}
			}
		}
	}

	for(int x = 0; x < 3; x++) {
		for(int y = 0; y < 3; y++) {
			for(int z = 0; z < 3; z++) {
				ps.push_back(P{ x, y, z });
			}
		}
	}

	int n;
	cin >> n;
	while(n--) {
		memset(u, 0, sizeof u);
		memset(f, 0, sizeof f);
		memset(r, 0, sizeof r);
		memset(ut, 0, sizeof u);
		memset(ft, 0, sizeof f);

		for(int z = 0; z < 3; z++) {
			for(int x = 0; x < 3; x++) {
				int a;
				cin >> a;
				for(int y = 0; y < 3; y++) {
					u[x][y][z] = a;
					if(a) ut[x][y][z] = 1;
				}
			}
		}
		for(int y = 0; y < 3; y++) {
			for(int x = 0; x < 3; x++) {
				int a;
				cin >> a;
				for(int z = 0; z < 3; z++) {
					f[x][y][z] = a;
					if(a) ft[x][y][z] = 1;
				}
			}
		}

		ans.clear();

		dfs(0);

		sort(ans.begin(), ans.end());
		ans.erase(unique(ans.begin(), ans.end()), ans.end());

		if(ans.size()) {
			for(int i = 0; i < ans.size(); i++) {
				cout << ans[i];
				if(i != ans.size() - 1) cout << " ";
			}
		}
		else {
			cout << 0;
		}
		cout << endl;
	}
}

Measured memory_bytes: 3272