import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class b {
    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        int T = fs.nextInt();

        try {
            for (int tt = 0; tt < T; tt++) {
                int n = fs.nextInt();
                ArrayList<Integer> p = new ArrayList<>();
                p=primes();
                int[][] matrix = new int[n][n];
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        matrix[i][j]=1;

                    }
                }

                for(int i=0;i<n;i++) {
                    for (int j = 0; j < n; j++) {

                        if (i == j) {

                            if (!(p.contains(n))) {
                                int sum = n - 1;
//                                System.out.println(sum);
                                for (int k = 4; k <= 100; k++) {
                                    if (p.contains(sum + k) && !(p.contains(k))) {
//                                        System.out.println(k);
                                        matrix[i][j] = k;

                                        System.out.print(matrix[i][j] + " ");
                                        break;
                                    }

                                }

                           continue; }

                            System.out.print(matrix[i][j] + " ");
                            continue;
                        }
                        System.out.print(matrix[i][j] + " ");
                    }
                        System.out.println();
                    }

            }
        } catch (Exception e) {
            return;
        }

    }

    static ArrayList<Integer> primes() {
        ArrayList<Integer> prime = new ArrayList<>();
        int n = 1000;
        boolean[] primes = new boolean[n + 1];
        for (int k = 0; k <= n; k++)
            primes[k] = true;

        for (int p = 2; p * p <= n; p++) {
            if (primes[p] == true) {

                for (int j = p * p; j <= n; j += p) {
                    primes[j] = false;

                }
            }
        }

        for (int i = 2; i <= n; i++) {
            if (primes[i] == true) {
                prime.add(i);

            }
        }
    return  prime;}

    static void sort(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class b {
    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        int T = fs.nextInt();

        try {
            for (int tt = 0; tt < T; tt++) {
                int n = fs.nextInt();
                ArrayList<Integer> p = new ArrayList<>();
                p=primes();
                int[][] matrix = new int[n][n];
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        matrix[i][j]=1;

                    }
                }

                for(int i=0;i<n;i++) {
                    for (int j = 0; j < n; j++) {

                        if (i == j) {

                            if (!(p.contains(n))) {
                                int sum = n - 1;
//                                System.out.println(sum);
                                for (int k = 4; k <= 100; k++) {
                                    if (p.contains(sum + k) && !(p.contains(k))) {
                                        matrix[i][j] = k;

                                        System.out.print(matrix[i][j] + " ");
                                        break;
                                    }

                                }

                           continue; }

                            System.out.print(matrix[i][j] + " ");
                            continue;
                        }
                        System.out.print(matrix[i][j] + " ");
                    }
                        System.out.println();
                    }

            }
        } catch (Exception e) {
            return;
        }

    }

    static ArrayList<Integer> primes() {
        ArrayList<Integer> prime = new ArrayList<>();
        int n = 100;
        boolean[] primes = new boolean[n + 1];
        for (int k = 0; k <= n; k++)
            primes[k] = true;

        for (int p = 2; p * p <= n; p++) {
            if (primes[p] == true) {

                for (int j = p * p; j <= n; j += p) {
                    primes[j] = false;

                }
            }
        }

        for (int i = 2; i <= n; i++) {
            if (primes[i] == true) {
                prime.add(i);

            }
        }
    return  prime;}

    static void sort(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

}


import java.util.*;
public class B678 {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        while(t-- > 0){
            int n = scan.nextInt();
            int big = (isPrime(n) - n) + 1;
            while(isPrimeB(big) == true){
                int cur = big;
                big = isPrime(cur) - n + 1;
            }
            int a [] = new int [n];
            for(int i = 0; i < n; i++){
                a[i] = 1;
            }
            for(int i = 0; i < n; i++){
                a[i] = big;
                for(int j = 0; j < n; j++){
                    System.out.print(a[j] + " ");
                }
                System.out.println();
                a[i] = 1;
            }

        }
    }
    static int isPrime(int a){
        boolean prime = false;
        int it = a;
        while(!prime){
            int count = 0;
            if(it == 1){
                prime = false;
                count++;
            }
            for (int i = 2; i < it; i++){
                if (it % i == 0){
                    prime = false;
                    count++;
                    break;
                }
            }
            if(count == 0){
                prime = true;
                break;
            }
            it++;
        }
        return it;
    }
    static boolean isPrimeB(int b){
        boolean prime = true;
        int it = b;
        if(it == 1){
            prime = false;
        }
        for (int i = 2; i < it; i++){
            if (it % i == 0){
                prime = false;
                break;
            }
        }
        return prime;
    }
}


import java.util.*;
public class MyClass {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        while(t-- > 0){
            int n = scan.nextInt();

            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    if(i == n - 1 && (j == 0 || j == n - 1)){
                        System.out.print(1 + " ");
                    }
                    else if(i == j || i + 1 == j){
                        System.out.print(1 + " ");
                    }
                    else System.out.print(0 + " ");
                }
                System.out.println();
            }

        }
    }
}


//package programs;

import java.util.*;

