import java.io.FileInputStream;
import java.io.PrintWriter;
import java.util.Scanner;

class WaterSheds {
	static PrintWriter out;
	static int t, h, w, kq;
	static int[][] m;
	static boolean[][] mdd;
	static char[][] mn;
	static char ch;
	static boolean timThayChim;

	static void Nhap() throws Exception {
		Scanner in = new Scanner(new FileInputStream("BMax.In"));
		out = new PrintWriter("WaterSheds.Out");
		t = in.nextInt();
		for (int i = 0; i < t; i++) {
//			System.out.println("===========Test thu " + (i + 1));
			h = in.nextInt();
			w = in.nextInt();
			m = new int[h][w];
			mn = new char[h][w];
			mdd = new boolean[h][w];
			// Arrays.fill(mn, 'a');
			mn[0][0] = 'a';
			for (int j = 0; j < m.length; j++) {
				for (int k = 0; k < m[j].length; k++) {
					m[j][k] = in.nextInt();
				}
			}
			// -----------
			XuLy();
			 GhiFile(i+1);
		}
		out.close();
		in.close();
	}

	static boolean DiLen(int dong) {
		if (dong >= 1) {
			return true;
		} else {
			return false;
		}
	}

	static boolean DiXuong(int dong) {
		if (dong < h - 1) {
			return true;
		} else {
			return false;
		}
	}

	static boolean DiTrai(int cot) {
		if (cot >= 1) {
			return true;
		} else {
			return false;
		}
	}

	static boolean DiPhai(int cot) {
		if (cot < w - 1) {
			return true;
		} else {
			return false;
		}
	}
	static char Chay(int d, int c) {
//		System.out.println("-----------------------Chay vao:" + d + "|" + c + "|" + ch);
		if (mdd[d][c]) {
			return mn[d][c];
		}
		mdd[d][c] = true;
		int tren = Integer.MAX_VALUE, duoi = Integer.MAX_VALUE, trai = Integer.MAX_VALUE, phai = Integer.MAX_VALUE, min = Integer.MAX_VALUE;
		int huong = 0;
		if (DiLen(d)) {// Bac
			tren = m[d - 1][c];
			min = tren;
			huong = 1;
		}
//		System.out.println("-----------Min len=" + min);
		if (DiTrai(c)) {// Tay
			trai = m[d][c - 1];
			if (min > trai) {
				min = trai;
				huong = 2;
			}
		}
//		System.out.println("-----------Min trai=" + min);
		if (DiPhai(c)) {// Dong
			phai = m[d][c + 1];
			if (min > phai) {
				min = phai;
				huong = 3;
			}
		}
//		System.out.println("-----------Min phai=" + min);
		if (DiXuong(d)) {// Nam
			duoi = m[d + 1][c];
			if (min > duoi) {
				min = duoi;
				huong = 4;
			}
		}
//		System.out.println("-----------Min xuong=" + min);
//		System.out.println(m[d][c] + "||" +  min);
		if (m[d][c] > min) {
//			m[d][c] = ch;
//			System.out.println("Huong =" + huong);
			switch (huong) {
			case 1://Bac
				mn[d][c]= Chay(d-1,c);
//				System.out.println("----------------------------gan: " + mn[d][c]);
				return mn[d][c];
			case 2://Tay
				mn[d][c]=Chay(d, c-1);
//				System.out.println("-----------------------------gan: " + mn[d][c]);
				return mn[d][c];
			case 3://Dong
				mn[d][c]= Chay(d, c+1);
//				System.out.println("-----------------------------gan: " + mn[d][c]);
				return mn[d][c];
			default://Nam
				mn[d][c]= Chay(d+1, c);
//				System.out.println("----------gan: " + mn[d][c]);
				return mn[d][c];
			}
		}else{
			mn[d][c] = ch;
			timThayChim = true;
//			System.out.println("---------------------------------gan: " + mn[d][c]);
			return ch;
		}
	}

	static void XuLy() {
		ch = 'a';
		for (int i = 0; i < m.length; i++) {
			for (int j = 0; j < m[i].length; j++) {
				if (!mdd[i][j]) {
					timThayChim = false;
//					System.out.println("Xuat phat chay tu:" + i + "|" + j);
//					mdd[i][j] = true;
					Chay(i,j);
					if (timThayChim) {
						ch++;
					}
				}
			}
//			System.out.println();
		}
		// arrCellChim = new ArrayList<Cell>();
		// boolean kl,kx,kt,kp;
		// for (int i = 0; i < m.length; i++) {
		// for (int j = 0; j < m[i].length; j++) {
		// kl = DiLen(i);
		// if (kl) {
		// if (m[i][j]<=m[i-1][j]) {//Kiem di len
		// kl = false;
		// }else{
		// continue;
		// }
		// }
		// kx = DiXuong(i);
		// if (kx) {
		// if (m[i][j]<=m[i+1][j]) {//Kiem di xuong
		// kx = false;
		// }else{
		// continue;
		// }
		// }
		// kt = DiTrai(j);
		// if (kt) {
		// if (m[i][j]<=m[i][j-1]) {//Kiem di trai
		// kt = false;
		// }else{
		// continue;
		// }
		// }
		// kp = DiPhai(j);
		// if (kp) {
		// if (m[i][j]<=m[i][j+1]) {//Kiem di phai
		// kp = false;
		// }else{
		// continue;
		// }
		// }
		// //Chay duo den day tu la cell chim
		// if(!kl &&!kx && !kt && !kp){
		// arrCellChim.add(new Cell(i,j));
		// }
		// }
		// }
		// for (Cell x : arrCellChim) {
		// System.out.println("O chim " + x.hang + "|" + x.cot);
		// }
	}

	static void GhiFile(int th) {
		out.println("Case #" + th + ":");
//		System.out.println("Case #" + th + ":");
		for (int i = 0; i < mn.length; i++) {
			for (int j = 0; j < mn[i].length; j++) {
				if (j < mn[i].length - 1) {
//					System.out.print(mn[i][j] + " ");
					out.print(mn[i][j] + " ");
				} else {
					out.print(mn[i][j]);
					if (th==t && i>=mn.length-1) {
					}else{
						out.println();
					}
//					System.out.println(mn[i][j]);
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {
		double st = System.currentTimeMillis();
		Nhap();
		System.out.println("Terminated lost "
				+ (System.currentTimeMillis() - st) / 1000 + "s");
	}
}
