//   package com.company;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
import java.lang.*;

public class Main{
    /*
     ** || ॐ श्री गणेशाय नमः ||
     ** @𝙖𝙪𝙩𝙝𝙤𝙧 𝙅𝙞𝙜𝙖𝙧_𝙉𝙖𝙞𝙣𝙪𝙟𝙞
     ** 𝙎𝙑𝙉𝙄𝙏-𝙎𝙐𝙍𝘼𝙏
     */
    public static void main(String args[]){
        PrintWriter out=new PrintWriter(System.out);
        InputReader in=new InputReader(System.in);
        TASK solver = new TASK();
        int t=1;
         t = in.nextInt();
        for(int i=0;i<t;i++)
        {
            solver.solve(in,out,i);
        }
        out.close();
    }
    static class TASK {
        static int mod = 1000000007;
        static int a[] = new int[100000];
        static int x = 2;
        static {
            for(int i=2;i<100000;i++)
            {
                if(a[i]==0)
                {
                    for(int j=2;j*i<100000;j++)
                    {
                        a[j]=1;
                    }
                }
            }
            for(int i=99999;i>=0;i--)
            {
                if(a[i]==0)
                {
                    x=i;
                    break;
                }
            }
        }
        void solve(InputReader in, PrintWriter out, int testNumber) {
            int n = in.nextInt();
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if(j==i)
                        System.out.print(2+" ");
                    else
                        System.out.print(0+" ");
                }
                System.out.println();
            }

        }
    }

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

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

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

        public static long factorial(int n) {
            long fact = 1;
            for (int i = 1; i <= n; i++) {
                fact *= i;
            }
            return fact;
        }
    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}


//   package com.company;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
import java.lang.*;

public class Main{
    /*
     ** || ॐ श्री गणेशाय नमः ||
     ** @𝙖𝙪𝙩𝙝𝙤𝙧 𝙅𝙞𝙜𝙖𝙧_𝙉𝙖𝙞𝙣𝙪𝙟𝙞
     ** 𝙎𝙑𝙉𝙄𝙏-𝙎𝙐𝙍𝘼𝙏
     */
    public static void main(String args[]){
        PrintWriter out=new PrintWriter(System.out);
        InputReader in=new InputReader(System.in);
        TASK solver = new TASK();
        int t=1;
         t = in.nextInt();
        for(int i=0;i<t;i++)
        {
            solver.solve(in,out,i);
        }
        out.close();
    }
    static class TASK {
        static int mod = 1000000000+7;
//        static long a[] = new long[1001];
//        static {
//            a[0]=1l;
//            for(int i=1;i<1001;i++)
//            {
//                a[i]=(a[i-1]*i)%mod;
//            }
//        }
       static int a[] = new int[100000];
        static ArrayList<Integer> al = new ArrayList<>();
        static {
            al.add(2);
            for(int i=3;i<100000;i++)
            {
                boolean flag=true;
                for(int j=0;j<al.size();j++)
                {
                    if(i%al.get(j)==0)
                    {
                        flag=false;
                        break;
                    }
                }
                if(flag)
                    al.add(i);
            }
            for(int i=0;i<al.size();i++)
            {
                a[al.get(i)]++;
            }
//            System.out.println(al);

        }

        void solve(InputReader in, PrintWriter out, int testNumber) {
            int n = in.nextInt();
            for(int i=0;i<al.size();i++)
            {
                if(al.get(i)-(n-1)>=0 && a[al.get(i)-(n-1)]==0)
                {
                    for(int j=0;j<n;j++)
                    {
                        for(int k=0;k<n;k++)
                        {
                            if(j==k)
                                System.out.print(al.get(i)-(n-1)+" ");
                            else
                                System.out.print(1+" ");
                        }
                        System.out.println();
                    }
                    break;
                }
            }

        }
    }

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

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

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

        public static long factorial(int n) {
            long fact = 1;
            for (int i = 1; i <= n; i++) {
                fact *= i;
            }
            return fact;
        }
    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}


/* package codechef; // don't place package name! */

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

public class Main
{
    public static void main(String[] args)
	{
		FastReader sc=new FastReader();
		PrintWriter out=new PrintWriter(System.out);
                int t=sc.nextInt();
                while(t-->0){
                      int n=sc.nextInt();
                      StringBuilder sb=new StringBuilder();
                      int ar[][]=new int[n][n];
                      for(int i=0;i<n;i++){
                            ar[i][i]=1;
                            ar[i][n-i-1]=1;
                      }if(n%2!=0){
                            int p=(n)/2;
                            ar[0][p]=1;
                            ar[p][0]=1;
                      }
                      for(int i=0;i<n;i++){
                            for(int j=0;j<n;j++){
                                  sb.append(ar[i][j]+" ");
                            }sb.append("\n");
                      }out.print(sb);
                }out.flush();
	}
	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;
		}
	}

}


/* package codechef; // don't place package name! */

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

public class Main
{
    public static void main(String[] args)
	{
		FastReader sc=new FastReader();
		PrintWriter out=new PrintWriter(System.out);
                int t=sc.nextInt();
                while(t-->0){
                      int n=sc.nextInt();
                      StringBuilder sb=new StringBuilder();
                      int ar[][]=new int[n][n];
                      for(int i=0;i<n;i++){
                            ar[i][i]=1;
                            ar[i][n-i-1]=1;
                      }for(int i=0;i<n;i++){
                            for(int j=0;j<n;j++){
                                  sb.append(ar[i][j]+" ");
                            }sb.append("\n");
                      }out.print(sb);
                }out.flush();
	}
	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;
		}
	}

}


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

public class Main {