import static java.lang.System.out;
import static java.lang.Math.*;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main
{

	static int mod = 1000000007;
	static int MOD = 1000000007;
	static final long M = (int)1e9+7;

	static class Pair {
		int first;
		int second;

		public Pair(int first, int second) {
			this.first = first;
			this.second = second;
		}
	}

	static class Reader
	{
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader()
		{
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException
		{
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String next() throws IOException
		{
			byte[] buf = new byte[64]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1)
			{
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException
		{
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do
			{
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}
		int[] readArray(int n) throws IOException {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

		public long nextLong() throws IOException
		{
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException
		{
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');

			if (c == '.')
			{
				while ((c = read()) >= '0' && c <= '9')
				{
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException
		{
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException
		{
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException
		{
			if (din == null)
				return;
			din.close();
		}

	}

	public static boolean isPrime(int n) {
		if(n == 1)
		{
			return false;
		}
		for(int i = 2;i*i<=n;i++)
		{
			if(n%i == 0)
			{
				return false;
			}
		}
		return true;
	}

	public static int gcd(int a, int b)
	{
		if(b == 0)
		 return a;

		else
		return gcd(b,a%b);
	}

	public static int[] computePrefix(int arr[], int n)
	{
		int[] prefix = new int[n];
		prefix[0] = arr[0];
		for(int i = 1;i<n;i++)
		{
			prefix[i] = prefix[i-1]+arr[i];
		}

		return prefix;
	}

	public static int lcm(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }

	public static int phi(int n)  //euler totient function
    {
        int result = 1;
        for (int i = 2; i < n; i++)
            if (gcd(i, n) == 1)
                result++;
        return result;
    }

	public static int fastPow(int x, int n) //include mod at each step if asked and in args of fn too
	{
		if(n == 0)
			return 1;
		else if(n%2 == 0)
			return fastPow(x*x,n/2);
		else
			return x*fastPow(x*x,(n-1)/2);
	}

	static int LowerBound(int a[], int x) {
		  int l=-1,r=a.length;
		  while(l+1<r) {
		    int m=(l+r)>>>1;
		    if(a[m]>=x) r=m;
		    else l=m;
		  }
		  return r;
		}

	public static void Sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static int UpperBound(int a[], int x) {
	    int l=-1,r=a.length;
	    while(l+1<r) {
	       int m=(l+r)>>>1;
	       if(a[m]<=x) l=m;
	       else r=m;
	    }
	    return l+1;
	 }

	public static long LongGCD(long a, long b)
	{
		if(b == 0)
			 return a;

			else
			return LongGCD(b,a%b);
	}

	public static void main(String[] args) throws IOException
	{
		Reader sc = new Reader();

		int t = sc.nextInt();

		while(t-- > 0)
		{
			int n = sc.nextInt();
			int[][] arr = new int[n][n];

			for(int i = 0;i<n;i++)
			{
				for(int j = 0;j<n;j++)
				{
					if(i == j)
					{
						arr[i][j] = 6;
					}
					else if(i == n-1-j)
					{
						arr[i][j] = 8;
					}
					else {
						arr[i][j] = 0;
					}
				}
			}

			if(n%2 == 1)
			{
				int mid = n/2;
				arr[mid][mid] = 6;
				arr[0][n-1] = 4;
				arr[n-1][0] = 4;
				arr[0][mid] = 4;
				arr[n-1][mid] = 4;
				arr[mid][0] = 4;
				arr[mid][n-1] = 4;
			}

			for(int i = 0;i<n;i++)
			{
				for(int j = 0;j<n;j++)
				{
					System.out.print(arr[i][j] + " ");
				}

				out.println();
			}

		}
	}
}


import java.util.*;

import static java.lang.System.out;
import static java.lang.Math.*;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main
{

	static int mod = 1000000007;
	static int MOD = 1000000007;
	static final long M = (int)1e9+7;

	static class Pair {
		int first;
		int second;

		public Pair(int first, int second) {
			this.first = first;
			this.second = second;
		}
	}

	static class Reader
	{
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader()
		{
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException
		{
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String next() throws IOException
		{
			byte[] buf = new byte[64]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1)
			{
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException
		{
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do
			{
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}
		int[] readArray(int n) throws IOException {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

		public long nextLong() throws IOException
		{
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException
		{
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');

			if (c == '.')
			{
				while ((c = read()) >= '0' && c <= '9')
				{
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException
		{
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException
		{
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException
		{
			if (din == null)
				return;
			din.close();
		}

	}

	public static boolean isPrime(int n) {
		if(n == 1)
		{
			return false;
		}
		for(int i = 2;i*i<=n;i++)
		{
			if(n%i == 0)
			{
				return false;
			}
		}
		return true;
	}

	public static int gcd(int a, int b)
	{
		if(b == 0)
		 return a;

		else
		return gcd(b,a%b);
	}

	public static int[] computePrefix(int arr[], int n)
	{
		int[] prefix = new int[n];
		prefix[0] = arr[0];
		for(int i = 1;i<n;i++)
		{
			prefix[i] = prefix[i-1]+arr[i];
		}

		return prefix;
	}

	public static int lcm(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }

	public static int phi(int n)  //euler totient function
    {
        int result = 1;
        for (int i = 2; i < n; i++)
            if (gcd(i, n) == 1)
                result++;
        return result;
    }

	public static int fastPow(int x, int n) //include mod at each step if asked and in args of fn too
	{
		if(n == 0)
			return 1;
		else if(n%2 == 0)
			return fastPow(x*x,n/2);
		else
			return x*fastPow(x*x,(n-1)/2);
	}

	static int LowerBound(int a[], int x) {
		  int l=-1,r=a.length;
		  while(l+1<r) {
		    int m=(l+r)>>>1;
		    if(a[m]>=x) r=m;
		    else l=m;
		  }
		  return r;
		}

	public static void Sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static int UpperBound(int a[], int x) {
	    int l=-1,r=a.length;
	    while(l+1<r) {
	       int m=(l+r)>>>1;
	       if(a[m]<=x) l=m;
	       else r=m;
	    }
	    return l+1;
	 }

	public static long LongGCD(long a, long b)
	{
		if(b == 0)
			 return a;

			else
			return LongGCD(b,a%b);
	}

	public static void main(String[] args) throws IOException
	{
		Reader sc = new Reader();

		int t = sc.nextInt();

		while(t-- > 0)
		{
			int n = sc.nextInt();
			int[][] arr = new int[n][n];

			for(int i = 0;i<n;i++)
			{
				for(int j = 0;j<n;j++)
				{
					if(i == j)
					{
						arr[i][j] = 15;
					}
					else if(i == n-1-j)
					{
						arr[i][j] = 8;
					}
					else {
						arr[i][j] = 0;
					}
				}
			}

			if(n%2 == 1)
			{
				int mid = n/2;
				arr[mid][mid] = 15;
				arr[0][n-1] = 4;
				arr[n-1][0] = 4;
				arr[0][mid] = 4;
				arr[n-1][mid] = 4;
				arr[mid][0] = 4;
				arr[mid][n-1] = 4;
			}

			for(int i = 0;i<n;i++)
			{
				for(int j = 0;j<n;j++)
				{
					System.out.print(arr[i][j] + " ");
				}

				out.println();
			}

		}
	}
}


import java.util.*;

public class letters{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int T = s.nextInt();
        for(int t= 0;t<T;t++){
            int n = s.nextInt();
            genSquare(n);;
        }
    }
    public static void genSquare(int n){
        int[][] s = new int[n][n];
        int posNf = 0;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                if(j==posNf||j==n-1-posNf){
                    s[i][j] = 1;
                }else{
                    s[i][j] = 0;
                }
            }
            posNf++;
        }
        if(n%2!=0){
            s[(n)/2][n-1]=1;
            s[n-1][(n/2)]=1;

        }
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                System.out.print(s[i][j]+" ");
            }
            System.out.println();
        }
    }
}


import java.util.*;

public class letters{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int T = s.nextInt();
        for(int t= 0;t<T;t++){
            int n = s.nextInt();
            genSquare(n);;
        }
    }
    public static void genSquare(int n){
        int[][] s = new int[n][n];
        int posNf = 0;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                if(j==posNf||j==n-1-posNf){
                    s[i][j] = 1;
                }else{
                    s[i][j] = 0;
                }
            }
            posNf++;
        }
        if(n%2!=0){
            s[(n)/2][n-1]=1;
            s[n-1][(n/2)]=1;

        }
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                System.out.print(s[i][j]);
            }
            System.out.println();
        }
    }
}


import java.util.*;

public class letters{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int T = s.nextInt();
        for(int t= 0;t<T;t++){
            int n = s.nextInt();
            genSquare(n);;
        }
    }
    public static void genSquare(int n){
        int[][] s = new int[n][n];
        int posNf = 0;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                if(j==posNf||j==n-1-posNf){
                    s[i][j] = 1;
                }else{
                    s[i][j] = 0;
                }
            }
            posNf++;
        }
        if(n%2!=0){
            s[(n)/2][n-1]=1;
            s[n-1][(n/2)]=1;

        }
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                System.out.print(s[i][j]);
            }
            System.out.println();
        }
    }


import java.io.*;
import java.math.*;
import java.util.*;

public class test {
	static boolean prime[] = new boolean[101];

	public static void main(String[] args) {
		sieve();
		int test = fs.nextInt();
//		int test = 1;
		for (int cases = 0; cases < test; cases++) {
			int n = fs.nextInt();
			int ar[][] = new int[n][n];
			if (ispowerof2(n)) {
				int x = 4;
				int z = (n - 1);
				while (!prime[z + x]) {
					x += 2;
				}
				for (int i = 0; i < n; i++) {
					if (i < n - 1) {
						for (int j = 0; j < n; j++) {
							if (j != n - 1) {
								ar[i][j] = 1;
							} else {
								ar[i][j] = x;
							}
						}
					} else {
						for (int j = 0; j < n; j++) {
							if (j != n - 1) {
								ar[i][j] = x;
							} else {
								ar[i][j] = 1;
							}
						}
					}
				}
			} else if (prime[n]) {
				for (int i = 0; i < n; i++) {
					for (int j = 0; j < n; j++) {
						ar[i][j] = 1;
					}
				}
			} else {
				int cp[] = new int[n];
				cp[0] = 1;
				int x = 4;
				for (int i = 1; i < n; i++) {
					cp[i] = x;
					x += 2;
				}
				for (int i = 0; i < n; i++) {
					int c = i;
					for (int j = 0; j < n; j++) {
						ar[i][j] = cp[c % n];
						++c;
					}
				}

			}
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					op.print(ar[i][j] + " ");
				}
				op.print("\n");
			}
			op.flush();
		}

	}

	static void sieve() {
		Arrays.fill(prime, true);
		prime[1] = false;
		for (int i = 2; i * i <= prime.length - 1; i++) {
			for (int j = i * i; j <= prime.length - 1; j += i) {
				prime[j] = false;
			}
		}
	}

	public static int[] radixSort(int[] f) {
		return radixSort(f, f.length);
	}

	public static int[] radixSort(int[] f, int n) {
		int[] to = new int[n];
		{
			int[] b = new int[65537];
			for (int i = 0; i < n; i++)
				b[1 + (f[i] & 0xffff)]++;
			for (int i = 1; i <= 65536; i++)
				b[i] += b[i - 1];
			for (int i = 0; i < n; i++)
				to[b[f[i] & 0xffff]++] = f[i];
			int[] d = f;
			f = to;
			to = d;
		}
		{
			int[] b = new int[65537];
			for (int i = 0; i < n; i++)
				b[1 + (f[i] >>> 16)]++;
			for (int i = 1; i <= 65536; i++)
				b[i] += b[i - 1];
			for (int i = 0; i < n; i++)
				to[b[f[i] >>> 16]++] = f[i];
			int[] d = f;
			f = to;
			to = d;
		}
		return f;
	}

	static void printArray(int ar[]) {
		Arrays.toString(ar);
	}

	static long expmodulo(long a, long b, long c) {
		long x = 1;
		long y = a;
		while (b > 0) {
			if (b % 2 == 1) {
				x = (x * y) % c;
			}
			y = (y * y) % c; // squaring the base
			b /= 2;
		}
		return (long) x % c;
	}

	static int modInverse(int a, int m) {
		int m0 = m;
		int y = 0, x = 1;

		if (m == 1)
			return 0;

		while (a > 1) {
			int q = a / m;
			int t = m;
			m = a % m;
			a = t;
			t = y;
			y = x - q * y;
			x = t;
		}
		if (x < 0)
			x += m0;
		return x;
	}

	static int gcd(int a, int b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static void sortMyMapusingValues(HashMap<String, Integer> hm) {

		List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());

		Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
			public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
				return (o1.getValue()).compareTo(o2.getValue());
			}
		});
		HashMap<String, Integer> result = new HashMap<>();
		for (Map.Entry<String, Integer> entry : capitalList) {
			result.put(entry.getKey(), entry.getValue());
		}
	}

	static boolean ispowerof2(long num) {
		if ((num & (num - 1)) == 0)
			return true;
		return false;
	}

	static void primeFactors(int n) {
		while (n % 2 == 0) {
			System.out.print(2 + " ");
			n /= 2;
		}
		for (int i = 3; i <= Math.sqrt(n); i += 2) {
			while (n % i == 0) {
				System.out.print(i + " ");
				n /= i;
			}
		}
		if (n > 2)
			System.out.print(n);
	}

	static boolean isPrime(long n) {
		// Corner cases
		if (n <= 1)
			return false;
		if (n <= 3)
			return true;
		if (n % 2 == 0 || n % 3 == 0)
			return false;

		long sq = (long) Math.sqrt(n);
		for (int i = 5; i <= sq; i = i + 6)
			if (n % i == 0 || n % (i + 2) == 0)
				return false;

		return true;
	}

	static class Graph {
		HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();

		private void addVertex(int vertex) {
			hm.put(vertex, new LinkedList<>());
		}

		private void addEdge(int source, int dest, boolean bi) {
			if (!hm.containsKey(source))
				addVertex(source);
			if (!hm.containsKey(dest))
				addVertex(dest);
			hm.get(source).add(dest);
			if (bi) {
				hm.get(dest).add(source);
			}
		}

		private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
			visited.add(i);
			LinkedList<Integer> list = hm.get(i);
			Iterator<Integer> it = list.iterator();
			while (it.hasNext()) {
				Integer integer = (Integer) it.next();
				if (!visited.contains(integer)) {
					if (uniCycle(integer, visited, i))
						return true;
				} else if (integer != parent) {
					return true;
				}
			}
			return false;
		}

		private boolean uniCyclic() {
			HashSet<Integer> visited = new HashSet<Integer>();
			Set<Integer> set = hm.keySet();
			for (Integer integer : set) {
				if (!visited.contains(integer)) {
					if (uniCycle(integer, visited, -1)) {
						return true;
					}
				}
			}
			return false;
		}

		private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
			if (countered.contains(i))
				return true;
			if (visited.contains(i))
				return false;
			visited.add(i);
			countered.add(i);
			LinkedList<Integer> list = hm.get(i);
			Iterator<Integer> it = list.iterator();
			while (it.hasNext()) {
				Integer integer = (Integer) it.next();
				if (isbiCycle(integer, visited, countered)) {
					return true;
				}
			}
			countered.remove(i);
			return false;
		}

		Boolean isReachable(int s, int d, int k) {
			if (hm.isEmpty()) {
				return false;
			}
			LinkedList<Integer> temp;
			boolean visited[] = new boolean[k];
			LinkedList<Integer> queue = new LinkedList<Integer>();
			visited[s] = true;
			queue.add(s);
			Iterator<Integer> i;
			while (queue.size() != 0) {
				s = queue.poll();
				int n;
				i = hm.get(s).listIterator();

				// Get all adjacent vertices of the dequeued vertex s
				// If a adjacent has not been visited, then mark it
				// visited and enqueue it
				while (i.hasNext()) {
					n = i.next();

					// If this adjacent node is the destination node,
					// then return true
					if (n == d)
						return true;

					// Else, continue to do BFS
					if (!visited[n]) {
						visited[n] = true;
						queue.add(n);
					}
				}
			}

			// If BFS is complete without visited d
			return false;
		}

		private boolean isbiCyclic() {
			HashSet<Integer> visited = new HashSet<Integer>();
			HashSet<Integer> countered = new HashSet<Integer>();
			Set<Integer> set = hm.keySet();
			for (Integer integer : set) {
				if (isbiCycle(integer, visited, countered)) {
					return true;
				}
			}
			return false;
		}
	}

	static class Node {
		Node left, right;
		int data;

		public Node(int data) {
			this.data = data;
		}

		public void insert(int val) {
			if (val <= data) {
				if (left == null) {
					left = new Node(val);
				} else {
					left.insert(val);
				}
			} else {
				if (right == null) {
					right = new Node(val);
				} else {
					right.insert(val);
				}
			}
		}

		public boolean contains(int val) {
			if (data == val) {
				return true;
			} else if (val < data) {
				if (left == null) {
					return false;
				} else {
					return left.contains(val);
				}
			} else {
				if (right == null) {
					return false;
				} else {
					return right.contains(val);
				}
			}
		}

		public void inorder() {
			if (left != null) {
				left.inorder();
			}
			System.out.print(data + " ");
			if (right != null) {
				right.inorder();
			}
		}

		public int maxDepth() {
			if (left == null)
				return 0;
			if (right == null)
				return 0;
			else {
				int ll = left.maxDepth();
				int rr = right.maxDepth();
				if (ll > rr)
					return (ll + 1);
				else
					return (rr + 1);
			}
		}

		public int countNodes() {
			if (left == null)
				return 1;
			if (right == null)
				return 1;
			else {
				return left.countNodes() + right.countNodes() + 1;
			}
		}

		public void preorder() {
			System.out.print(data + " ");
			if (left != null) {
				left.inorder();
			}

			if (right != null) {
				right.inorder();
			}

		}

		public void postorder() {
			if (left != null) {
				left.inorder();
			}

			if (right != null) {
				right.inorder();
			}
			System.out.print(data + " ");

		}

		public void levelorder(Node node) {
			LinkedList<Node> ll = new LinkedList<Node>();
			ll.add(node);
			getorder(ll);

		}

		public void getorder(LinkedList<Node> ll) {
			while (!ll.isEmpty()) {
				Node node = ll.poll();
				System.out.print(node.data + " ");
				if (node.left != null)
					ll.add(node.left);
				if (node.right != null)
					ll.add(node.right);
			}
		}
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	static class Pair {
		int first, second;

		public Pair(int first, int second) {
			this.first = first;
			this.second = second;
		}

	}

	static class Pair2 {
		int first;
		Pair second;

		public Pair2(int first, Pair second) {
			this.first = first;
			this.second = second;
		}

	}

	static class LongPair {
		long first, second;

		public LongPair(long first, long second) {
			this.first = first;
			this.second = second;
		}

	}

	static class OutputWriter {
		private final PrintWriter writer;

		public OutputWriter(OutputStream outputStream) {
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
		}

		public OutputWriter(Writer writer) {
			this.writer = new PrintWriter(writer);
		}

		public void print(Object... objects) {
			for (int i = 0; i < objects.length; i++) {
				if (i != 0)
					writer.print(' ');
				writer.print(objects[i]);
			}
		}

		public void printLine(Object... objects) {
			print(objects);
			writer.println();
		}

		public void close() {
			writer.close();
		}

		public void flush() {
			writer.flush();
		}

	}

	private static final FastReader fs = new FastReader();
	private static final OutputWriter op = new OutputWriter(System.out);
}


import java.io.*;
import java.math.*;
import java.util.*;

public class test {
	static int INF = 1000000009;

	public static void main(String[] args) {
		int test = fs.nextInt();
//		int test = 1;
		for (int cases = 0; cases < test; cases++) {
			int n = fs.nextInt();
			int a = 1;
			int b = 0;
			int ar[][] = new int[n][n];
			if (n % 2 == 0) {
				for (int i = 0; i < n; i++) {
					for (int j = 0; j < n; j++) {
						if (i == j || i == n - j - 1) {
							op.print(1 + " ");
						} else {
							op.print(0 + " ");
						}
					}
					op.print("\n");
				}
			} else {
				for (int i = 0; i < n; i++) {
					for (int j = 0; j < n; j++) {
						if (i == n / 2 && (j == n / 2 - 1 || j == n / 2 + 1)) {
							op.print(1 + " ");
						}
						else if(j== n / 2 && (i == n / 2 - 1 || i== n / 2 + 1)) {
							op.print(1 + " ");
						}
						else if (i == j || i == n - j - 1) {
							op.print(1 + " ");
						} else {
							op.print(0 + " ");
						}
					}
					op.print("\n");
				}
			}
			op.flush();
		}

	}

	static void sieve() {
		boolean prime[] = new boolean[101];
		Arrays.fill(prime, true);
		prime[1] = false;
		for (int i = 2; i * i <= prime.length - 1; i++) {
			for (int j = i * i; j <= prime.length - 1; j += i) {
				prime[j] = false;
			}
		}
	}

	public static int[] radixSort(int[] f) {
		return radixSort(f, f.length);
	}

	public static int[] radixSort(int[] f, int n) {
		int[] to = new int[n];
		{
			int[] b = new int[65537];
			for (int i = 0; i < n; i++)
				b[1 + (f[i] & 0xffff)]++;
			for (int i = 1; i <= 65536; i++)
				b[i] += b[i - 1];
			for (int i = 0; i < n; i++)
				to[b[f[i] & 0xffff]++] = f[i];
			int[] d = f;
			f = to;
			to = d;
		}
		{
			int[] b = new int[65537];
			for (int i = 0; i < n; i++)
				b[1 + (f[i] >>> 16)]++;
			for (int i = 1; i <= 65536; i++)
				b[i] += b[i - 1];
			for (int i = 0; i < n; i++)
				to[b[f[i] >>> 16]++] = f[i];
			int[] d = f;
			f = to;
			to = d;
		}
		return f;
	}

	static void printArray(int ar[]) {
		Arrays.toString(ar);
	}

	static long expmodulo(long a, long b, long c) {
		long x = 1;
		long y = a;
		while (b > 0) {
			if (b % 2 == 1) {
				x = (x * y) % c;
			}
			y = (y * y) % c; // squaring the base
			b /= 2;
		}
		return (long) x % c;
	}

	static int modInverse(int a, int m) {
		int m0 = m;
		int y = 0, x = 1;

		if (m == 1)
			return 0;

		while (a > 1) {
			int q = a / m;
			int t = m;
			m = a % m;
			a = t;
			t = y;
			y = x - q * y;
			x = t;
		}
		if (x < 0)
			x += m0;
		return x;
	}

	static int gcd(int a, int b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static void sortMyMapusingValues(HashMap<String, Integer> hm) {

		List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());

		Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
			public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
				return (o1.getValue()).compareTo(o2.getValue());
			}
		});
		HashMap<String, Integer> result = new HashMap<>();
		for (Map.Entry<String, Integer> entry : capitalList) {
			result.put(entry.getKey(), entry.getValue());
		}
	}

	static boolean ispowerof2(long num) {
		if ((num & (num - 1)) == 0)
			return true;
		return false;
	}

	static void primeFactors(int n) {
		while (n % 2 == 0) {
			System.out.print(2 + " ");
			n /= 2;
		}
		for (int i = 3; i <= Math.sqrt(n); i += 2) {
			while (n % i == 0) {
				System.out.print(i + " ");
				n /= i;
			}
		}
		if (n > 2)
			System.out.print(n);
	}

	static boolean isPrime(long n) {
		// Corner cases
		if (n <= 1)
			return false;
		if (n <= 3)
			return true;
		if (n % 2 == 0 || n % 3 == 0)
			return false;

		long sq = (long) Math.sqrt(n);
		for (int i = 5; i <= sq; i = i + 6)
			if (n % i == 0 || n % (i + 2) == 0)
				return false;

		return true;
	}

	static class Graph {
		HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();

		private void addVertex(int vertex) {
			hm.put(vertex, new LinkedList<>());
		}

		private void addEdge(int source, int dest, boolean bi) {
			if (!hm.containsKey(source))
				addVertex(source);
			if (!hm.containsKey(dest))
				addVertex(dest);
			hm.get(source).add(dest);
			if (bi) {
				hm.get(dest).add(source);
			}
		}

		private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
			visited.add(i);
			LinkedList<Integer> list = hm.get(i);
			Iterator<Integer> it = list.iterator();
			while (it.hasNext()) {
				Integer integer = (Integer) it.next();
				if (!visited.contains(integer)) {
					if (uniCycle(integer, visited, i))
						return true;
				} else if (integer != parent) {
					return true;
				}
			}
			return false;
		}

		private boolean uniCyclic() {
			HashSet<Integer> visited = new HashSet<Integer>();
			Set<Integer> set = hm.keySet();
			for (Integer integer : set) {
				if (!visited.contains(integer)) {
					if (uniCycle(integer, visited, -1)) {
						return true;
					}
				}
			}
			return false;
		}

		private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
			if (countered.contains(i))
				return true;
			if (visited.contains(i))
				return false;
			visited.add(i);
			countered.add(i);
			LinkedList<Integer> list = hm.get(i);
			Iterator<Integer> it = list.iterator();
			while (it.hasNext()) {
				Integer integer = (Integer) it.next();
				if (isbiCycle(integer, visited, countered)) {
					return true;
				}
			}
			countered.remove(i);
			return false;
		}

		Boolean isReachable(int s, int d, int k) {
			if (hm.isEmpty()) {
				return false;
			}
			LinkedList<Integer> temp;
			boolean visited[] = new boolean[k];
			LinkedList<Integer> queue = new LinkedList<Integer>();
			visited[s] = true;
			queue.add(s);
			Iterator<Integer> i;
			while (queue.size() != 0) {
				s = queue.poll();
				int n;
				i = hm.get(s).listIterator();

				// Get all adjacent vertices of the dequeued vertex s
				// If a adjacent has not been visited, then mark it
				// visited and enqueue it
				while (i.hasNext()) {
					n = i.next();

					// If this adjacent node is the destination node,
					// then return true
					if (n == d)
						return true;

					// Else, continue to do BFS
					if (!visited[n]) {
						visited[n] = true;
						queue.add(n);
					}
				}
			}

			// If BFS is complete without visited d
			return false;
		}

		private boolean isbiCyclic() {
			HashSet<Integer> visited = new HashSet<Integer>();
			HashSet<Integer> countered = new HashSet<Integer>();
			Set<Integer> set = hm.keySet();
			for (Integer integer : set) {
				if (isbiCycle(integer, visited, countered)) {
					return true;
				}
			}
			return false;
		}
	}

	static class Node {
		Node left, right;
		int data;

		public Node(int data) {
			this.data = data;
		}

		public void insert(int val) {
			if (val <= data) {
				if (left == null) {
					left = new Node(val);
				} else {
					left.insert(val);
				}
			} else {
				if (right == null) {
					right = new Node(val);
				} else {
					right.insert(val);
				}
			}
		}

		public boolean contains(int val) {
			if (data == val) {
				return true;
			} else if (val < data) {
				if (left == null) {
					return false;
				} else {
					return left.contains(val);
				}
			} else {
				if (right == null) {
					return false;
				} else {
					return right.contains(val);
				}
			}
		}

		public void inorder() {
			if (left != null) {
				left.inorder();
			}
			System.out.print(data + " ");
			if (right != null) {
				right.inorder();
			}
		}

		public int maxDepth() {
			if (left == null)
				return 0;
			if (right == null)
				return 0;
			else {
				int ll = left.maxDepth();
				int rr = right.maxDepth();
				if (ll > rr)
					return (ll + 1);
				else
					return (rr + 1);
			}
		}

		public int countNodes() {
			if (left == null)
				return 1;
			if (right == null)
				return 1;
			else {
				return left.countNodes() + right.countNodes() + 1;
			}
		}

		public void preorder() {
			System.out.print(data + " ");
			if (left != null) {
				left.inorder();
			}

			if (right != null) {
				right.inorder();
			}

		}

		public void postorder() {
			if (left != null) {
				left.inorder();
			}

			if (right != null) {
				right.inorder();
			}
			System.out.print(data + " ");

		}

		public void levelorder(Node node) {
			LinkedList<Node> ll = new LinkedList<Node>();
			ll.add(node);
			getorder(ll);

		}

		public void getorder(LinkedList<Node> ll) {
			while (!ll.isEmpty()) {
				Node node = ll.poll();
				System.out.print(node.data + " ");
				if (node.left != null)
					ll.add(node.left);
				if (node.right != null)
					ll.add(node.right);
			}
		}
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	static class Pair {
		int first, second;

		public Pair(int first, int second) {
			this.first = first;
			this.second = second;
		}

	}

	static class Pair2 {
		int first;
		Pair second;

		public Pair2(int first, Pair second) {
			this.first = first;
			this.second = second;
		}

	}

	static class LongPair {
		long first, second;

		public LongPair(long first, long second) {
			this.first = first;
			this.second = second;
		}

	}

	static class OutputWriter {
		private final PrintWriter writer;

		public OutputWriter(OutputStream outputStream) {
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
		}

		public OutputWriter(Writer writer) {
			this.writer = new PrintWriter(writer);
		}

		public void print(Object... objects) {
			for (int i = 0; i < objects.length; i++) {
				if (i != 0)
					writer.print(' ');
				writer.print(objects[i]);
			}
		}

		public void printLine(Object... objects) {
			print(objects);
			writer.println();
		}

		public void close() {
			writer.close();
		}

		public void flush() {
			writer.flush();
		}

	}

	private static final FastReader fs = new FastReader();
	private static final OutputWriter op = new OutputWriter(System.out);
}


