#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

char order[25] = "NNNNWNNNWNNNENNNENNNWNNN";

class Dice{
public:
	Dice(){
		init();
	};

	void roll(char dst){
		for(int i = 0; i < 6; i++) work[i] = number[i];
		switch(dst){
		case 'E':
			setNumber(work[3],work[1],work[0],work[5],work[4],work[2]);
			break;
		case 'S':
			setNumber(work[1],work[5],work[2],work[3],work[0],work[4]);
			break;
		case 'N':
			setNumber(work[4],work[0],work[2],work[3],work[5],work[1]);
			break;
		case 'W':
			setNumber(work[2],work[1],work[5],work[0],work[4],work[3]);
			break;
		}
	};

	void setPos(int num){
		for(int i = 0; i <= num; i++){
			roll(order[i]);
		}
	}

	int getTop(){
		return number[0];
	}

	int getFront(){
		return number[4];
	}

	int getEast(){
		return number[2];
	}

	int getNorth(){
		return number[1];
	}

	int getWest(){
		return number[3];
	}

	int getBottom(){
		return number[5];
	}

	int getSouth(){
		return number[4];
	}

	void setNumber(int n0,int n1,int n2,int n3,int n4,int n5){
		number[0] = n0;
		number[1] = n1;
		number[2] = n2;
		number[3] = n3;
		number[4] = n4;
		number[5] = n5;
	}

	void init(){
		setNumber(1,5,3,4,2,6);
	}

	void set(Dice arg){
		for(int i = 0; i < 6; i++){
			number[i] = arg.number[i];
		}
	}

	int number[6];
	int work[6];
};

bool rangeCheck(int height,int row,int col){
	if(height >= 0 && height <= 2 && row >= 0 && row <= 2 && col >= 0 && col <= 2)return true;
	else{
		return false;
	}
}

int top[3][3],front[3][3];

struct Puzzle{
	Dice dice[3][3][3];
	bool check[3][3][3];
	int num;
};

vector<Puzzle> V;
vector<int> ANS;

void copyPuzzle(Puzzle& to,Puzzle from){

	for(int a = 0; a < 3; a++){
		for(int b = 0; b < 3; b++){
			for(int c = 0; c < 3; c++){
				to.dice[a][b][c].set(from.dice[a][b][c]);
				to.check[a][b][c] = from.check[a][b][c];
			}
		}
	}
}

bool adj_check(Puzzle puzzle,int height,int row,int col){

	if(rangeCheck(height+1,row,col) == true && puzzle.check[height+1][row][col] == true && puzzle.dice[height][row][col].getTop()+puzzle.dice[height+1][row][col].getBottom() != 7)return false;

	if(rangeCheck(height,row,col+1) == true && puzzle.check[height][row][col+1] == true && puzzle.dice[height][row][col].getEast()+puzzle.dice[height][row][col+1].getWest()!= 7)return false;

	if(rangeCheck(height,row,col-1) == true && puzzle.check[height][row][col-1] == true && puzzle.dice[height][row][col].getWest()+puzzle.dice[height][row][col-1].getEast()!= 7)return false;

	if(rangeCheck(height,row+1,col) == true && puzzle.check[height][row+1][col] == true && puzzle.dice[height][row][col].getSouth()+puzzle.dice[height][row+1][col].getNorth()!= 7)return false;

	if(rangeCheck(height,row-1,col) == true && puzzle.check[height][row-1][col] == true && puzzle.dice[height][row][col].getNorth()+puzzle.dice[height][row-1][col].getSouth()!= 7)return false;

	if(rangeCheck(height-1,row,col) == true && puzzle.check[height-1][row][col] == true && puzzle.dice[height][row][col].getBottom()+puzzle.dice[height-1][row][col].getTop()!= 7)return false;

	return true;
}

void recursive(Puzzle puzzle);

void func(Puzzle puzzle,int height,int row,int col){
	for(int i = 0; i < 24; i++){
		Puzzle new_puzzle;
		copyPuzzle(new_puzzle,puzzle);
		new_puzzle.dice[height][row][col].setPos(i);
		if(!adj_check(new_puzzle,height,row,col))continue;
		new_puzzle.check[height][row][col] = true;
		new_puzzle.num = puzzle.num+1;
		recursive(new_puzzle);
	}
}