    void solve() {
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j){
                    out.print(1);
                }else{
                    out.print(0);
                }
            }
            out.println();
        }
    }

    static final boolean MULTI_TEST = true;

    // --------------------------------------------------------------------------------------------------------------
    // --------------------------------------------------HELPER------------------------------------------------------
    // --------------------------------------------------------------------------------------------------------------

    void run() {
        if (MULTI_TEST) {
            int t = in.nextInt();
            for (int i = 0; i < t; i++) {
                solve();
            }
        } else {
            solve();
        }
    }

    // --------------------ALGORITHM-------------------------

    public void sort(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted().collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    public void sortRev(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted(Comparator.comparing((Integer x) -> x).reversed())
                .collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    // --------------------SCANNER-------------------------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner(String fileName) {
            if (fileName != null) {
                try {
                    br = new BufferedReader(new FileReader(fileName));
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            } else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

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

        int[] nextInts(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        boolean[] nextLineBools() {
            String line = nextLine();
            int n = line.length();
            boolean[] booleans = new boolean[n];
            for (int i = 0; i < n; i++) {
                booleans[i] = line.charAt(i) == '1';
            }
            return booleans;
        }

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

        public long[] nextLongs(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

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

        String nextLine() {
            try {
                String line = br.readLine();
                if (line == null) {
                    throw new RuntimeException("empty line");
                }
                return line;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    // --------------------WRITER-------------------------
    public static class MyWriter extends PrintWriter {

        public static MyWriter of(String fileName) {
            if (fileName != null) {
                try {
                    return new MyWriter(new FileWriter(fileName));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                return new MyWriter(new BufferedOutputStream(System.out));
            }
        }

        public MyWriter(FileWriter fileWriter) {
            super(fileWriter);
        }

        public MyWriter(OutputStream out) {
            super(out);
        }

        void println(int[] arr) {
            String line = Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" "));
            println(line);
        }

        void printlnYes(){
            println("YES");
        }

        void printlnNo(){
            println("NO");
        }
    }

    // -----------------------BENCHMARK-------------------------
    public static class Benchmark {
        long time;

        public void reset() {
            time = System.currentTimeMillis();
        }

        public long calc() {
            long curTime = System.currentTimeMillis();
            long tmp = curTime - time;
            time = curTime;
            return tmp;
        }

    }

    // --------------------MAIN-------------------------

    public MyScanner in;
    public MyWriter out;
    public Benchmark bm;

    /**
     * run as `java Main`, `java Main input.txt' or `java Main input.txt output.txt'
     * add `-Xss256m` to increase stack size
     */
    public static void main(String[] args) {
        Main m = new Main();
        m.in = new MyScanner(args.length > 0 ? args[0] : null);
        m.out = MyWriter.of(args.length > 1 ? args[1] : null);
        m.bm = new Benchmark();
        m.run();
        m.out.close();
    }
}


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

public class Main {

    void solve() {
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j){
                    out.print(0);
                }else{
                    out.print(1);
                }
            }
            out.println();
        }
    }

    static final boolean MULTI_TEST = true;

    // --------------------------------------------------------------------------------------------------------------
    // --------------------------------------------------HELPER------------------------------------------------------
    // --------------------------------------------------------------------------------------------------------------

    void run() {
        if (MULTI_TEST) {
            int t = in.nextInt();
            for (int i = 0; i < t; i++) {
                solve();
            }
        } else {
            solve();
        }
    }

    // --------------------ALGORITHM-------------------------

    public void sort(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted().collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    public void sortRev(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted(Comparator.comparing((Integer x) -> x).reversed())
                .collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    // --------------------SCANNER-------------------------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner(String fileName) {
            if (fileName != null) {
                try {
                    br = new BufferedReader(new FileReader(fileName));
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            } else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

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

        int[] nextInts(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        boolean[] nextLineBools() {
            String line = nextLine();
            int n = line.length();
            boolean[] booleans = new boolean[n];
            for (int i = 0; i < n; i++) {
                booleans[i] = line.charAt(i) == '1';
            }
            return booleans;
        }

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

        public long[] nextLongs(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

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

        String nextLine() {
            try {
                String line = br.readLine();
                if (line == null) {
                    throw new RuntimeException("empty line");
                }
                return line;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    // --------------------WRITER-------------------------
    public static class MyWriter extends PrintWriter {

        public static MyWriter of(String fileName) {
            if (fileName != null) {
                try {
                    return new MyWriter(new FileWriter(fileName));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                return new MyWriter(new BufferedOutputStream(System.out));
            }
        }

        public MyWriter(FileWriter fileWriter) {
            super(fileWriter);
        }

        public MyWriter(OutputStream out) {
            super(out);
        }

        void println(int[] arr) {
            String line = Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" "));
            println(line);
        }

        void printlnYes(){
            println("YES");
        }

        void printlnNo(){
            println("NO");
        }
    }

    // -----------------------BENCHMARK-------------------------
    public static class Benchmark {
        long time;

        public void reset() {
            time = System.currentTimeMillis();
        }

        public long calc() {
            long curTime = System.currentTimeMillis();
            long tmp = curTime - time;
            time = curTime;
            return tmp;
        }

    }

    // --------------------MAIN-------------------------

    public MyScanner in;
    public MyWriter out;
    public Benchmark bm;

    /**
     * run as `java Main`, `java Main input.txt' or `java Main input.txt output.txt'
     * add `-Xss256m` to increase stack size
     */
    public static void main(String[] args) {
        Main m = new Main();
        m.in = new MyScanner(args.length > 0 ? args[0] : null);
        m.out = MyWriter.of(args.length > 1 ? args[1] : null);
        m.bm = new Benchmark();
        m.run();
        m.out.close();
    }
}


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

public class Main {

    void solve() {
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j){
                    out.print("1 ");
                }else{
                    out.print("0 ");
                }
            }
            out.println();
        }
    }

    static final boolean MULTI_TEST = true;

    // --------------------------------------------------------------------------------------------------------------
    // --------------------------------------------------HELPER------------------------------------------------------
    // --------------------------------------------------------------------------------------------------------------

    void run() {
        if (MULTI_TEST) {
            int t = in.nextInt();
            for (int i = 0; i < t; i++) {
                solve();
            }
        } else {
            solve();
        }
    }

    // --------------------ALGORITHM-------------------------

    public void sort(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted().collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    public void sortRev(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted(Comparator.comparing((Integer x) -> x).reversed())
                .collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    // --------------------SCANNER-------------------------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner(String fileName) {
            if (fileName != null) {
                try {
                    br = new BufferedReader(new FileReader(fileName));
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            } else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

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

        int[] nextInts(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        boolean[] nextLineBools() {
            String line = nextLine();
            int n = line.length();
            boolean[] booleans = new boolean[n];
            for (int i = 0; i < n; i++) {
                booleans[i] = line.charAt(i) == '1';
            }
            return booleans;
        }

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

        public long[] nextLongs(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

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

        String nextLine() {
            try {
                String line = br.readLine();
                if (line == null) {
                    throw new RuntimeException("empty line");
                }
                return line;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    // --------------------WRITER-------------------------
    public static class MyWriter extends PrintWriter {

        public static MyWriter of(String fileName) {
            if (fileName != null) {
                try {
                    return new MyWriter(new FileWriter(fileName));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                return new MyWriter(new BufferedOutputStream(System.out));
            }
        }

        public MyWriter(FileWriter fileWriter) {
            super(fileWriter);
        }

        public MyWriter(OutputStream out) {
            super(out);
        }

        void println(int[] arr) {
            String line = Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" "));
            println(line);
        }

        void printlnYes(){
            println("YES");
        }

        void printlnNo(){
            println("NO");
        }
    }

    // -----------------------BENCHMARK-------------------------
    public static class Benchmark {
        long time;

        public void reset() {
            time = System.currentTimeMillis();
        }

        public long calc() {
            long curTime = System.currentTimeMillis();
            long tmp = curTime - time;
            time = curTime;
            return tmp;
        }

    }

    // --------------------MAIN-------------------------

    public MyScanner in;
    public MyWriter out;
    public Benchmark bm;

    /**
     * run as `java Main`, `java Main input.txt' or `java Main input.txt output.txt'
     * add `-Xss256m` to increase stack size
     */
    public static void main(String[] args) {
        Main m = new Main();
        m.in = new MyScanner(args.length > 0 ? args[0] : null);
        m.out = MyWriter.of(args.length > 1 ? args[1] : null);
        m.bm = new Benchmark();
        m.run();
        m.out.close();
    }
}


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

public class Main {

    void solve() {
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j || (i+1)%n == j){
                    out.print("1 ");
                }else{
                    out.print("0 ");
                }
            }
            out.println();
        }
    }

    static final boolean MULTI_TEST = true;

    // --------------------------------------------------------------------------------------------------------------
    // --------------------------------------------------HELPER------------------------------------------------------
    // --------------------------------------------------------------------------------------------------------------

    void run() {
        if (MULTI_TEST) {
            int t = in.nextInt();
            for (int i = 0; i < t; i++) {
                solve();
            }
        } else {
            solve();
        }
    }

    // --------------------ALGORITHM-------------------------

    public void sort(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted().collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    public void sortRev(int[] arr) {
        List<Integer> tmp = Arrays.stream(arr).boxed().sorted(Comparator.comparing((Integer x) -> x).reversed())
                .collect(Collectors.toList());
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tmp.get(i);
        }
    }

    // --------------------SCANNER-------------------------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner(String fileName) {
            if (fileName != null) {
                try {
                    br = new BufferedReader(new FileReader(fileName));
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            } else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

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

        int[] nextInts(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        boolean[] nextLineBools() {
            String line = nextLine();
            int n = line.length();
            boolean[] booleans = new boolean[n];
            for (int i = 0; i < n; i++) {
                booleans[i] = line.charAt(i) == '1';
            }
            return booleans;
        }

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

        public long[] nextLongs(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

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

        String nextLine() {
            try {
                String line = br.readLine();
                if (line == null) {
                    throw new RuntimeException("empty line");
                }
                return line;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    // --------------------WRITER-------------------------
    public static class MyWriter extends PrintWriter {

        public static MyWriter of(String fileName) {
            if (fileName != null) {
                try {
                    return new MyWriter(new FileWriter(fileName));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                return new MyWriter(new BufferedOutputStream(System.out));
            }
        }

        public MyWriter(FileWriter fileWriter) {
            super(fileWriter);
        }

        public MyWriter(OutputStream out) {
            super(out);
        }

        void println(int[] arr) {
            String line = Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" "));
            println(line);
        }

        void printlnYes(){
            println("YES");
        }

        void printlnNo(){
            println("NO");
        }
    }

    // -----------------------BENCHMARK-------------------------
    public static class Benchmark {
        long time;

        public void reset() {
            time = System.currentTimeMillis();
        }

        public long calc() {
            long curTime = System.currentTimeMillis();
            long tmp = curTime - time;
            time = curTime;
            return tmp;
        }

    }

    // --------------------MAIN-------------------------

    public MyScanner in;
    public MyWriter out;
    public Benchmark bm;

    /**
     * run as `java Main`, `java Main input.txt' or `java Main input.txt output.txt'
     * add `-Xss256m` to increase stack size
     */
    public static void main(String[] args) {
        Main m = new Main();
        m.in = new MyScanner(args.length > 0 ? args[0] : null);
        m.out = MyWriter.of(args.length > 1 ? args[1] : null);
        m.bm = new Benchmark();
        m.run();
        m.out.close();
    }
}


import java.util.Scanner;

public class Main{
    public static boolean IsPrime(int x){
        if(x==2){return true;}
        if(x==1 || x%2==0){return false;}

        for(int i=3;i<=Math.sqrt(x);i+=2){
            if(x%i==0){
                return false;
            }
        }
        return true;
    }
    public static int addForPrime(int x) {
        for(int i=x+(x%2)+3;;i+=2){
            if(IsPrime(i)){return i-x;}
        }
    }
    public static int EvenAdd(int x,int y) {
        for(int i=1;;i+=2){
            if(!IsPrime(i)){
                if(IsPrime(x+i) && IsPrime(y+i)){
                    return i;
                }
            }
        }
    }
    public static void Printer(int n) {
        int [][]base={{4,9},{15,8}};
        if(n==2){for (int i = 0; i < base.length; i++) {
            for (int j = 0; j < base.length; j++) {
                System.out.print(base[i][j]+" ");
            }
            System.out.println();
        }}
        while(base.length!=n){
            int []arr=new int[2*base.length+1];
            for (int i = 0; i < base.length; i++) {
                int sum_row=0;
                int sum_cloumn=0;
                for (int j = 0; j < base.length; j++) {

                    sum_row+=base[i][j];
                    sum_cloumn+=base[j][i];

                    arr[i]=addForPrime(sum_row);
                    arr[base.length+i+1]=addForPrime(sum_cloumn);
                }
            }
            int arr_row=0;
            int arr_column=0;
            for (int i = 0; i < base.length; i++) {
                arr_row+=arr[i];
                arr_column+=arr[2*base.length-i];
            }
            arr[base.length]=EvenAdd(arr_column, arr_row);
            int [][]new_base=new int[base.length+1][base.length+1];
            for (int i = 0; i < new_base.length; i++) {
                for (int j = 0; j < new_base.length; j++) {
                    if(j==0||i==base.length){
                        new_base[i][j]=arr[i+j];
                    }
                    else {new_base[i][j]=base[i][j-1];}
                }
            }
        base=new_base;
        }
        for (int i = 0; i < base.length; i++) {
            for (int j = 0; j < base.length; j++) {
                System.out.print(base[i][j]+" ");
            }
            System.out.println();
        }

    }
    public static void main(String[] args) {
        //Base case
        Scanner obj=new Scanner(System.in);
        int t=obj.nextInt();
        int []n=new int[t];
        for (int i = 0; i < n.length; i++) {
            n[t-1-i]=obj.nextInt();}
    while(t>0){
        Printer(n[t-1]);
    t-=1;
    }
    }
}


import java.util.Scanner;

public class Main{
    public static boolean IsPrime(int x){
        if(x==2){return true;}
        if(x==1 || x%2==0){return false;}

        for(int i=3;i<=Math.sqrt(x);i+=2){
            if(x%i==0){
                return false;
            }
        }
        return true;
    }
    public static int addForPrime(int x) {
        for(int i=x+(x%2)+3;;i+=2){
            if(IsPrime(i)){return i-x;}
        }
    }
    public static int EvenAdd(int x,int y) {
        for(int i=1;;i+=2){
            if(!IsPrime(i)){
                if(IsPrime(x+i) && IsPrime(y+i)){
                    return i;
                }
            }
        }
    }
    public static void main(String[] args) {
        //Base case
        Scanner obj=new Scanner(System.in);
        int [][]base={{4,9},{15,8}};
        int t=obj.nextInt();
        int []n=new int[t];
        for (int i = 0; i < n.length; i++) {
            n[t-1-i]=obj.nextInt();
        }

    while(t>0){
        while(base.length!=n[t-1]){
            int []arr=new int[2*base.length+1];
            for (int i = 0; i < base.length; i++) {
                int sum_row=0;
                int sum_cloumn=0;
                for (int j = 0; j < base.length; j++) {

                    sum_row+=base[i][j];
                    sum_cloumn+=base[j][i];

                    arr[i]=addForPrime(sum_row);
                    arr[base.length+i+1]=addForPrime(sum_cloumn);
                }
            }
            int arr_row=0;
            int arr_column=0;
            for (int i = 0; i < base.length; i++) {
                arr_row+=arr[i];
                arr_column+=arr[2*base.length-i];
            }
            arr[base.length]=EvenAdd(arr_column, arr_row);
            int [][]new_base=new int[base.length+1][base.length+1];
            for (int i = 0; i < new_base.length; i++) {
                for (int j = 0; j < new_base.length; j++) {
                    if(j==0||i==base.length){
                        new_base[i][j]=arr[i+j];
                    }
                    else {new_base[i][j]=base[i][j-1];}
                }
            }
        base=new_base;
        }
        for (int i = 0; i < base.length; i++) {
            for (int j = 0; j < base.length; j++) {
                System.out.print(base[i][j]+" ");
            }
            System.out.println();
        }

    t-=1;
    }
    }
}


import java.util.Scanner;

public class Main{
    public static boolean IsPrime(int x){
        if(x==2){return true;}
        if(x==1 || x%2==0){return false;}

        for(int i=3;i<=Math.sqrt(x);i+=2){
            if(x%i==0){
                return false;
            }
        }
        return true;
    }
    public static int addForPrime(int x) {
        for(int i=x+(x%2)+3;;i+=2){
            if(IsPrime(i)){return i-x;}
        }
    }
    public static int EvenAdd(int x,int y) {
        for(int i=1;;i+=2){
            if(!IsPrime(i)){
                if(IsPrime(x+i) && IsPrime(y+i)){
                    return i;
                }
            }
        }
    }
    public static void Printer(int n) {
        int [][]base={{4,9},{15,8}};

        while(base.length!=n){
            int []arr=new int[2*base.length+1];
            for (int i = 0; i < base.length; i++) {
                int sum_row=0;
                int sum_cloumn=0;
                for (int j = 0; j < base.length; j++) {

                    sum_row+=base[i][j];
                    sum_cloumn+=base[j][i];

                    arr[i]=addForPrime(sum_row);
                    arr[base.length+i+1]=addForPrime(sum_cloumn);
                }
            }
            int arr_row=0;
            int arr_column=0;
            for (int i = 0; i < base.length; i++) {
                arr_row+=arr[i];
                arr_column+=arr[2*base.length-i];
            }
            arr[base.length]=EvenAdd(arr_column, arr_row);
            int [][]new_base=new int[base.length+1][base.length+1];
            for (int i = 0; i < new_base.length; i++) {
                for (int j = 0; j < new_base.length; j++) {
                    if(j==0||i==base.length){
                        new_base[i][j]=arr[i+j];
                    }
                    else {new_base[i][j]=base[i][j-1];}
                }
            }
        base=new_base;
        }
        for (int i = 0; i < base.length; i++) {
            for (int j = 0; j < base.length; j++) {
                System.out.print(base[i][j]+" ");
            }
            System.out.println();
        }

    }
    public static void main(String[] args) {
        //Base case
        Scanner obj=new Scanner(System.in);
        int t=obj.nextInt();
        int []n=new int[t];
        for (int i = 0; i < n.length; i++) {
            n[t-1-i]=obj.nextInt();}
    while(t>0){
        Printer(n[t-1]);
    t-=1;
    }
    }
}


import java.util.Scanner;
public class PrimeSquare
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int k=1;k<=t;k++)
        {
            int n=sc.nextInt();
            int a[][]=new int[n][n];
            if(n%2==0)
            {
                for(int i=0;i<n;i++)
                {
                    a[i][i]=1;
                    a[i][n-i-1]=1;
                }
            }
            else
            {
                for(int i=0;i<n;i++)
                {
                    a[i][i]=1;
                    a[i][n-i-1]=1;
                }
                a[0][n/2]=1;
                a[n/2][0]=1;
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                    System.out.print(a[i][j]+" ");
                System.out.println();
            }
        }
    }
}


import java.util.Scanner;
public class PrimeSquare
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int k=1;k<=t;k++)
        {
            int n=sc.nextInt();
            int a[][]=new int[n][n];
            for(int i=0;i<n;i++)
            {
                a[i][i]=1;
                a[i][n-i-1]=1;
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                    System.out.print(a[i][j]+" ");
                System.out.println();
            }
        }
    }
}


import java.util.Scanner;
public class PrimeSquare
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[][]=new int[n][n];
        for(int i=0;i<n;i++)
            a[i][i]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                System.out.print(a[i][j]+" ");
            System.out.println();
        }
    }
}


import java.util.Scanner;
public class PrimeSquare
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[][]=new int[n][n];
        for(int i=0;i<n;i++)
            a[i][i]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                System.out.print(a[i][j]);
            System.out.println();
        }
    }
}