import java.io.*;
import java.math.*;
import java.util.*;

public class test {
	static int INF = 1000000009;

	public static void main(String[] args) {
		int test = fs.nextInt();
//		int test = 1;
		for (int cases = 0; cases < test; cases++) {
			int n = fs.nextInt();
			int a = 1;
			int b = 0;
			int ar[][] = new int[n][n];
			if (n % 2 == 0) {
				for (int i = 0; i < n; i++) {
					for (int j = 0; j < n; j++) {
						if (i == j || i == n - j - 1) {
							op.print(1 + " ");
						} else {
							op.print(0 + " ");
						}
					}
					op.print("\n");
				}
			} else {
				for (int i = 0; i < n; i++) {
					for (int j = 0; j < n; j++) {
						if (i == n / 2 && (j == n / 2 - 1 || j == n / 2 + 1)) {
							op.print(1 + " ");
						} else if (i == j || i == n - j - 1) {
							op.print(1 + " ");
						} else {
							op.print(0 + " ");
						}
					}
					op.print("\n");
				}
			}
			op.flush();
		}

	}

	static void sieve() {
		boolean prime[] = new boolean[101];
		Arrays.fill(prime, true);
		prime[1] = false;
		for (int i = 2; i * i <= prime.length - 1; i++) {
			for (int j = i * i; j <= prime.length - 1; j += i) {
				prime[j] = false;
			}
		}
	}

