// ecole polytechnique 2011, c.durr + s.oudot
// english comments 2012

/* The class ExactCover implements the backtracking algorithm called dancing links.
   We are given a universe of elements 0,1,...,universe-1.
   We have sets over these elements.
   The ExactCover problem consists in selecing a collection of these sets that partition the universe.
   The algorithm is exponential in the worst case.

   It is illustrated on the example of solving a 16*16 Sudoku.

   http://www.spoj.pl/problems/SUDOKU/   
*/


import java.util.*;


/* we reduce sudoku to exact cover
   http://www.spoj.pl/problems/SUDOKU/   
*/
class Main {

    /* an assignment is an integer a which codes a row, a column, a
     * block and a value. Assignments are the sets of the exact cover
     * instance.
     */
    static int row(int a) {return a/16/16;                }
    static int col(int a) {return (a/16)%16;              }
    static int blk(int a) {return (row(a)/4)*4 + col(a)/4;}
    static int val(int a) {return a%16;                   }

    /* a constraint is a couple, either row,column or row,value or
     * column,value or block,value. THe constraints form the universe
     * of the exactc cover instance
     */
    static int rc(int a)  {return           row(a)*16+col(a);}
    static int rv(int a)  {return   16*16 + row(a)*16+val(a);}
    static int cv(int a)  {return 2*16*16 + col(a)*16+val(a);}
    static int bv(int a)  {return 3*16*16 + blk(a)*16+val(a);}

    public static void main(String args[]) {
        char[] M = new char[256];
        
        Scanner in = new Scanner(System.in);
        int testCases = in.nextInt();
        while (testCases-->0) {
            // --- reads the grid
            for (int i=0; i<16; i++) {    
                String w = in.next();
                for (int j=0; j<16; j++) {
                    M[16*i+j] = w.charAt(j);
                }
            }

            // --- reduce to exact cover
            ExactCover e = new ExactCover(4*16*16);
            
            for (int a=0; a<16*16*16; a++) {
                e.add(a, rc(a));
                e.add(a, rv(a));
                e.add(a, cv(a));
                e.add(a, bv(a));
            }

            // encode initial assignements
            for (int p=0; p<16*16; p++)
                if (M[p] != '-') {
                    int a = 16*p + M[p]-'A';
                    e.cover(rc(a));
                    e.cover(rv(a));
                    e.cover(cv(a));
                    e.cover(bv(a));
		}

            if (!e.solve()) 
                System.err.println("** no solution");
            // --- extract the solution
            for (int a : e.solution)
		M[a/16] = (char)('A' + a%16);
            
            // --- show the solution
            for (int p=0; p<256; p++) {
                System.out.print(M[p]);
                if (p%16==15) 
                    System.out.println();
            }
            if (testCases >0)
                System.out.println();
        }
    }
}