import java.util.Scanner;
public class PrimeSquare
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int k=1;k<=t;k++)
        {
            int n=sc.nextInt();
            int a[][]=new int[n][n];
            for(int i=0;i<n;i++)
                a[i][i]=1;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                    System.out.print(a[i][j]+" ");
                System.out.println();
            }
        }
    }
}


import java.util.Scanner;

public class q2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0)
        {
            int n = sc.nextInt();
            boolean yes = true;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if(i==n-1 && yes)
                    {
                        System.out.print(1+" ");
                        yes = false;
                    }
                    else if(i==j && i==n-1)
                    {
                        System.out.print(4+" ");
                    }

                    else if(i==j)
                    {
                        System.out.print(4+" "+1+" ");
                        j++;
                    }
                    else
                    System.out.print(0+" ");
                }
                System.out.println();
            }
        }
        sc.close();
    }
}


import java.util.Scanner;

public class q2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0)
        {
            int n = sc.nextInt();
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if(i==j)
                    System.out.print(2+" ");
                    else
                    System.out.print(0+" ");
                }
                System.out.println();
            }
        }
        sc.close();
    }
}


import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

/* Name of the class has to be "Main" only if the class is public. */
public class Cp
{
	static int dp[][]=new int[1002][1002];
	 //	Template For Fast i/o copied From Gfg
		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) {

	   FastReader obj=new FastReader();
       int t=obj.nextInt();
       PrintWriter out=new PrintWriter(System.out);
       while(t-->0)
       {
    	   int n=obj.nextInt();
    	   int []a=new int[n];
    	   a[0]=1;
    	   a[1]=1;
    	   int i;
    	   int j=0;
    	   while(j<n)
    	   {
    		   int temp=a[n-1];
    		   for(i=n-1;i>0;i--)
    		   {
    			   a[i]=a[i-1];
    		   }
    		   a[0]=temp;

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

}


import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

/* Name of the class has to be "Main" only if the class is public. */
public class Cp
{
	static int dp[][]=new int[1002][1002];
	 //	Template For Fast i/o copied From Gfg
		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) {

	   FastReader obj=new FastReader();
       int t=obj.nextInt();
       while(t-->0)
       {
    	   int n=obj.nextInt();
    	   int [][]a=new int[n][n];
    	   for(int i=0;i<n;i++)
    	   {
    		   for(int j=0;j<n;j++)
    		   {
    			   if(i==j || (i+j)==n-1)
    			   {
    				   a[i][j]=1;
    			   }
    			   else
    			   {
    				   a[i][j]=0;
    			   }
    		   }
    	   }
    	   for(int i=0;i<n;i++)
    	   {
    		   for(int j=0;j<n;j++)
    		   {
    			   System.out.print(a[i][j]+" ");
    		   }
    		   System.out.println();
    	   }
       }
}

	public static void topologicalSort(HashMap<Integer,ArrayList<Integer>> map,int src,boolean visited[],Stack<Integer> stack)
	{
		visited[src]=true;
		ArrayList<Integer> edges=new ArrayList<>();
		edges=map.get(src);
		if(edges!=null)
		{
			for(int i=0;i<edges.size();i++)
			{
				if(visited[edges.get(i)]==false)
				{
					topologicalSort(map,edges.get(i),visited,stack);
				}
			}
		}
		stack.push(src);
	}
}
class pair implements Comparable<pair>
{
    int nvertex;
    int wt;
    pair(int nvertex,int wt)
    {
        this.nvertex=nvertex;
        this.wt=wt;
    }
	@Override
	public int compareTo(pair o) {

		return this.wt-o.wt;
	}
}


import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

/* Name of the class has to be "Main" only if the class is public. */
public class Cp
{
	static int dp[][]=new int[1002][1002];
	 //	Template For Fast i/o copied From Gfg
		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) {

	   FastReader obj=new FastReader();
       int t=obj.nextInt();
       while(t-->0)
       {
    	   int n=obj.nextInt();
    	   int []a=new int[n];
    	   a[0]=1;
    	   a[1]=1;
    	   int i;
    	   int j=0;
    	   while(j<n)
    	   {
    		   int temp=a[n-1];
    		   for(i=n-1;i>0;i--)
    		   {
    			   a[i]=a[i-1];
    		   }
    		   a[0]=temp;

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

	public static void topologicalSort(HashMap<Integer,ArrayList<Integer>> map,int src,boolean visited[],Stack<Integer> stack)
	{
		visited[src]=true;
		ArrayList<Integer> edges=new ArrayList<>();
		edges=map.get(src);
		if(edges!=null)
		{
			for(int i=0;i<edges.size();i++)
			{
				if(visited[edges.get(i)]==false)
				{
					topologicalSort(map,edges.get(i),visited,stack);
				}
			}
		}
		stack.push(src);
	}
}
class pair implements Comparable<pair>
{
    int nvertex;
    int wt;
    pair(int nvertex,int wt)
    {
        this.nvertex=nvertex;
        this.wt=wt;
    }
	@Override
	public int compareTo(pair o) {

		return this.wt-o.wt;
	}
}


import java.io.BufferedReader;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.math.BigInteger;
public class BPrime
{
	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
static class AnotherReader {
	BufferedReader br;
	StringTokenizer st;

	AnotherReader() throws FileNotFoundException {
		br = new BufferedReader(new InputStreamReader(System.in));
	}

	AnotherReader(int a) throws FileNotFoundException {
		br = new BufferedReader(new FileReader("input.txt"));
	}

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

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

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

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

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

	static void pn(Object o) {
		out.println(o);
		out.flush();

	}

	static void p(Object o) {
		out.print(o);
		out.flush();
	}

	static void pni(Object o) {
		out.println(o);
		System.out.flush();
	}

	static int I() throws IOException {
		return sc.nextInt();
	}

	static long L() throws IOException {
		return sc.nextLong();
	}

	static double D() throws IOException {
		return sc.nextDouble();
	}

	static String S() throws IOException {
		return sc.nextLine();
	}

	static char C() throws IOException {
		return sc.next().charAt(0);
	}
	static int[] Ai(int n) throws IOException {
		int[] arr = new int[n];

		for (int i = 0; i < n; i++)
			arr[i] = I();

		return arr;
	}

	static String[] As(int n) throws IOException {
		String s[] = new String[n];

		for (int i = 0; i < n; i++)
			s[i] = S();

		return s;
	}

	static long[] Al(int n) throws IOException {
		long[] arr = new long[n];

		for (int i = 0; i < n; i++)
			arr[i] = L();

		return arr;
	}
	static int next(int[] arr, int target) {
		int start = 0, end = arr.length - 1;

		int ans = -1;
		while (start <= end) {
			int mid = (start + end) / 2;

			// Move to the left side if the target is smaller
			if (arr[mid] >= target) {
				end = mid - 1;
			}

			// Move right side
			else {
				ans = mid;
				start = mid + 1;
			}
		}
		return ans;
	}
	static void sieveOfEratosthenes(ArrayList<Integer> arr,int n)
    {
		n=n+1;
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        boolean prime[] = new boolean[n+1];
        for(int i=0;i<n;i++)
            prime[i] = true;

        for(int p = 2; p*p <=n; p++)
        {
            // If prime[p] is not changed, then it is a prime
            if(prime[p] == true)
            {
                // Update all multiples of p
                for(int i = p*p; i <= n; i += p)
                    prime[i] = false;
            }
        }

        // Print all prime numbers
        for(int i = 2; i <= n; i++)
        {
            if(prime[i] == true)
                arr.add(i);
        }
    }
	public static void process() throws IOException
	{
		int n=I();
		ArrayList<Integer> prime1=new ArrayList<Integer>();
		int a[][]=new int[n][n];
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				a[i][j]=0;
			}
		}
		sieveOfEratosthenes(prime1,n);

		if(prime1.contains(n))
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++)
				{
					p(1+" ");
				}
				pn("");
			}
		}
		else
		{
			if(n%2!=0)
			{

			int k=0;int p=-1;
			for(int i=0;i<n/2;i++)
			{

				for(int j=k;j<k+2;j++)
				{
					a[i][j]=1;

				}
				k=k+2;
				 p++;

			}

			k=k-1;
				for(int j=k;j>=k-1;j--)
				{
					a[p+1][j]=1;
				}
			p=p+1;
			k=k-2;

			for(int i=p+1;i<(n-1);i++)
			{
				for(int j=k;j>=k-1;j--)
				{
					a[i][j]=1;

				}
				k=k-2;
			}

			if(n%2!=0)
			{
			a[0][0]=3;
			a[0][n-1]=1;
			a[n-1][n-1]=1;
			a[n-1][0]=1;
			}
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++)
				{
					p(a[i][j]+" ");
				}
				pn("");
			}
			}

			else
			{

				int k=0;int p=-1;
				for(int i=0;i<n/2;i++)
				{

					for(int j=k;j<k+2;j++)
					{
						a[i][j]=1;

					}
					k=k+2;
					 p++;

				}

		   k=k-1;
				for(int i=p+1;i<n;i++)
				{
					for(int j=k;j>=k-1;j--)
					{
						a[i][j]=1;

					}
					k=k-2;
				}
				for(int i=0;i<n;i++)
				{
					for(int j=0;j<n;j++)
					{
						p(a[i][j]+" ");
					}
					pn("");
				}

			}

			}
	}
	public static void main(String[] args)throws IOException
	{
		try
		{
			boolean oj=true;

			if(oj==true)
			{
				AnotherReader sk=new AnotherReader();
				PrintWriter out=new PrintWriter(System.out);}
			else
			{
				AnotherReader sk=new AnotherReader(100);
				out=new PrintWriter("output.txt");
			}
			long T=L();
			while(T-->0)
			{
				process();
			}
			out.flush();out.close();
		}
		catch(Exception e)
		{
			return;
		}
	}

}