	public static int[] radixSort(int[] f) {
		return radixSort(f, f.length);
	}

	public static int[] radixSort(int[] f, int n) {
		int[] to = new int[n];
		{
			int[] b = new int[65537];
			for (int i = 0; i < n; i++)
				b[1 + (f[i] & 0xffff)]++;
			for (int i = 1; i <= 65536; i++)
				b[i] += b[i - 1];
			for (int i = 0; i < n; i++)
				to[b[f[i] & 0xffff]++] = f[i];
			int[] d = f;
			f = to;
			to = d;
		}
		{
			int[] b = new int[65537];
			for (int i = 0; i < n; i++)
				b[1 + (f[i] >>> 16)]++;
			for (int i = 1; i <= 65536; i++)
				b[i] += b[i - 1];
			for (int i = 0; i < n; i++)
				to[b[f[i] >>> 16]++] = f[i];
			int[] d = f;
			f = to;
			to = d;
		}
		return f;
	}

	static void printArray(int ar[]) {
		Arrays.toString(ar);
	}

	static long expmodulo(long a, long b, long c) {
		long x = 1;
		long y = a;
		while (b > 0) {
			if (b % 2 == 1) {
				x = (x * y) % c;
			}
			y = (y * y) % c; // squaring the base
			b /= 2;
		}
		return (long) x % c;
	}

	static int modInverse(int a, int m) {
		int m0 = m;
		int y = 0, x = 1;

		if (m == 1)
			return 0;

		while (a > 1) {
			int q = a / m;
			int t = m;
			m = a % m;
			a = t;
			t = y;
			y = x - q * y;
			x = t;
		}
		if (x < 0)
			x += m0;
		return x;
	}

	static int gcd(int a, int b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static void sortMyMapusingValues(HashMap<String, Integer> hm) {

		List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());

		Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
			public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
				return (o1.getValue()).compareTo(o2.getValue());
			}
		});
		HashMap<String, Integer> result = new HashMap<>();
		for (Map.Entry<String, Integer> entry : capitalList) {
			result.put(entry.getKey(), entry.getValue());
		}
	}

	static boolean ispowerof2(long num) {
		if ((num & (num - 1)) == 0)
			return true;
		return false;
	}

	static void primeFactors(int n) {
		while (n % 2 == 0) {
			System.out.print(2 + " ");
			n /= 2;
		}
		for (int i = 3; i <= Math.sqrt(n); i += 2) {
			while (n % i == 0) {
				System.out.print(i + " ");
				n /= i;
			}
		}
		if (n > 2)
			System.out.print(n);
	}

	static boolean isPrime(long n) {
		// Corner cases
		if (n <= 1)
			return false;
		if (n <= 3)
			return true;
		if (n % 2 == 0 || n % 3 == 0)
			return false;

		long sq = (long) Math.sqrt(n);
		for (int i = 5; i <= sq; i = i + 6)
			if (n % i == 0 || n % (i + 2) == 0)
				return false;

		return true;
	}

	static class Graph {
		HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();

		private void addVertex(int vertex) {
			hm.put(vertex, new LinkedList<>());
		}

		private void addEdge(int source, int dest, boolean bi) {
			if (!hm.containsKey(source))
				addVertex(source);
			if (!hm.containsKey(dest))
				addVertex(dest);
			hm.get(source).add(dest);
			if (bi) {
				hm.get(dest).add(source);
			}
		}

		private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
			visited.add(i);
			LinkedList<Integer> list = hm.get(i);
			Iterator<Integer> it = list.iterator();
			while (it.hasNext()) {
				Integer integer = (Integer) it.next();
				if (!visited.contains(integer)) {
					if (uniCycle(integer, visited, i))
						return true;
				} else if (integer != parent) {
					return true;
				}
			}
			return false;
		}

		private boolean uniCyclic() {
			HashSet<Integer> visited = new HashSet<Integer>();
			Set<Integer> set = hm.keySet();
			for (Integer integer : set) {
				if (!visited.contains(integer)) {
					if (uniCycle(integer, visited, -1)) {
						return true;
					}
				}
			}
			return false;
		}

		private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
			if (countered.contains(i))
				return true;
			if (visited.contains(i))
				return false;
			visited.add(i);
			countered.add(i);
			LinkedList<Integer> list = hm.get(i);
			Iterator<Integer> it = list.iterator();
			while (it.hasNext()) {
				Integer integer = (Integer) it.next();
				if (isbiCycle(integer, visited, countered)) {
					return true;
				}
			}
			countered.remove(i);
			return false;
		}

		Boolean isReachable(int s, int d, int k) {
			if (hm.isEmpty()) {
				return false;
			}
			LinkedList<Integer> temp;
			boolean visited[] = new boolean[k];
			LinkedList<Integer> queue = new LinkedList<Integer>();
			visited[s] = true;
			queue.add(s);
			Iterator<Integer> i;
			while (queue.size() != 0) {
				s = queue.poll();
				int n;
				i = hm.get(s).listIterator();

				// Get all adjacent vertices of the dequeued vertex s
				// If a adjacent has not been visited, then mark it
				// visited and enqueue it
				while (i.hasNext()) {
					n = i.next();

					// If this adjacent node is the destination node,
					// then return true
					if (n == d)
						return true;

					// Else, continue to do BFS
					if (!visited[n]) {
						visited[n] = true;
						queue.add(n);
					}
				}
			}

			// If BFS is complete without visited d
			return false;
		}

		private boolean isbiCyclic() {
			HashSet<Integer> visited = new HashSet<Integer>();
			HashSet<Integer> countered = new HashSet<Integer>();
			Set<Integer> set = hm.keySet();
			for (Integer integer : set) {
				if (isbiCycle(integer, visited, countered)) {
					return true;
				}
			}
			return false;
		}
	}

	static class Node {
		Node left, right;
		int data;

		public Node(int data) {
			this.data = data;
		}

		public void insert(int val) {
			if (val <= data) {
				if (left == null) {
					left = new Node(val);
				} else {
					left.insert(val);
				}
			} else {
				if (right == null) {
					right = new Node(val);
				} else {
					right.insert(val);
				}
			}
		}

		public boolean contains(int val) {
			if (data == val) {
				return true;
			} else if (val < data) {
				if (left == null) {
					return false;
				} else {
					return left.contains(val);
				}
			} else {
				if (right == null) {
					return false;
				} else {
					return right.contains(val);
				}
			}
		}

		public void inorder() {
			if (left != null) {
				left.inorder();
			}
			System.out.print(data + " ");
			if (right != null) {
				right.inorder();
			}
		}

		public int maxDepth() {
			if (left == null)
				return 0;
			if (right == null)
				return 0;
			else {
				int ll = left.maxDepth();
				int rr = right.maxDepth();
				if (ll > rr)
					return (ll + 1);
				else
					return (rr + 1);
			}
		}

		public int countNodes() {
			if (left == null)
				return 1;
			if (right == null)
				return 1;
			else {
				return left.countNodes() + right.countNodes() + 1;
			}
		}

		public void preorder() {
			System.out.print(data + " ");
			if (left != null) {
				left.inorder();
			}

			if (right != null) {
				right.inorder();
			}

		}

		public void postorder() {
			if (left != null) {
				left.inorder();
			}

			if (right != null) {
				right.inorder();
			}
			System.out.print(data + " ");

		}

		public void levelorder(Node node) {
			LinkedList<Node> ll = new LinkedList<Node>();
			ll.add(node);
			getorder(ll);

		}

		public void getorder(LinkedList<Node> ll) {
			while (!ll.isEmpty()) {
				Node node = ll.poll();
				System.out.print(node.data + " ");
				if (node.left != null)
					ll.add(node.left);
				if (node.right != null)
					ll.add(node.right);
			}
		}
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	static class Pair {
		int first, second;

		public Pair(int first, int second) {
			this.first = first;
			this.second = second;
		}

	}

	static class Pair2 {
		int first;
		Pair second;

		public Pair2(int first, Pair second) {
			this.first = first;
			this.second = second;
		}

	}

	static class LongPair {
		long first, second;

		public LongPair(long first, long second) {
			this.first = first;
			this.second = second;
		}

	}

	static class OutputWriter {
		private final PrintWriter writer;

		public OutputWriter(OutputStream outputStream) {
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
		}

		public OutputWriter(Writer writer) {
			this.writer = new PrintWriter(writer);
		}

		public void print(Object... objects) {
			for (int i = 0; i < objects.length; i++) {
				if (i != 0)
					writer.print(' ');
				writer.print(objects[i]);
			}
		}

		public void printLine(Object... objects) {
			print(objects);
			writer.println();
		}

		public void close() {
			writer.close();
		}

		public void flush() {
			writer.flush();
		}

	}

	private static final FastReader fs = new FastReader();
	private static final OutputWriter op = new OutputWriter(System.out);
}