void recursive(Puzzle puzzle){

	if(puzzle.num == 27){
		V.push_back(puzzle);
		return;
	}

	switch(puzzle.num){
	case 0:
		func(puzzle,2,1,1);
		break;
	case 1:
		func(puzzle,2,0,1);
		break;
	case 2:
		func(puzzle,2,1,2);
		break;
	case 3:
		func(puzzle,2,2,1);
		break;
	case 4:
		func(puzzle,2,1,0);
		break;
	case 5:
		func(puzzle,2,0,0);
		break;
	case 6:
		func(puzzle,2,0,2);
		break;
	case 7:
		func(puzzle,2,2,2);
		break;
	case 8:
		func(puzzle,2,2,0);
		break;
	case 9:
		func(puzzle,1,1,1);
		break;
	case 10:
		func(puzzle,1,0,1);
		break;
	case 11:
		func(puzzle,1,1,2);
		break;
	case 12:
		func(puzzle,1,2,1);
		break;
	case 13:
		func(puzzle,1,1,0);
		break;
	case 14:
		func(puzzle,1,0,0);
		break;
	case 15:
		func(puzzle,1,0,2);
		break;
	case 16:
		func(puzzle,1,2,2);
		break;
	case 17:
		func(puzzle,1,2,0);
		break;
	case 18:
		func(puzzle,0,1,1);
		break;
	case 19:
		func(puzzle,0,0,1);
		break;
	case 20:
		func(puzzle,0,1,2);
		break;
	case 21:
		func(puzzle,0,2,1);
		break;
	case 22:
		func(puzzle,0,1,0);
		break;
	case 23:
		func(puzzle,0,0,0);
		break;
	case 24:
		func(puzzle,0,0,2);
		break;
	case 25:
		func(puzzle,0,2,2);
		break;
	case 26:
		func(puzzle,0,2,0);
		break;
	}

}

void calc(){

	ANS.clear();

	for(int row = 0; row < 3; row++){
		for(int col = 0; col < 3; col++)scanf("%d",&top[row][col]);
	}

	for(int row = 0; row < 3; row++){
		for(int col = 0; col < 3; col++)scanf("%d",&front[row][col]);
	}

	bool FLG;

	for(int i = 0; i < V.size(); i++){

		FLG = true;
		for(int row = 0; row < 3; row++){
			for(int col = 0; col < 3; col++){
				if(top[row][col] != 0 && V[i].dice[2][row][col].getTop() != top[row][col]){
					FLG = false;
					break;
				}
			}
			if(!FLG)break;
		}
		if(!FLG)continue;

		if((front[0][0] != 0 && V[i].dice[2][2][0].getSouth() != front[0][0]) ||
				(front[0][1] != 0 && V[i].dice[2][2][1].getSouth() != front[0][1]) ||
				(front[0][2] != 0 && V[i].dice[2][2][2].getSouth() != front[0][2]) ||
				(front[1][0] != 0 && V[i].dice[1][2][0].getSouth() != front[1][0]) ||
				(front[1][1] != 0 && V[i].dice[1][2][1].getSouth() != front[1][1]) ||
				(front[1][2] != 0 && V[i].dice[1][2][2].getSouth() != front[1][2]) ||
				(front[2][0] != 0 && V[i].dice[0][2][0].getSouth() != front[2][0]) ||
				(front[2][1] != 0 && V[i].dice[0][2][1].getSouth() != front[2][1]) ||
				(front[2][2] != 0 && V[i].dice[0][2][2].getSouth() != front[2][2])
		)continue;

		int sum = 0;
		for(int h = 0; h <= 2; h++){
			for(int row = 0; row <= 2; row++){
				sum += V[i].dice[h][row][2].getEast();
			}
		}
		ANS.push_back(sum);
	}

	if(ANS.size() == 0){
		printf("0\n");
		return;
	}

	sort(ANS.begin(),ANS.end());

	printf("%d",ANS[0]);
	int pre = ANS[0];

	for(int i = 1; i < ANS.size(); i++){
		if(ANS[i] != pre){
			printf(" %d",ANS[i]);
		}
		pre = ANS[i];
	}
	printf("\n");
}



int main(){

	Puzzle first;
	first.num = 0;

	for(int a = 0; a < 3; a++){
		for(int b = 0; b < 3; b++){
			for(int c = 0; c < 3; c++)first.check[a][b][c] = false;
		}
	}

	recursive(first);

	int N;
	scanf("%d",&N);

	for(int i = 0; i < N; i++)calc();

	return 0;
}

Measured memory_bytes: 24088