import java.io.BufferedReader;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.math.BigInteger;
public class BPrime
{
	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
static class AnotherReader {
	BufferedReader br;
	StringTokenizer st;

	AnotherReader() throws FileNotFoundException {
		br = new BufferedReader(new InputStreamReader(System.in));
	}

	AnotherReader(int a) throws FileNotFoundException {
		br = new BufferedReader(new FileReader("input.txt"));
	}

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

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

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

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

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

	static void pn(Object o) {
		out.println(o);
		out.flush();

	}

	static void p(Object o) {
		out.print(o);
		out.flush();
	}

	static void pni(Object o) {
		out.println(o);
		System.out.flush();
	}

	static int I() throws IOException {
		return sc.nextInt();
	}

	static long L() throws IOException {
		return sc.nextLong();
	}

	static double D() throws IOException {
		return sc.nextDouble();
	}

	static String S() throws IOException {
		return sc.nextLine();
	}

	static char C() throws IOException {
		return sc.next().charAt(0);
	}
	static int[] Ai(int n) throws IOException {
		int[] arr = new int[n];

		for (int i = 0; i < n; i++)
			arr[i] = I();

		return arr;
	}

	static String[] As(int n) throws IOException {
		String s[] = new String[n];

		for (int i = 0; i < n; i++)
			s[i] = S();

		return s;
	}