//package cp;

import java.util.*;
import java.io.*;

public class Solution
{
	public static void main(String[] args)
	{
		FastScanner sc = new FastScanner();
		int t = sc.nextInt();
		while(t-- >0) {
			int n = sc.nextInt();
			int a[][] = new int[n][n];
			boolean f = true;
			if (n <= 1)
		        f =false;

		    else if (n == 2)
		    	f= true;

		    else if (n % 2 == 0)
		        f = false;

		    for(int i = 3; i <= Math.sqrt(n); i += 2)
		    {
		        if (n % i == 0)
		            f = false;
		    }

		    if(f == true) {
		    	for(int i=0 ; i<n ; i++) {
		    		for(int j=0 ; j<n ; j++) {
		    			a[i][j] = 1;
		    		}
		    	}
		    }
		    else {
		    	for(int i=0 ; i<n ; i++) {
		    		for(int j=0 ; j<n ; j++) {
		    			a[i][j] = 1;
		    		}
		    	}
		    	for(int i=0 ; i<n-1 ; i++) {
		    		a[i][0] = n;
		    	}
		    	for(int j=1 ; j<n ; j++) {
		    		a[n-1][j] = n;
		    	}
		    }

		    for(int i=0 ; i<n ; i++) {
		    	for(int j=0 ; j<n ; j++) {
		    		System.out.print(a[i][j]+" ");
		    	}
		    	System.out.println();
		    }
		}

	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int [] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


//package cp;

import java.util.*;
import java.io.*;

public class Solution
{
	public static void main(String[] args)
	{
		FastScanner sc = new FastScanner();
		int t = sc.nextInt();
		while(t-- >0) {
			int n = sc.nextInt();
			int a[][] = new int[n][n];

		    	for(int i=0 ; i<n ; i++) {
		    		for(int j=0 ; j<n ; j++) {
		    			if(i==j) {
		    				a[i][j] = 1;
		    			}
		    			if(i+j==n-1) {
		    				a[i][j]=1;
		    			}
		    		}
		    	}
		    	if(n%2!=0) {
		    		a[n/2][n/2]=0;
		    		a[n/2][n/2 - 1]=1;
		    		a[n/2][n/2 + 1]=1;
		    		a[n/2 - 1][n/2]=1;
		    		a[n/2 + 1][n/2]=1;
		    	}
		    for(int i=0 ; i<n ; i++) {
		    	for(int j=0 ; j<n ; j++) {
		    		System.out.print(a[i][j]+" ");
		    	}
		    	System.out.println();
		    }
		}

	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int [] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


//package cp;

import java.util.*;
import java.io.*;

public class Solution
{
	public static void main(String[] args)
	{
		FastScanner sc = new FastScanner();
		int t = sc.nextInt();
		while(t-- >0) {
			int n = sc.nextInt();
			int a[][] = new int[n][n];
			boolean f = true;
			if (n <= 1)
		        f =false;

		    else if (n == 2)
		    	f= true;

		    else if (n % 2 == 0)
		        f = false;

		    for(int i = 3; i <= Math.sqrt(n); i += 2)
		    {
		        if (n % i == 0)
		            f = false;
		    }

		    if(f == true) {
		    	for(int i=0 ; i<n ; i++) {
		    		for(int j=0 ; j<n ; j++) {
		    			a[i][j] = 1;
		    		}
		    	}
		    }
		    else {
		    	for(int i=0 ; i<n ; i++) {
		    		for(int j=0 ; j<n ; j++) {
		    			if(i==j) {
		    				a[i][j] = 1;
		    			}
		    			if(i+j==n-1) {
		    				a[i][j]=1;
		    			}
		    		}
		    	}
		    }

		    for(int i=0 ; i<n ; i++) {
		    	for(int j=0 ; j<n ; j++) {
		    		System.out.print(a[i][j]+" ");
		    	}
		    	System.out.println();
		    }
		}

	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int [] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.io.*;
import java.util.*;

public class Div678 {

    static class FastReader {

        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String args[]) {
        FastReader scn = new FastReader();
        int t = scn.nextInt();
        int c = 101;
        while (t-- > 0) {
            int n = scn.nextInt();
            boolean flag = isPrime(c - n + 1);
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < n; j++) {
                    if (i == j) {
                        if (flag) {
                            System.out.print(c - n + 1 - 3);
                            System.out.print(" ");
                            System.out.print("4 ");
                            j++;
                        } else {
                            System.out.print(c - n + 1);
                            System.out.print(" ");
                        }

                    } else {
                        System.out.print("1 ");
                    }
                }
                System.out.println();
            }
            if (flag) {
                System.out.print("4 ");
            } else {
                System.out.print("1 ");
            }
            for (int i = 1; i < n - 1; i++) {
                System.out.print("1 ");
            }
            if (flag) {
                System.out.print(c - n + 1 - 3);
            } else {
                System.out.print(c - n + 1);
            }
            System.out.println();
        }
    }

    public static boolean isPrime(int n) {

        if (n <= 1) {
            return false;
        }
        else if (n == 2) {
            return true;
        }
        else if (n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

}


import java.io.*;
import java.util.*;

public class Div678 {

    static class FastReader {

        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String args[]) {
        FastReader scn = new FastReader();
        int t = scn.nextInt();
        int c = 99991;
        while (t-- > 0) {
            int n = scn.nextInt();

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (i == j) {
                        System.out.print(c - n + 1);
                        System.out.print(" ");
                    } else {
                        System.out.print("1 ");
                    }
                }
                System.out.println();
            }
        }
    }

}


import java.io.*;
import java.util.*;

public class Div678 {

    static class FastReader {

        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String args[]) {
        FastReader scn = new FastReader();
        int t = scn.nextInt();
        int c = 127;
        while (t-- > 0) {
            int n = scn.nextInt();
            boolean flag = isPrime(c - n + 1);
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < n; j++) {
                    if (i == j) {
                        if (flag) {
                            System.out.print(c - n + 1 - 3);
                            System.out.print(" ");
                            System.out.print("4 ");
                            j++;
                        } else {
                            System.out.print(c - n + 1);
                            System.out.print(" ");
                        }

                    } else {
                        System.out.print("1 ");
                    }
                }
                System.out.println();
            }
            if (flag) {
                System.out.print("4 ");
            } else {
                System.out.print("1 ");
            }
            for (int i = 1; i < n - 1; i++) {
                System.out.print("1 ");
            }
            if (flag) {
                System.out.print(c - n + 1 - 3);
            } else {
                System.out.print(c - n + 1);
            }
            System.out.println();
        }
    }

    public static boolean isPrime(int n) {

        if (n <= 1) {
            return false;
        }
        else if (n == 2) {
            return true;
        }
        else if (n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

}


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import static java.lang.Math.sqrt;
public class B {
  int USE_FILE = 0;
  int HAS_TESTCASES = 1;
  String INPUT = "2\n" + "12\n" + "2\n" + "";
  void solve(int caseNr) {
    int n = in.nextInt();
    if (caseNr != 1) {
      return;
    }
    int[] primes = findPrimes();
    boolean[] isPrime = new boolean[10000];
    for (int p : primes) {
      isPrime[p] = true;
    }
    int diag = 1;
    int sum = n;
    while (!isPrime[sum] || isPrime[diag]) {
      sum++;
      diag++;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j) {
          System.out.print(diag + " ");
        } else {
          System.out.print(1 + " ");
        }
      }
      System.out.println();
    }
  }
  static int[] findPrimes() {
    int[] primes = new int[8363];
    int index = 0;
    for (int i = 2; i <= 10000; i++) {
      if (checkPrime(i)) {
        primes[index++] = i;
      }
    }
    return primes;
  }
  static boolean checkPrime(int num) {
    for (int i = 2; i <= Math.sqrt(num); i++) {
      if (num % i == 0) {
        return false;
      }
    }
    return true;
  }
  static boolean LOCAL = java.lang.System.getProperty("ONLINE_JUDGE") == null;
  static Scanner in;
  static PrintWriter output_writer = new PrintWriter(java.lang.System.out);
  static class System {
    static PrintWriter out = null;
  }
  void setup() throws Exception {
    System.out = output_writer;
    if (LOCAL && USE_FILE == 0) {
      java.lang.System.setIn(new ByteArrayInputStream(INPUT.getBytes()));
      in = new Scanner(java.lang.System.in);
    }
    if (LOCAL && USE_FILE > 0) {
      String filename = String.format("in/cf%d.txt", USE_FILE);
      File inputFile = new File(filename);
      in = new Scanner(inputFile);
      System.out.printf("[%s]\n", filename);
    }
    if (!LOCAL) {
      in = new Scanner(java.lang.System.in);
    }
    long startTime = java.lang.System.currentTimeMillis();
    int t = HAS_TESTCASES > 0 ? in.nextInt() : 1;
    for (int i = 1; i <= t; i++) {
      solve(i);
    }
    in.close();
    if (LOCAL) {
      System.out.printf("[%dms]\n", java.lang.System.currentTimeMillis() - startTime);
    }
    output_writer.flush();
  }
  public static void main(String[] args) throws Exception {
    new B().setup();
  }
}


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import static java.lang.Math.sqrt;
public class B {
  int USE_FILE = 0;
  int HAS_TESTCASES = 1;
  String INPUT = "2\n" + "12\n" + "2\n" + "";
  void solve(int caseNr) {
    int n = in.nextInt();
    int[] primes = findPrimes();
    boolean[] isPrime = new boolean[10000];
    for (int p : primes) {
      isPrime[p] = true;
    }
    int diag = 1;
    int sum = n;
    while (!isPrime[sum] || isPrime[diag]) {
      sum++;
      diag++;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j) {
          System.out.print(diag + " ");
        } else {
          System.out.print(1 + " ");
        }
      }
      System.out.println();
    }
  }
  static int[] findPrimes() {
    int[] primes = new int[8363];
    int index = 0;
    for (int i = 2; i <= 10000; i++) {
      if (checkPrime(i)) {
        primes[index++] = i;
      }
    }
    return primes;
  }
  static boolean checkPrime(int num) {
    for (int i = 2; i <= Math.sqrt(num); i++) {
      if (num % i == 0) {
        return false;
      }
    }
    return true;
  }
  static boolean LOCAL = java.lang.System.getProperty("ONLINE_JUDGE") == null;
  static Scanner in;
  static PrintWriter output_writer = new PrintWriter(java.lang.System.out);
  static class System {
    static PrintWriter out = null;
  }
  void setup() throws Exception {
    System.out = output_writer;
    if (LOCAL && USE_FILE == 0) {
      java.lang.System.setIn(new ByteArrayInputStream(INPUT.getBytes()));
      in = new Scanner(java.lang.System.in);
    }
    if (LOCAL && USE_FILE > 0) {
      String filename = String.format("in/cf%d.txt", USE_FILE);
      File inputFile = new File(filename);
      in = new Scanner(inputFile);
      System.out.printf("[%s]\n", filename);
    }
    if (!LOCAL) {
      in = new Scanner(java.lang.System.in);
    }
    long startTime = java.lang.System.currentTimeMillis();
    int t = HAS_TESTCASES > 0 ? in.nextInt() : 1;
    for (int i = 1; i <= t; i++) {
      solve(i);
    }
    in.close();
    if (LOCAL) {
      System.out.printf("[%dms]\n", java.lang.System.currentTimeMillis() - startTime);
    }
    output_writer.flush();
  }
  public static void main(String[] args) throws Exception {
    new B().setup();
  }
}


