@SuppressWarnings("unchecked")
	void run(){
		Dice<Integer> d = new Dice<Integer>(1, 6, 2, 5, 3, 4);
		dice = new Dice[24];
		int id = 0;
		for(Dice<Integer> x:d.getAllState())dice[id++] = x;
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T--!=0){
			top = new int[3][3]; front = new int[3][3];
			for(int i=0;i<3;i++)for(int j=0;j<3;j++)top[i][j] = sc.nextInt();
			for(int i=0;i<3;i++)for(int j=0;j<3;j++)front[i][j] = sc.nextInt();
			a = new Dice[3][3][3];
			res = new HashSet<Integer>();
			dfs(0, 0, 0);
			if(res.isEmpty())System.out.println(0);
			else{
				PriorityQueue<Integer> q = new PriorityQueue<Integer>();
				for(int x:res)q.add(x);
				System.out.print(q.poll());
				while(!q.isEmpty())System.out.print(" "+q.poll());
				System.out.println();
			}
		}
	}