	static long[] Al(int n) throws IOException {
		long[] arr = new long[n];

		for (int i = 0; i < n; i++)
			arr[i] = L();

		return arr;
	}
	static int next(int[] arr, int target) {
		int start = 0, end = arr.length - 1;

		int ans = -1;
		while (start <= end) {
			int mid = (start + end) / 2;

			// Move to the left side if the target is smaller
			if (arr[mid] >= target) {
				end = mid - 1;
			}

			// Move right side
			else {
				ans = mid;
				start = mid + 1;
			}
		}
		return ans;
	}
	static void sieveOfEratosthenes(ArrayList<Integer> arr,int n)
    {
		n=n+1;
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        boolean prime[] = new boolean[n+1];
        for(int i=0;i<n;i++)
            prime[i] = true;

        for(int p = 2; p*p <=n; p++)
        {
            // If prime[p] is not changed, then it is a prime
            if(prime[p] == true)
            {
                // Update all multiples of p
                for(int i = p*p; i <= n; i += p)
                    prime[i] = false;
            }
        }

        // Print all prime numbers
        for(int i = 2; i <= n; i++)
        {
            if(prime[i] == true)
                arr.add(i);
        }
    }
	public static void process() throws IOException
	{
		int n=I();
		ArrayList<Integer> prime1=new ArrayList<Integer>();
		int a[][]=new int[n][n];
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				a[i][j]=0;
			}
		}
		sieveOfEratosthenes(prime1,n);