import java.util.*;
import java.io.*;
import java.math.*;

public class CP{

  public static OutputStream out=new BufferedOutputStream(System.out);
  static Scanner sc=new Scanner(System.in);
  static long MOD=998244353l;
  static int INF=10000000;
  static long[] fact;
  static long[] inv_fact;
  static int[] rr=new int[]{1, 0, -1, 0};                                 //rook row move
  static int[] rc=new int[]{0, 1, 0, -1};                                 //rook col move

  //nl-->neew line; //l-->line;  //arp-->array print;  //arpnl-->array print new line
  public static void nl(Object o) throws IOException{out.write((o+"\n").getBytes()); }
  public static void l(Object o) throws IOException{out.write((o+"").getBytes());}
  public static void arp(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+" ").getBytes()); out.write(("\n").getBytes());}
  public static void arpnl(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+"\n").getBytes());}
  public static void scanl(long[] a,int n) {for(int i=0;i<n;i++) a[i]=sc.nextLong();}
  public static void scani(int[] a,int n) {for(int i=0;i<n;i++) a[i]=sc.nextInt();}
  public static void scan2D(int[][] a,int n,int m) {for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=sc.nextInt();}
  //static variables

  //

  public static void main(String[] args) throws IOException{
    long sttm=System.currentTimeMillis();

    int t=1;
    t=sc.nextInt();
    while(t-->0) solve();

    out.flush();
  }

  public static void solve() throws IOException{
      int n=sc.nextInt(), m=sc.nextInt();
      int n=sc.nextInt();
      int[] a=new int[n];
      int pr=149;
      int sum=0;
      if((pr-n+1)%2!=0){
          a[0]=4;sum+=4;
      }
      else {a[0]=1;sum+=1;}
      for(int i=1;in-1;i++){
          a[]=1;
          sum++;
      }
      a[n-1]=pr-sum;
      for(int i=0;i<n;i++){
          for(int j0;j<n;j++){
              l(a[(i+j)%n]+" ");
          }
          nl("");
      }
  }

  static long gcd(long a, long b)
	{
	if (b == 0)
		return a;
	return gcd(b, a % b);
  }

}

class Pair{
  int x; int y;
  Pair(int x,int y){
    this.x=x;
    this.y=y;
  }
}


import java.util.*;
import java.io.*;
import java.math.*;

public class CP{

  public static OutputStream out=new BufferedOutputStream(System.out);
  static Scanner sc=new Scanner(System.in);
  static long MOD=998244353l;
  static int INF=10000000;
  static long[] fact;
  static long[] inv_fact;
  static int[] rr=new int[]{1, 0, -1, 0};                                 //rook row move
  static int[] rc=new int[]{0, 1, 0, -1};                                 //rook col move

  //nl-->neew line; //l-->line;  //arp-->array print;  //arpnl-->array print new line
  public static void nl(Object o) throws IOException{out.write((o+"\n").getBytes()); }
  public static void l(Object o) throws IOException{out.write((o+"").getBytes());}
  public static void arp(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+" ").getBytes()); out.write(("\n").getBytes());}
  public static void arpnl(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+"\n").getBytes());}
  public static void scanl(long[] a,int n) {for(int i=0;i<n;i++) a[i]=sc.nextLong();}
  public static void scani(int[] a,int n) {for(int i=0;i<n;i++) a[i]=sc.nextInt();}
  public static void scan2D(int[][] a,int n,int m) {for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=sc.nextInt();}
  //static variables

  //

  public static void main(String[] args) throws IOException{
    long sttm=System.currentTimeMillis();

    int t=1;
    t=sc.nextInt();
    while(t-->0) solve();

    out.flush();
  }

  public static void solve() throws IOException{
      // int n=sc.nextInt(), m=sc.nextInt();
      int n=sc.nextInt();
      int[] a=new int[n];
      int pr=149;
      int sum=0;
      if((pr-n+1)%2!=0){
          a[0]=4;sum+=4;
      }
      else {a[0]=1;sum+=1;}
      for(int i=1;i<n-1;i++){
          a[i]=1;
          sum++;
      }
      a[n-1]=pr-sum;
      for(int i=0;i<n;i++){
          for(int j=0;j<n;j++){
              l(a[(i+j)%n]+" ");
          }
          nl("");
      }
  }

  static long gcd(long a, long b)
	{
	if (b == 0)
		return a;
	return gcd(b, a % b);
  }

}

class Pair{
  int x; int y;
  Pair(int x,int y){
    this.x=x;
    this.y=y;
  }
}


import java.util.Scanner;

public class Gen {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		while(t-->0) {
			int n = scan.nextInt();

			for(int i =0;i<n;i++) {
				for(int j =0;j<n;j++) {
					if(i==j) {
						System.out.print(101+" ");
					}
					else
						System.out.print(0+" ");
				}
				System.out.println();
			}

		}

	}

}


import java.util.Scanner;

public class Gen {

	static boolean isPrime(int n) {
		for(int i=2;i<n;i++) {
			if(n%i==0) {
				return false;
			}
		}
		return true;
	}
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		while(t-->0) {
			int n = scan.nextInt();
			int primes[] = {101,103,107, 109,113,127,131, 137, 139, 149, 151, 157,163, 167, 173, 179, 181, 191, 193, 197, 199};

			int ans =101-n;
			for(int i = 0;i<primes.length;i++ ) {
				if(!isPrime(primes[i]-n+1)) {
					ans = primes[i]-n+1;
					break;
				}
			}
			for(int i =0;i<n;i++) {
				for(int j =0;j<n;j++) {
					if(i==j) {
						System.out.print(ans+" ");
					}
					else
						System.out.print(1+" ");
				}
				System.out.println();
			}

		}

	}

}


