/* Problem 3--Jigsaw Puzzle
   This program runs surprisingly quickly.  It assembles the grid in a
   trial-and-error fashion.  However, it stops assembling and backs up a
   piece whenever a piece does not fit.  In other words, this solution
   implements a depth-first-search strategy.  As soon as a solution is
   found, the program stops searching since we know the solution is
   unique, if it exists. */

import java.io.*;
import java.util.*;

public class prob3 {

 public static Scanner in;
 public static PrintStream out;
 public static int cs;
 public static Piece[][] Puzzle = new Piece[3][3],
                         solved = new Piece[3][3];

 public static void main (String[] args) throws Exception {

  in = new Scanner (new File ("prob3.in"));
  out = new PrintStream (new FileOutputStream ("prob3.out"));
  int cases = in.nextInt (); //Number of input cases
  for (int i=0; i < cases; i++) {
   GetPuzzle (); //Read in the puzzle
   try {
    solve (0); //Try to assemble it
    out.println ("No Solution");
   } catch (FoundIt e) {}
   out.println ();
  }
 }

 /* GetPuzzle reads in the puzzle from the input file. */
 public static void GetPuzzle () {

  for (int i=0; i < 3; i++)
   for (int j=0; j < 3; j++)
    Puzzle[i][j] = new Piece (3*i+j+1);
 }

 /* solve recursively tries to put a piece into slot n and build the
    rest of the puzzle from there. */
 public static void solve (int n) throws FoundIt {

  if (n==0) { //Very first piece
   out.println ("Case "+(++cs));
   out.println ();
  }
  int r = n/3, c = n%3;
  for (int i=0; i < 3; i++)
   for (int j=0; j < 3; j++) {
    if (Puzzle[i][j].used) continue; //already using that piece
    try {if (Puzzle[i][j].N != solved[r-1][c].S) continue;
    } catch (ArrayIndexOutOfBoundsException e) {} //see if piece fits
    try {if (Puzzle[i][j].W != solved[r][c-1].E) continue;
    } catch (ArrayIndexOutOfBoundsException e) {}
    solved[r][c] = Puzzle[i][j]; //add piece
    Puzzle[i][j].used = true;
    if (n==8) {PrintSoln();throw new FoundIt();}  //done?
    else solve (n+1);
    Puzzle[i][j].used = false; //this piece didn't work
   }
 }

 /* PrintSoln prints the final solution */
 public static void PrintSoln () {

  for (int i=0; i < 3; i++) {
   for (int j=0; j < 3; j++)
    out.print (solved[i][j].id+" ");
   out.println ();
  }
 }
}

/* class Piece represents the individual piece of the puzzle */
class Piece {

 public int N, E, S, W;
 public boolean used=false;
 public int id;

 /* constructor reads a piece from the input file */
 public Piece(int id) {

  N = prob3.in.nextInt ();
  E = prob3.in.nextInt ();
  S = prob3.in.nextInt ();
  W = prob3.in.nextInt ();
  this.id = id;
 }
}

/* class FoundIt is thrown when a solution is found */
class FoundIt extends Throwable {};