		if(prime1.contains(n))
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++)
				{
					p(1+" ");
				}
				pn("");
			}
		}
		else
		{
			if(n%2!=0)
			{

			int k=0;int p=-1;
			for(int i=0;i<n/2;i++)
			{

				for(int j=k;j<k+2;j++)
				{
					a[i][j]=1;

				}
				k=k+2;
				 p++;

			}

			k=k-1;
				for(int j=k;j>=k-1;j--)
				{
					a[p+1][j]=1;
				}
			p=p+1;
			k=k-2;

			for(int i=p+1;i<(n-1);i++)
			{
				for(int j=k;j>=k-1;j--)
				{
					a[i][j]=1;

				}
				k=k-2;
			}

			if(n%2!=0)
			{
			a[0][0]=9;
			a[0][n-1]=1;
			a[n-1][n-1]=1;
			a[n-1][0]=1
					;
			}
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++)
				{
					p(a[i][j]+" ");
				}
				pn("");
			}
			}

			else
			{

				int k=0;int p=-1;
				for(int i=0;i<n/2;i++)
				{

					for(int j=k;j<k+2;j++)
					{
						a[i][j]=1;

					}
					k=k+2;
					 p++;

				}

		   k=k-1;
				for(int i=p+1;i<n;i++)
				{
					for(int j=k;j>=k-1;j--)
					{
						a[i][j]=1;

					}
					k=k-2;
				}
				for(int i=0;i<n;i++)
				{
					for(int j=0;j<n;j++)
					{
						p(a[i][j]+" ");
					}
					pn("");
				}

			}

			}
	}
	public static void main(String[] args)throws IOException
	{
		try
		{
			boolean oj=true;

			if(oj==true)
			{
				AnotherReader sk=new AnotherReader();
				PrintWriter out=new PrintWriter(System.out);}
			else
			{
				AnotherReader sk=new AnotherReader(100);
				out=new PrintWriter("output.txt");
			}
			long T=L();
			while(T-->0)
			{
				process();
			}
			out.flush();out.close();
		}
		catch(Exception e)
		{
			return;
		}
	}

}


import java.util.Scanner;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.Set;

public class Main {
    public static boolean is_prime(int n) {
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int T = stdin.nextInt();
        for (int t = 1; t <= T; t++) {
            int n = stdin.nextInt();
            if (is_prime(n) == true) {
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        System.out.print(1 + " ");
                    }
                    System.out.print("\n");
                }
                continue;
            } else {
                int x = (n - 1) * 4, y = x;
                while (is_prime(x) == false || is_prime(x - y) == true)
                    x++;
                x = x - y;
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if (j == i) {
                            System.out.print(x + " ");
                        } else
                            System.out.print(4 + " ");
                    }
                    System.out.print("\n");
                }
            }
        }
        stdin.close();
    }
}


import java.util.Scanner;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.Set;

public class Main {
    public static boolean is_prime(int n) {
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int T = stdin.nextInt();
        for (int t = 1; t <= T; t++) {
            int n = stdin.nextInt();
            if (is_prime(n) == true) {
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        System.out.print(1 + " ");
                    }
                    System.out.print("\n");
                }
                continue;
            } else {
                int x = (n - 1) * 4, y = x;
                while (is_prime(x) == false)
                    x++;
                x = x - y;
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if (j == i) {
                            System.out.print(x + " ");
                        } else
                            System.out.print(4 + " ");
                    }
                    System.out.print("\n");
                }
            }
        }
        stdin.close();
    }
}