import java.lang.*;
import java.util.*;
import java.io.*;
// import java.io.BufferedReader;
// import java.io.IOException;
// import java.io.InputStreamReader;

 public class Main {

    // static class FastReader
    // {
    //     BufferedReader br;
    //     StringTokenizer st;

    //     public FastReader()
    //     {
    //         br = new BufferedReader(new
    //                  InputStreamReader(System.in));
    //     }

    //     String next()
    //     {
    //         while (st == null || !st.hasMoreElements())
    //         {
    //             try
    //             {
    //                 st = new StringTokenizer(br.readLine());
    //             }
    //             catch (IOException  e)
    //             {
    //                 e.printStackTrace();
    //             }
    //         }
    //         return st.nextToken();
    //     }

    //     int nextInt()
    //     {
    //         return Integer.parseInt(next());
    //     }

    //     long nextLong()
    //     {
    //         return Long.parseLong(next());
    //     }

    //     double nextDouble()
    //     {
    //         return Double.parseDouble(next());
    //     }

    //     String nextLine()
    //     {
    //         String str = "";
    //         try
    //         {
    //             str = br.readLine();
    //         }
    //         catch (IOException e)
    //         {
    //             e.printStackTrace();
    //         }
    //         return str;
    //     }
    // }

    public static void main(String[] args)throws java.lang.Exception {
        //  FastReader sc = new FastReader();
        Scanner sc=new Scanner(System.in);
         StringBuffer sb = new StringBuffer();
            ArrayList<Integer> al=new ArrayList<>();
        ArrayList<Integer> b=new ArrayList<>();
            // Set<Integer> x=new HashSet<>();
        //    Set<Integer> y=new HashSet<>();

        int t=sc.nextInt();
        while(t-->0)
        {
         int n=sc.nextInt();
        int[][] a=new int[n][n];
        int j=0;
        a[0][0]=6;
        a[0][n-1]=1;
        for(int i=0;(i<n-1) && (j<n-1);i++,j++)
        {
            a[i+1][j+1]=6;
        }
        j=n-1;
        for(int i=0;(i<n-1) && (j>0);i++,j--)
        {
            a[i+1][j-1]=1;
        }

        for(int i=0;i<n;i++)
        {
            for(int k=0;k<n;k++)
            {
                System.out.print(a[i][k]+" ");
            }
            System.out.println();
        }

        }

}

}


import java.lang.*;
import java.util.*;
import java.io.*;
// import java.io.BufferedReader;
// import java.io.IOException;
// import java.io.InputStreamReader;

 public class Main {

    // static class FastReader
    // {
    //     BufferedReader br;
    //     StringTokenizer st;

    //     public FastReader()
    //     {
    //         br = new BufferedReader(new
    //                  InputStreamReader(System.in));
    //     }

    //     String next()
    //     {
    //         while (st == null || !st.hasMoreElements())
    //         {
    //             try
    //             {
    //                 st = new StringTokenizer(br.readLine());
    //             }
    //             catch (IOException  e)
    //             {
    //                 e.printStackTrace();
    //             }
    //         }
    //         return st.nextToken();
    //     }

    //     int nextInt()
    //     {
    //         return Integer.parseInt(next());
    //     }

    //     long nextLong()
    //     {
    //         return Long.parseLong(next());
    //     }

    //     double nextDouble()
    //     {
    //         return Double.parseDouble(next());
    //     }

    //     String nextLine()
    //     {
    //         String str = "";
    //         try
    //         {
    //             str = br.readLine();
    //         }
    //         catch (IOException e)
    //         {
    //             e.printStackTrace();
    //         }
    //         return str;
    //     }
    // }

    public static void main(String[] args)throws java.lang.Exception {
        //  FastReader sc = new FastReader();
        Scanner sc=new Scanner(System.in);
         StringBuffer sb = new StringBuffer();
            ArrayList<Integer> al=new ArrayList<>();
        ArrayList<Integer> b=new ArrayList<>();
            // Set<Integer> x=new HashSet<>();
        //    Set<Integer> y=new HashSet<>();

        int t=sc.nextInt();
        while(t-->0)
        {
         int n=sc.nextInt();
        int[][] a=new int[n][n];
        int j=0;
        a[0][0]=7;
        for(int i=0;(i<n-1) && (j<n-1);i++,j++)
        {
            a[i+1][j+1]=7;
        }

        for(int i=0;i<n;i++)
        {
            for(int k=0;k<n;k++)
            {
                System.out.print(a[i][k]+" ");
            }
            System.out.println();
        }

        }

}

}


import java.lang.*;
import java.util.*;
import java.io.*;
// import java.io.BufferedReader;
// import java.io.IOException;
// import java.io.InputStreamReader;

 public class Main {

    // static class FastReader
    // {
    //     BufferedReader br;
    //     StringTokenizer st;

    //     public FastReader()
    //     {
    //         br = new BufferedReader(new
    //                  InputStreamReader(System.in));
    //     }

    //     String next()
    //     {
    //         while (st == null || !st.hasMoreElements())
    //         {
    //             try
    //             {
    //                 st = new StringTokenizer(br.readLine());
    //             }
    //             catch (IOException  e)
    //             {
    //                 e.printStackTrace();
    //             }
    //         }
    //         return st.nextToken();
    //     }

    //     int nextInt()
    //     {
    //         return Integer.parseInt(next());
    //     }

    //     long nextLong()
    //     {
    //         return Long.parseLong(next());
    //     }

    //     double nextDouble()
    //     {
    //         return Double.parseDouble(next());
    //     }

    //     String nextLine()
    //     {
    //         String str = "";
    //         try
    //         {
    //             str = br.readLine();
    //         }
    //         catch (IOException e)
    //         {
    //             e.printStackTrace();
    //         }
    //         return str;
    //     }
    // }

    public static void main(String[] args)throws java.lang.Exception {
        //  FastReader sc = new FastReader();
        Scanner sc=new Scanner(System.in);
         StringBuffer sb = new StringBuffer();
            ArrayList<Integer> al=new ArrayList<>();
        ArrayList<Integer> b=new ArrayList<>();
            // Set<Integer> x=new HashSet<>();
        //    Set<Integer> y=new HashSet<>();

        int t=sc.nextInt();
        while(t-->0)
        {
         int n=sc.nextInt();
        int[][] a=new int[n][n];
        int j=0;
        a[0][0]=6;
        a[0][n-1]=1;
        for(int i=0;(i<n-1) && (j<n-1);i++,j++)
        {
            a[i+1][j+1]=6;
        }
        j=n-1;
        for(int i=0;(i<n-1) && (j>0);i++,j--)
        {
            a[i+1][j-1]=1;
        }

        if((n&1)==1)
       { a[n/2][n-1]=4;
        a[n-1][n/2]=4;
    }

        for(int i=0;i<n;i++)
        {
            for(int k=0;k<n;k++)
            {
                System.out.print(a[i][k]+" ");
            }
            System.out.println();
        }

        }

}

}


import java.lang.*;
import java.util.*;
import java.io.*;
// import java.io.BufferedReader;
// import java.io.IOException;
// import java.io.InputStreamReader;

 public class Main {

    // static class FastReader
    // {
    //     BufferedReader br;
    //     StringTokenizer st;

    //     public FastReader()
    //     {
    //         br = new BufferedReader(new
    //                  InputStreamReader(System.in));
    //     }

    //     String next()
    //     {
    //         while (st == null || !st.hasMoreElements())
    //         {
    //             try
    //             {
    //                 st = new StringTokenizer(br.readLine());
    //             }
    //             catch (IOException  e)
    //             {
    //                 e.printStackTrace();
    //             }
    //         }
    //         return st.nextToken();
    //     }

    //     int nextInt()
    //     {
    //         return Integer.parseInt(next());
    //     }

    //     long nextLong()
    //     {
    //         return Long.parseLong(next());
    //     }

    //     double nextDouble()
    //     {
    //         return Double.parseDouble(next());
    //     }

    //     String nextLine()
    //     {
    //         String str = "";
    //         try
    //         {
    //             str = br.readLine();
    //         }
    //         catch (IOException e)
    //         {
    //             e.printStackTrace();
    //         }
    //         return str;
    //     }
    // }

    public static void main(String[] args)throws java.lang.Exception {
        //  FastReader sc = new FastReader();
        Scanner sc=new Scanner(System.in);
         StringBuffer sb = new StringBuffer();
            ArrayList<Integer> al=new ArrayList<>();
        ArrayList<Integer> b=new ArrayList<>();
            // Set<Integer> x=new HashSet<>();
        //    Set<Integer> y=new HashSet<>();

        int t=sc.nextInt();
        while(t-->0)
        {
         int n=sc.nextInt();
        int[][] a=new int[n][n];
        int j=0;
        a[0][0]=6;
        a[0][n-1]=1;
        for(int i=0;(i<n-1) && (j<n-1);i++,j++)
        {
            a[i+1][j+1]=6;
        }
        j=n-1;
        for(int i=0;(i<n-1) && (j>0);i++,j--)
        {
            a[i+1][j-1]=1;
        }

        if((n&1)==1)
        a[n/2][n-1]=4;

        for(int i=0;i<n;i++)
        {
            for(int k=0;k<n;k++)
            {
                System.out.print(a[i][k]+" ");
            }
            System.out.println();
        }

        }

}

}


import java.util.Arrays;
import java.util.Scanner;

public class CF678B {
  public static void solve(Scanner sc, int caseNum) {
    int n = sc.nextInt();
    int P = 127;
    int a = 0;
    int b = 0;

    if (n % 2 == 0) {
      a = 1;
    } else {
      a = 4;
    }

    b = P - (n - 2 + a);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (j > 0) {
          System.out.printf(" ");
        }

        int v = 1;
        if (i == j) v = a;
        else if ((i + 1) % n == j) v = b;
        System.out.printf("%d", v);
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    for (int k = 0; k < t; k++) {
      solve(sc, k + 1);
    }
  }
}


import java.util.Arrays;
import java.util.Scanner;

public class CF678B {
  public static void solve(Scanner sc, int caseNum) {
    int n = sc.nextInt();
    int P = 127;
    int a = 0;
    int b = 0;

    if (n % 2 == 0) {
      a = 1;
    } else {
      a = 4;
    }

    b = P - (n - 2 - a);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (j > 0) {
          System.out.printf(" ");
        }

        int v = 1;
        if (i == j) v = a;
        else if ((i + 1) % n == j) v = b;
        System.out.printf("%d", v);
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    for (int k = 0; k < t; k++) {
      solve(sc, k + 1);
    }
  }
}


import java.util.*;
public class Solution{
	 public static int M=(int)(1e9)+7;
     public static void main(String args[])
     {
      Scanner take=new Scanner(System.in);
      int t=take.nextInt();
      int i,j;
      while(t--!=0)
      {	  int n=take.nextInt();
    	  int a[]=new int [n];
    	  for(i=0;i<(n-1);i++) a[i]=1;
    	  for(int k=1;;k++)
    	  {
    		  if(isPrime((k+n-1))&&isPrime(k)!=true)
    			  {
    			  a[n-1]=k;
    			  break;
    			  }
    	  }
    	  for(j=0;j<n;j++)
    	  {  int k=n;
    		  for(i=j;k--!=0;i++)
    		  {
    		  System.out.print(a[i%n]+" ");

    		  }
    	  System.out.println();
    	  }
      }
     }
     static boolean isPrime(int n)
     {
         // Corner cases
         if (n <= 1) return false;
         if (n <= 3) return true;

         // This is checked so that we can skip
         // middle five numbers in below loop
         if (n % 2 == 0 || n % 3 == 0) return false;

         for (int i = 5; i * i <= n; i = i + 6)
             if (n % i == 0 || n % (i + 2) == 0)
             return false;

         return true;
     }
}


