// Arup Guha
// 4/3/2020
// Solution to 2020 Code Jam Qualification Round Question E: Indicium Small Data Only
// Written in Contest

import java.util.*;

public class esmall {

	public static int n;
	public static int trace;
	public static int[][] g;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=1; loop<=nC; loop++) {
		
			n = stdin.nextInt();
			trace = stdin.nextInt();
			g = new int[n][n];
			
			// Backtrack it...
			boolean res = go(0);
			
			// It worked so print.
			if (res) {
				System.out.println("Case #"+loop+": POSSIBLE");
				for (int i=0; i<n; i++) {
					System.out.print(g[i][0]+1);
					for (int j=1; j<n; j++)
						System.out.print(" "+(g[i][j]+1));
					System.out.println();
				}
			}
			
			// Otherwise it didn't.
			else {
				System.out.println("Case #"+loop+": IMPOSSIBLE");
			}
		}
	}
	
	// Solve given that the first k slots are fixed.
	public static boolean go(int k) {
	
		// See if the trace adds up.
		if (k == n*n) return valid();
		
		// Keep track of which numbers have been used in this row and column already.
		boolean[] used = new boolean[n];
		int r = k/n;
		int c = k%n;
		
		// Mark all of these as used.
		for (int i=0; i<r; i++) used[g[i][c]] = true;
		for (int i=0; i<c; i++) used[g[r][i]] = true;
		
		// Try each number  in this slot.
		for (int i=0; i<n; i++) {
			
			// We used this already.
			if (used[i]) continue;
			
			// Place it.
			g[r][c] = i;
			
			// See if this works.
			boolean tmp = go(k+1);
			
			// If it does, return true.
			if (tmp) return true;
		}
	
		// This path didn't work.
		return false;
	}
	
	// Returns true if the trace is the right sum.
	public static boolean valid() {
		int s = 0;
		for (int i=0; i<n; i++) s += g[i][i];
		return s+n == trace;
	}
}