import java.io.*;
import java.util.*;
import java.util.Arrays.*;
public class PrimeSquare
{
    public static void main(String[] args) throws IOException
    {
        Scanner in=new Scanner(System.in);
        PrintWriter out=new PrintWriter(System.out);
        int t=in.nextInt();
        while(t-->0)
        {
            int te=0;
            int n=in.nextInt();
            int arr[][]=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 || j==n-i-1)
                    arr[i][j]=1;
                    else
                    arr[i][j]=0;
                }
            }
        }
       else
        {
            for(int i=0;i<n;i++)
            {
                if(i==((n+1)/2)-1)
                arr[i][0]=1;
                for(int j=0;j<n;j++)
                {

                    if(i==j || j==n-i-1)
                    arr[i][j]=1;
                    else
                    arr[i][j]=0;
                }
            }
             te=((n+1)/2)-1;
            arr[n-1][t]=1;
        }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    out.print(arr[i][j]+" ");
                }
                out.println();
            }
        }

        out.close();
    }

}


import java.io.*;
import java.util.*;
import java.util.Arrays.*;
public class PrimeSquare
{
    public static void main(String[] args) throws IOException
    {
        Scanner in=new Scanner(System.in);
        PrintWriter out=new PrintWriter(System.out);
        int t=in.nextInt();
        while(t-->0)
        {
            int te=0;
            int n=in.nextInt();
            int arr[][]=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 || j==n-i-1)
                    arr[i][j]=1;
                    else
                    arr[i][j]=0;
                }
            }
        }
       else
        {
            for(int i=0;i<n;i++)
            {
                //if(i==((n+1)/2)-1)

                //arr[i][0]=1;

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

                    if(i==j || j==n-i-1)
                    arr[i][j]=1;
                    else
                    arr[i][j]=0;
                }
            }
             te=((n+1)/2)-1;
            arr[n-1][te]=1;
            arr[te][0]=1;
        }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    out.print(arr[i][j]+" ");
                }
                out.println();
            }
        }

        out.close();
    }

}


import java.util.*;
import java.io.*;
public class B
{
    public static void main(String[] args)
    {
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        for (int tt = 0;tt < t; tt++)
        {
            int n=sc.nextInt();
            if(isPrime(n))
            {
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                        System.out.print(1+" ");
                    System.out.println();
                }
            }
            else
            {
                int arr[][]=new int[n][n];
                int a=0,b=1;
                for (int i = 0; i < n-1; i++)
                {
                    arr[i][a]=1;
                    arr[i][b]=1;
                    a++;
                    b++;
                }
                arr[n-1][0]=1;
                arr[n-1][n-1]=1;
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                    {
                        System.out.print(arr[i][j]+" ");
                    }
                    System.out.println();
                }
            }
        }
    }
    public static boolean isPrime(int n)
    {
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            if(n%i==0) return false;
        }
        return true;
    }
}
class FastReader extends B
{
    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;
    }
    char nextChar()
    {
        String str = "";
        try
        {
            str = br.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str.charAt(0);
    }
}


import java.util.*;
import java.io.*;
public class B
{
    public static void main(String[] args)
    {
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        for (int tt = 0;tt < t; tt++)
        {
            int n=sc.nextInt();
            if(isPrime(n))
            {
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                        System.out.print(1+" ");
                    System.out.println();
                }
            }

            else
            {
                int sum=0;
                for (int i = n; i <=97; i++)
                {
                    if(isPrime(i))
                    {
                        sum=i;
                        break;
                    }
                }
                int common=sum/n;
                int extra=sum-(n-1*common);
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                    {
                        if(i==j) System.out.print(extra);
                        else System.out.print(common);
                    }
                    System.out.println();
                }

            }
        }
    }
    public static boolean isPrime(int n)
    {
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            if(n%i==0) return false;
        }
        return true;
    }
}
class FastReader extends B
{
    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;
    }
    char nextChar()
    {
        String str = "";
        try
        {
            str = br.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str.charAt(0);
    }
}


import java.util.*;
import java.io.*;
public class B
{
    public static void main(String[] args)
    {
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        for (int tt = 0;tt < t; tt++)
        {
            int n=sc.nextInt();
            if(isPrime(n))
            {
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                        System.out.print(1+" ");
                    System.out.println();
                }
            }

            else
            {
                int sum=0;
                for (int i = n; i <=97; i++)
                {
                    if(isPrime(i))
                    {
                        sum=i;
                        break;
                    }
                }
                int common=sum/n;
                int extra=sum-(n-1*common);
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                    {
                        if(i==j) System.out.print(extra+" ");
                        else System.out.print(common+" ");
                    }
                    System.out.println();
                }

            }
        }
    }
    public static boolean isPrime(int n)
    {
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            if(n%i==0) return false;
        }
        return true;
    }
}
class FastReader extends B
{
    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;
    }
    char nextChar()
    {
        String str = "";
        try
        {
            str = br.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str.charAt(0);
    }
}


import java.util.*;
import java.io.*;
public class B
{
    public static void main(String[] args)
    {
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        for (int tt = 0;tt < t; tt++)
        {
            int n=sc.nextInt();
            if(isPrime(n))
            {
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                        System.out.print(1+" ");
                    System.out.println();
                }
            }

            else
            {
                /*int sum=0;
                for (int i = n; i <=97; i++)
                {
                    if(isPrime(i))
                    {
                        sum=i;
                        break;
                    }
                }
                int common=sum/n;
                int extra=sum-(n-1*common);
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                    {
                        if(i==j) System.out.print(extra+" ");
                        else System.out.print(common+" ");
                    }
                    System.out.println();
                } */
                for (int i = 0; i < n; i++)
                {
                    for(int j = 0; j < n; j++)
                    {
                        if(i==j) System.out.print(2+" ");
                        else System.out.print(0+" ");
                    }
                    System.out.println();
                }
            }
        }
    }
    public static boolean isPrime(int n)
    {
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            if(n%i==0) return false;
        }
        return true;
    }
}
class FastReader extends B
{
    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;
    }
    char nextChar()
    {
        String str = "";
        try
        {
            str = br.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str.charAt(0);
    }
}


import java.util.Scanner;

public class PrimeSquare {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int test = scanner.nextInt();
        for (int t = 0; t < test; t++) {
            int size = scanner.nextInt();
            int i = size - 1;
            int k = 1;
            boolean primeFound = false;
            if (size > 3) {
                for (int index = size; index >= 2; index--) {
                    if ((size * i + k) % index == 0) {
                        i--;
                        k++;
                        index = size + 1;
                    }
                }
            }
            int[] arr1 = new int[size];
            int k2 = 0;
            for (int i1 = 0; i1 < size; i1++) {
                if (k2 < i) {
                    arr1[i1] = size;
                } else {
                    arr1[i1] = 1;
                }
                k2++;
            }
            int k1 = 0;
            for (int n = 0; n < size; n++) {
                for (int i1 = k1; i1 < size; i1++) {
                    System.out.print(arr1[i1] + " ");
                }
                if (k1 > 0) {
                    for (int i2 = 0; i2 < k1; i2++) {
                        System.out.print(arr1[i2] + " ");
                    }
                }
                k1++;
                System.out.println();
            }

        }
    }
}


import java.util.Scanner;