import java.util.*;
public class Solution{
	 public static int M=(int)(1e9)+7;
     public static void main(String args[])
     {
      Scanner take=new Scanner(System.in);
      int t=take.nextInt();
      int i,j;
      while(t--!=0)
      {	  int n=take.nextInt();
    	  int a[]=new int [n];
    	  a[0]=1;
    	  for(i=1;i<(n-1);i++) a[i]=2;
    	  a[n-1]=(211-2*(n-1)+1);
    	  for(j=0;j<n;j++)
    	  {  int k=n;
    		  for(i=j;k--!=0;i++)
    		  {
    		  System.out.print(a[i%n]+" ");

    		  }
    	  System.out.println();
    	  }
      }
     }
}


import java.util.*;
public class Reorder {

	static boolean[] arr = new boolean[1001];
	static ArrayList<Integer> a = new ArrayList<Integer>();
	public static void main(String[] args) {

		Scanner s = new Scanner(System.in);
		prime();
		primelist();
		int t = s.nextInt();
		while(t-->0){
			int n = s.nextInt();
			int i=0;
			while(n>a.get(i)) {
				i++;
			}
			int primeno = a.get(i);
		//	System.out.println(primeno);
			for(int k=0;k<n;k++) {
				for(int j=0;j<n;j++) {
					if(k==n-1||j==n-1) {
						System.out.print(primeno-(n-1)+" ");
					}
					else {
						System.out.print(1+" ");
					}
				}
				System.out.println();
			}

		}

	}

	public static void prime() {
		arr[0]=false;
		arr[1]=false;
		for(int i =2;i<arr.length;i++) {
			arr[i]=true;
		}
		for(int i=2;i*i<=arr.length;i++) {
			if(arr[i]==true) {
				for(int j = i*i;j<arr.length;j=j+i) {
					if(j%i==0) {
						arr[j]=false;
					}
				}
			}
		}

	}
	public static void primelist() {
		for(int i=0;i<arr.length;i++) {
			if(arr[i]==true) {
				a.add(i);
			}
		}
	}

}


#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define sqr(x) ((x) * (x))
#define cub(x) ((x) * (x) * (x))
#define pii pair<int, int>
#define all(c) c.begin(), c.end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define no cout<<"NO"
#define yes cout<<"YES"
#define name "input"

typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int ui;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MAXN = 6e5 + 1;
const ld EPS = 1e-12;

int bPow(int v, int u, int mod){if(!u)	return 1;	int res = bPow(v, u / 2, mod);	res = (res * 1ll * res) % mod;	if(u & 1) res = (res * 1ll * v) % mod;	return res; }
int fact(int n){ int f=1;  if((n==0)||(n==1)) f = 1; else for(int i=1;i<=n;i++) f*=i; return f; }
int fib(int n){ int a=0,b=1; for (int i = 0; i < n; ++i){ b=b+a; a=b-a; }return b; }
bool compare(const string &c1,const string &c2){ return c1.size() < c2.size(); }
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
string alph = "abcdefghijklmnopqrstuvwxyz";
// freopen("area1.in","r",stdin);
// freopen("area1.out","w",stdout);

int n,k;
void solve(){
	cin>>n>>k;
	set<int>a;
	for (int i = 0; i < n; ++i)
	{
		int t;
		cin>>t;
		a.insert(t);
	}
	if(a.size()>k){
		cout<<-1;
		return;
	}
	cout<<n*k<<endl;
	for(int i=0;i<n;i++){
		for(auto j:a) cout << j << " ";
		for(int j = a.size();j<k;j++) cout << 1 << " ";
	}
}

int main(){

	faster();

	int T=1;
	cin>>T;

	while(T--){
		solve();
		cout<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define sqr(x) ((x) * (x))
#define cub(x) ((x) * (x) * (x))
#define pii pair<int, int>
#define all(c) c.begin(), c.end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define no cout<<"NO"
#define yes cout<<"YES"
#define name "input"

typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int ui;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MAXN = 6e5 + 1;
const ld EPS = 1e-12;

int bPow(int v, int u, int mod){if(!u)	return 1;	int res = bPow(v, u / 2, mod);	res = (res * 1ll * res) % mod;	if(u & 1) res = (res * 1ll * v) % mod;	return res; }
int fact(int n){ int f=1;  if((n==0)||(n==1)) f = 1; else for(int i=1;i<=n;i++) f*=i; return f; }
int fib(int n){ int a=0,b=1; for (int i = 0; i < n; ++i){ b=b+a; a=b-a; }return b; }
bool compare(const string &c1,const string &c2){ return c1.size() < c2.size(); }
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
string alph = "abcdefghijklmnopqrstuvwxyz";
// freopen("area1.in","r",stdin);
// freopen("area1.out","w",stdout);

int n,k;
void solve(){
	cin>>n>>k;
	std::vector<int> a(n),cnt(101,0);
	bool f=true;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		cnt[a[i]]++;
		if(a[i]>k)
			f=false;
	}
	sort(all(cnt));
	if(!f){
		cout<<-1;
	}else{
		for (int i = 0; i < cnt[101]; ++i)
		{
			for (int j = 1; j <= k; ++j)
			{
				cout<<j<<" ";
			}
		}
	}

}

int main(){

	faster();

	int T=1;
	cin>>T;

	while(T--){
		solve();
		cout<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define sqr(x) ((x) * (x))
#define cub(x) ((x) * (x) * (x))
#define pii pair<int, int>
#define all(c) c.begin(), c.end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define no cout<<"NO"
#define yes cout<<"YES"
#define name "input"

typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int ui;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MAXN = 6e5 + 1;
const ld EPS = 1e-12;

int bPow(int v, int u, int mod){if(!u)	return 1;	int res = bPow(v, u / 2, mod);	res = (res * 1ll * res) % mod;	if(u & 1) res = (res * 1ll * v) % mod;	return res; }
int fact(int n){ int f=1;  if((n==0)||(n==1)) f = 1; else for(int i=1;i<=n;i++) f*=i; return f; }
int fib(int n){ int a=0,b=1; for (int i = 0; i < n; ++i){ b=b+a; a=b-a; }return b; }
bool compare(const string &c1,const string &c2){ return c1.size() < c2.size(); }
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
string alph = "abcdefghijklmnopqrstuvwxyz";
// freopen("area1.in","r",stdin);
// freopen("area1.out","w",stdout);

int n,k;
void solve(){
	cin>>n>>k;
	std::vector<int> a(n),cnt(101,0);
	bool f=true;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		cnt[a[i]]++;
		if(a[i]>k)
			f=false;
	}
	sort(all(a));
	sort(all(cnt));
	reverse(all(cnt));
	int mx=cnt[0];
	if(!f){
		cout<<-1;
	}else{
		for (int i = 0; i < 99; ++i)
		{
			for (int j = 1; j <= a[n-1]; ++j)
			{
				cout<<j<<" ";
			}
		}
	}

}

int main(){

	faster();

	int T=1;
	cin>>T;

	while(T--){
		solve();
		cout<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define sqr(x) ((x) * (x))
#define cub(x) ((x) * (x) * (x))
#define pii pair<int, int>
#define all(c) c.begin(), c.end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define no cout<<"NO"
#define yes cout<<"YES"
#define name "input"

typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int ui;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MAXN = 6e5 + 1;
const ld EPS = 1e-12;

int bPow(int v, int u, int mod){if(!u)	return 1;	int res = bPow(v, u / 2, mod);	res = (res * 1ll * res) % mod;	if(u & 1) res = (res * 1ll * v) % mod;	return res; }
int fact(int n){ int f=1;  if((n==0)||(n==1)) f = 1; else for(int i=1;i<=n;i++) f*=i; return f; }
int fib(int n){ int a=0,b=1; for (int i = 0; i < n; ++i){ b=b+a; a=b-a; }return b; }
bool compare(const string &c1,const string &c2){ return c1.size() < c2.size(); }
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
string alph = "abcdefghijklmnopqrstuvwxyz";
// freopen("area1.in","r",stdin);
// freopen("area1.out","w",stdout);

int n,k;
void solve(){
	cin>>n>>k;
	std::vector<int> a(n),cnt(101,0);
	bool f=true;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		cnt[a[i]]++;
		if(a[i]>k)
			f=false;
	}
	sort(all(a));
	sort(all(cnt));
	reverse(all(cnt));
	int mx=cnt[0];
	if(!f){
		cout<<-1;
	}else{
		for (int i = 0; i < k; ++i)
		{
			for (int j = a[0]; j <= a[n-1]; ++j)
			{
				cout<<j<<" ";
			}
		}
	}

}

int main(){

	faster();

	int T=1;
	cin>>T;

	while(T--){
		solve();
		cout<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define sqr(x) ((x) * (x))
#define cub(x) ((x) * (x) * (x))
#define pii pair<int, int>
#define all(c) c.begin(), c.end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define no cout<<"NO"
#define yes cout<<"YES"
#define name "input"

typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int ui;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MAXN = 6e5 + 1;
const ld EPS = 1e-12;

int bPow(int v, int u, int mod){if(!u)	return 1;	int res = bPow(v, u / 2, mod);	res = (res * 1ll * res) % mod;	if(u & 1) res = (res * 1ll * v) % mod;	return res; }
int fact(int n){ int f=1;  if((n==0)||(n==1)) f = 1; else for(int i=1;i<=n;i++) f*=i; return f; }
int fib(int n){ int a=0,b=1; for (int i = 0; i < n; ++i){ b=b+a; a=b-a; }return b; }
bool compare(const string &c1,const string &c2){ return c1.size() < c2.size(); }
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
string alph = "abcdefghijklmnopqrstuvwxyz";
// freopen("area1.in","r",stdin);
// freopen("area1.out","w",stdout);

int n,k;
void solve(){
	cin>>n>>k;
	std::vector<int> a(n);
	bool f=true;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		if(a[i]>k)
			f=false;
	}
	if(!f){
		cout<<-1;
	}else{
		for (int i = 0; i < n; ++i)
		{
			for (int j = 1; j <= k; ++j)
			{
				cout<<j;
			}
		}
	}

}

int main(){

	faster();

	int T=1;
	cin>>T;

	while(T--){
		solve();
		cout<<"\n";
	}
	return 0;
}