public class PrimeSquare {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int test = scanner.nextInt();
        for (int t = 0; t < test; t++) {
            int size = scanner.nextInt();
            int i = size - 1;
            int k = 1;
            boolean primeFound = false;
            if (size >= 3) {
                for (int index = size*2; index >= 2; index--) {
                    if (((size*2) * i + k) % index == 0) {
                        i--;
                        k++;
                        index = size*2 + 1;
                    }
                }
            }
            int[] arr1 = new int[size];
            int k2 = 0;
            for (int i1 = 0; i1 < size; i1++) {
                if (k2 < i) {
                    arr1[i1] = size*2;
                } else {
                    arr1[i1] = 1;
                }
                k2++;
            }
            int k1 = 0;
            for (int n = 0; n < size; n++) {
                for (int i1 = k1; i1 < size; i1++) {
                    System.out.print(arr1[i1] + " ");
                }
                if (k1 > 0) {
                    for (int i2 = 0; i2 < k1; i2++) {
                        System.out.print(arr1[i2] + " ");
                    }
                }
                k1++;
                System.out.println();
            }

        }
    }
}


import java.util.Scanner;

public class PrimeSquare {
    private static  Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int test = scanner.nextInt();
        for (int t = 0; t < test; t++) {
            int size = scanner.nextInt();
            int i = size - 1;
            int k = 1;
            boolean primeFound = false;
            if (size >= 3) {
                for (int index = size; index >= 2; index--) {
                    if (((size*2) * i + k) % index == 0) {
                        i--;
                        k++;
                        index = size + 1;
                    }
                }
            }
            int[] arr1 = new int[size];
            int k2 = 0;
            for (int i1 = 0; i1 < size; i1++) {
                if (k2 < i) {
                    arr1[i1] = size*2;
                } else {
                    arr1[i1] = 1;
                }
                k2++;
            }
            int k1 = 0;
            for (int n = 0; n < size; n++) {
                for (int i1 = k1; i1 < size; i1++) {
                    System.out.print(arr1[i1] + " ");
                }
                if (k1 > 0) {
                    for (int i2 = 0; i2 < k1; i2++) {
                        System.out.print(arr1[i2] + " ");
                    }
                }
                k1++;
                System.out.println();
            }

        }
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class B {

    private static boolean isPrime(int n) {
        int factors = 0;
        for (int i=1; i <= n; i++) {
            if (n % i == 0) {
                factors++;
            }
        }
        if(factors == 2) return true;
        else return false;
    }
    private static int nextPrime(int n){
        while(true) {
            if (isPrime(++n)) return n;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());

        for (int i = 0; i < t; i++) {
            int n = Integer.parseInt(sc.nextLine());
            if (isPrime(n)) {
                for (int j = 0; j < n; j++) {
                    for(int k = 0; k < n; k++) {
                        System.out.print("1 ");
                    }
                    System.out.println();
                }
            } else {
                ArrayList<Integer> arr = new ArrayList();
                int nextprime = nextPrime(n-1);
                arr.add(nextprime-(n-1));
                for (int j = 1; j < n-1; j++) {
                    nextprime = nextPrime(nextprime);
                    arr.add(nextprime-(n-1));
                }

                int sum = 0;
                for (int a : arr)
                    sum = sum + a;
                arr.add(nextPrime(sum));

                for (int j = 0; j < n-1; j++) {
                    for(int k = 0; k < n-1; k++) {
                        System.out.print("1 ");
                    }
                    System.out.println(arr.get(j) + "\n");
                }
                for(int j = 0; j <n; j++) {
                    System.out.print(arr.get(j) + " ");
                }
                System.out.println();
            }
        }
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class B {

    private static boolean isPrime(int n) {
        int factors = 0;
        for (int i=1; i <= n; i++) {
            if (n % i == 0) {
                factors++;
            }
        }
        if(factors == 2) return true;
        else return false;
    }
    private static int nextPrime(int n){
        while(true) {
            if (isPrime(++n)) return n;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());

        for (int i = 0; i < t; i++) {
            int n = Integer.parseInt(sc.nextLine());
            if (isPrime(n)) {
                for (int j = 0; j < n; j++) {
                    for(int k = 0; k < n; k++) {
                        System.out.print("1 ");
                    }
                    System.out.println();
                }
            } else {
                ArrayList<Integer> arr = new ArrayList();
                int nextprime = nextPrime(n-1);
                while (isPrime(nextprime-(n-1))) {
                    nextprime = nextPrime(nextprime+1);
                }
                arr.add(nextprime-(n-1));
                for (int j = 1; j < n-1; j++) {
                    boolean notok = true;
                    nextprime = nextPrime(nextprime);
                    while (isPrime(nextprime-(n-1))) {
                        nextprime = nextPrime(nextprime+1);
                    }
                    arr.add(nextprime-(n-1));
                }

                int sum = 0;
                for (int a : arr)
                    sum = sum + a;
                arr.add(nextPrime(sum));

                for (int j = 0; j < n-1; j++) {
                    for(int k = 0; k < n-1; k++) {
                        System.out.print("1 ");
                    }
                    System.out.println(arr.get(j) + "\n");
                }
                for(int j = 0; j <n; j++) {
                    System.out.print(arr.get(j) + " ");
                }
                System.out.println();
            }
        }
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class B {

    private static boolean isPrime(int n) {
        int factors = 0;
        for (int i=1; i <= n; i++) {
            if (n % i == 0) {
                factors++;
            }
        }
        if(factors == 2) return true;
        else return false;
    }
    private static int nextPrime(int n){
        while(true) {
            if (isPrime(++n)) return n;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());

        for (int i = 0; i < t; i++) {
            int n = Integer.parseInt(sc.nextLine());
            if (isPrime(n)) {
                for (int j = 0; j < n; j++) {
                    for(int k = 0; k < n; k++) {
                        System.out.print("1 ");
                    }
                    System.out.println();
                }
            } else {
                ArrayList<Integer> arr = new ArrayList();
                int nextprime = nextPrime(n-1);
                while (isPrime(nextprime-(n-1))) {
                    nextprime = nextPrime(nextprime);
                }
                arr.add(nextprime-(n-1));
                for (int j = 1; j < n-1; j++) {
                    boolean notok = true;
                    nextprime = nextPrime(nextprime);
                    while (isPrime(nextprime-(n-1))) {
                        nextprime = nextPrime(nextprime);
                    }
                    arr.add(nextprime-(n-1));
                }

                int sum = 0;
                for (int a : arr) sum = sum + a;

                nextprime = nextPrime(sum);
                while (isPrime(nextprime-sum)) {
                    nextprime = nextPrime(nextprime);
                }
                arr.add(nextprime-sum);

                for (int j = 0; j < n-1; j++) {
                    for(int k = 0; k < n-1; k++) {
                        System.out.print("1 ");
                    }
                    System.out.println(arr.get(j) + "\n");
                }
                for(int j = 0; j <n; j++) {
                    System.out.print(arr.get(j) + " ");
                }
                System.out.println();
            }
        }
    }
}