  q = gets.chomp.to_i

  q.times {

    n, m, x, y = gets.split.map(&:to_i)

    table = []
    n.times {
      row = gets.chomp
      table << row
    }

    d = []
    n.times { |i|
      d << []
      m.times {
        d[i] << 0
      }
    }

    n.times { |i|
      m.times { |j|

        if j == 0

          if table[i][j] == '.'
            d[i][j] = x
          end

          next
        end

        d[i][j] = d[i][j - 1]

        if table[i][j] == '*'
          next
        end

        d[i][j] = d[i][j - 1] + x

        if table[i][j - 1] == '.'
          curVal = 0
          if j == 1
            curVal = y
          else
            curVal = d[i][j - 2] + y
          end

          d[i][j] = [d[i][j], curVal].min
        end
      }
    }

    sum = 0
    d.each { |row|
      sum += row.last
    }

    puts sum
  }


t = input()
while t > 0:
	t -= 1
	n, m, x, y = [int(i) for i in raw_input().split()]
	cost = 0
	for i in range(n):
		st = raw_input()
		z = st.split('*')
		z = [i for i in z if i]
		for j in z:
			if len(j) == 1:
				cost += x
			else:
				e = len(j) / 2
				r = len (j) % 2
				cost += min(len(j) * x, e * y + r * x)
	print(cost)


import math
for _ in range(input()):
    n,m,x,y=map(int,raw_input().split())
    if y>=2*x:
        y=2*x
    ans=0
    for i in range(n):
        q=raw_input()
        currsize=0
        for j in range(m):
            if q[j]=="*":
                if currsize!=0:
                    ans+=(currsize/2)*y+(currsize%2)*x
                    currsize=0
            else:
                currsize+=1
        ans+=(currsize/2)*y+(currsize%2)*x
    print ans


import sys
import math

T = int(input())

for t in range(T):
    n, m, x, y = list(map(int, sys.stdin.readline().split()))
    cost = 0
    for i in range(n):
        a = sys.stdin.readline()

        if x <= 2 * y:
            cost += a.count('.') * x
            continue

        doubles = a.count('..')
        singles = a.count('.')
        cost += doubles * y + max(singles - 2 * doubles, 0) * x
    print(cost)


import sys
import math

T = int(input())

for t in range(T):
    n, m, x, y = list(map(int, sys.stdin.readline().split()))
    cost = 0
    for i in range(n):
        a = sys.stdin.readline()

        if y > 2 * x:
            cost += a.count('.') * x
            continue

        doubles = a.count('..')
        singles = a.count('.')
        cost += doubles * y + max(singles - 2 * doubles, 0) * x
    print(cost)


test = int(raw_input())
for m in range(test):
	n, m , s, b = map(int ,raw_input().split())
	mat = []
	for g in range(n):
		arr = raw_input()
		count = 0
		a= []
		for i in arr:
			if i == '.':
				count+=1
			elif i == '*' and count > 0:
				a.append(count)
				count = 0
			else:
				count = 0
		if(count > 0):
			a.append(count)
		mat.append(a)
	# print(mat)
	add = 0
	for a in mat:
		for i in a:
			if(i%2 == 0):
				if((i/2)*b < i*s ):
					add += (i/2)*b
				else:
					add += i*s
			else:
				if((i/2)*b  +s < i*s):
					add+= (i/2)*b  +s
				else:
					add += i*s
	# print("in ", n, m ,s, b)
	print(add)


def f(n,m,x,y,a):
 if y>=(2*x):
  return sum(sum(a[i][j]=='.' for j in xrange(m)) for i in xrange(n))*x
 else:
  out=0; run=0
  for line in a:
   for c in line:
    if c=='*': out+=(run/2)*y+(run%2)*x; run=0
    else: run+=1
   out+=(run/2)*y+(run%2)*x; run=0
  return out

for t in xrange(int(raw_input())):
 n,m,x,y=map(int,raw_input().split())
 a=[raw_input() for i in xrange(n)]
 print f(n,m,x,y,a)


one, two, inf = 0, 0, 10**18
dp = [-1 for _ in range(2000)]

def go(len):
    if len < 0:
        return inf
    if len == 0:
        return 0

    if dp[len] != -1:
        return dp[len]

    cost1 = one + go(len - 1)
    cost2 = two + go(len - 2)
    dp[len] = min(cost1, cost2)

    return dp[len]

t = int(raw_input())
while t > 0:
    t -= 1

    dp = [-1 for _ in range(2000)]
    n, m, x, y = map(int, raw_input().split())
    one, two = x, y

    ans = 0

    for i in range(n):
        s = raw_input()
        consec = []
        len = 0
        for id in range(m):
            if s[id] == '.':
                len += 1
            else :
                if len :
                    consec.append(len)
                len = 0
        if len :
            consec.append(len)

        for l in consec:
            ans += go(l)

    print(ans)


for _ in range(input()):
    n,m,x,y=[int(i) for i in raw_input().split()]
    a=[[i for i in raw_input()] for j in range(n)]
    if y>2*x or m==1:
        ct=0
        for i in range(n):
            for j in range(m):
                if a[i][j]=='.':
                    ct+=1
        print x*ct
    else:
        val=0
        for i in range(n):
            j=0
            while j<m:
                if j==m-1:
                    if a[i][j]=='.':
                        val+=x
                    j+=1
                    break
                if a[i][j]=='.' and a[i][j+1]=='.':
                    j+=1
                    val+=y
                elif a[i][j]=='.':
                    val+=x
                j+=1
        print val


#include <bits/stdc++.h>
using namespace std;
int main() {
    //cout<<"Hello World!";
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n][n];
        memset(arr,0,sizeof(arr));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j){
                    arr[i][j]=1;
                }
                else if(i+j==n-1){
                    arr[i][j]=1;
                }

            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<arr[i][j]<<" ";
            }
            cout<<endl;
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    //cout<<"Hello World!";
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n][n];
        memset(arr,0,sizeof(arr));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j){
                    arr[i][j]=1;
                }
                else if(i+j==n-1){
                    arr[i][j]=1;
                }
                if(i==0 && j==n/2){
                    arr[i][j]=1;
                }
                if(j==0 && i==n/2){
                    arr[i][j]=1;

                }

            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<arr[i][j]<<" ";
            }
            cout<<endl;
        }

    }
}


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

vector<int>primes;
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
void prime_no(int a)
{
	int root,ans =0;
	bool prime[a];
	vector<int>vec;
	memset(prime,true,a);
	root = sqrt(a);
	for(int i=2;i<=root;i=i+1)
	{
		for(int j=2*i;j<a;j=j+i)
		{
			prime[j] = false;
		}
	}
	for(int i=2;i<a;i=i+1)
	{
		if(prime[i])
		{
			primes.push_back(i);
		}
	}
}
int main()
{
    prime_no(100);
    int size = primes.size();
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int sum = 0;
        vector<int>vec;
        for(int i=2;i<=n+1;i=i+1)
        {
            vec.push_back(i);
            sum = sum + i;
        }
        int isize = vec.size();
        for(int i=0;i<size;i=i+1)
        {
            if(sum==primes[i])
            {
                break;
            }
            if(primes[i]>sum)
            {
                if(primes[i] - sum > vec[isize-1])
                {
                    if(!isPrime(primes[i] - sum + vec[isize-1]))
                    {
                        vec[isize-1] = primes[i] - sum + vec[isize-1];
                        break;
                    }
                }
            }
        }
        int ans[n][n];
        for(int i=0;i<n;i=i+1)
        {
            for(int j=0;j<n;j=j+1)
            {
                ans[i][j] = vec[j];
            }
            rotate(vec.begin(), vec.begin()+1, vec.end());
        }
        for(int i=0;i<n;i=i+1)
        {
            for(int j=0;j<n;j=j+1)
            {
                cout<<ans[i][j]<<" ";
            }cout<<endl;
        }
    }
    return 0;
}


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

vector<int>primes;
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
void prime_no(int a)
{
	int root,ans =0;
	bool prime[a];
	vector<int>vec;
	memset(prime,true,a);
	root = sqrt(a);
	for(int i=2;i<=root;i=i+1)
	{
		for(int j=2*i;j<a;j=j+i)
		{
			prime[j] = false;
		}
	}
	for(int i=2;i<a;i=i+1)
	{
		if(prime[i])
		{
			primes.push_back(i);
		}
	}
}
int main()
{
    prime_no(100);
    int size = primes.size();
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int sum = 0;
        vector<int>vec;
        for(int i=1;i<=n;i=i+1)
        {
            vec.push_back(i);
            sum = sum + i;
        }
        int isize = vec.size();
        for(int i=0;i<size;i=i+1)
        {
            if(sum==primes[i])
            {
                break;
            }
            if(primes[i]>sum)
            {
                if(primes[i] - sum > vec[isize-1])
                {
                    if(!isPrime(primes[i] - sum + vec[isize-1]))
                    {
                        vec[isize-1] = primes[i] - sum + vec[isize-1];
                        break;
                    }
                }
            }
        }
        int ans[n][n];
        for(int i=0;i<n;i=i+1)
        {
            for(int j=0;j<n;j=j+1)
            {
                ans[i][j] = vec[j];
            }
            rotate(vec.begin(), vec.begin()+1, vec.end());
        }
        for(int i=0;i<n;i=i+1)
        {
            for(int j=0;j<n;j=j+1)
            {
                cout<<ans[i][j]<<" ";
            }cout<<endl;
        }
    }
    return 0;
}


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

vector<int>primes;
void prime_no(int a)
{
	int root,ans =0;
	bool prime[a];
	vector<int>vec;
	memset(prime,true,a);
	root = sqrt(a);
	for(int i=2;i<=root;i=i+1)
	{
		for(int j=2*i;j<a;j=j+i)
		{
			prime[j] = false;
		}
	}
	for(int i=2;i<a;i=i+1)
	{
		if(prime[i])
		{
			primes.push_back(i);
		}
	}
}
int main()
{
    prime_no(100);
    int size = primes.size();
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int sum = 0;
        vector<int>vec;
        for(int i=1;i<=n;i=i+1)
        {
            vec.push_back(i);
            sum = sum + i;
        }
        int isize = vec.size();
        for(int i=0;i<size;i=i+1)
        {
            if(sum==primes[i])
            {
                break;
            }
            if(primes[i]>sum)
            {
                if(primes[i]-sum > vec[isize-1])
                {
                    vec[isize-1] = primes[i] - sum + vec[isize-1];
                    break;
                }
            }
        }
        int ans[n][n];
        for(int i=0;i<n;i=i+1)
        {
            for(int j=0;j<n;j=j+1)
            {
                ans[i][j] = vec[j];
            }
            rotate(vec.begin(), vec.begin()+1, vec.end());
        }
        for(int i=0;i<n;i=i+1)
        {
            for(int j=0;j<n;j=j+1)
            {
                cout<<ans[i][j]<<" ";
            }cout<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool isprime(int n)
{
    if(n<=1)
    {
        return false;
    }
    for(int i=2;i<n;i=i+1)
    {
        if(n%i == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<int>vec;
        int sum = 1;
        vec.push_back(sum);
        for(int i=0;vec.size() < n && i<10000;i=i+1)
        {
            if(isprime(i)==false && isprime(sum+i)==true)
            {
                vec.push_back(i);
                sum = sum+i;
            }
        }
        for(int i=0;i<n;i=i+1)
        {
            int t = i;
            while(t<n)
            {
                cout<<vec[t]<<" ";
                t++;
            }
            t = i-1;
            while(t>=0)
            {
                cout<<vec[t]<<" ";
                t--;
            }
            cout<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool isprime(int n)
{
    if(n<=1)
    {
        return false;
    }
    for(int i=2;i<n;i=i+1)
    {
        if(n%i == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<int>vec;
        int sum = 1;
        vec.push_back(sum);
        for(int i=2;vec.size() < n && i<10000;i=i+1)
        {
            if(isprime(i)==false && isprime(sum+i)==true)
            {
                vec.push_back(i);
                sum = sum+i;
            }
        }
        for(int i=0;i<n;i=i+1)
        {
            int t = i;
            while(t<n)
            {
                cout<<vec[t]<<" ";
                t++;
            }
            int p =0;
            t = i-1;
            while(p<=t)
            {
                cout<<vec[p]<<" ";
                p++;
            }
            cout<<endl;
        }
    }
    return 0;
}


#include<iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;

    while(t--)
    {
        int n;
    cin>>n;

    int a=0,b=1;
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(j==a || j==b)
            {
                cout<<"1 ";
                continue;
            }

            cout<<"0 ";
        }

        cout<<endl;
        a++;
        b++;
    }

    for(int i=0;i<n;i++)
    {
        if(i==0 || i==n-1)
        cout<<"1 ";
        else
        cout<<"0 ";

    }
    cout<<endl;

    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
    int size = sizeof(primes) / sizeof(primes[0]);

    while (t--)
    {
        int n;
        cin >> n;

        auto x = upper_bound(primes, primes + size, n);
        int k = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (j == k)
                {
                    cout << (*x - (n - 1)) << " ";

                    continue;
                }
                cout << 1 << " ";
            }
            k++;

            cout << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
    int size = sizeof(primes) / sizeof(primes[0]);

    while (t--)
    {
        int n;
        cin >> n;

        auto x = upper_bound(primes, primes + size, n);

        while (binary_search(primes, primes + n, *x - (n - 1)))
        {
            x++;
        }

        int k = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (j == k)
                {
                    cout << (*x - (n - 1)) << " ";

                    continue;
                }
                cout << 1 << " ";
            }
            k++;

            cout << endl;
        }
    }
    return 0;
}


#include<iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;

    int a=0,b=1;
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(j==a || j==b)
            {
                cout<<"1 ";
                continue;
            }

            cout<<"0 ";
        }

        cout<<endl;
        a++;
        b++;
    }

    for(int i=0;i<n;i++)
    {
        if(i==0 || i==n-1)
        cout<<"1 ";
        else
        cout<<"0 ";

    }
    cout<<endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107};
    int size = sizeof(primes) / sizeof(primes[0]);

    while (t--)
    {
        int n;
        cin >> n;

        auto x = upper_bound(primes, primes + size, n);

        while (binary_search(primes, primes + n, *x - (n - 1)))
        {
            x++;
        }

        int k = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (j == k)
                {
                    cout << (*x - (n - 1)) << " ";

                    continue;
                }
                cout << 1 << " ";
            }
            k++;

            cout << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

bool checknt(int n)
{
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            return false;
        }
    }
    return true;
}

void process()
{
    int n;
    cin >> n;
    if(checknt(n))
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << "1 ";
            }
            cout << endl;
        }
    }
    else
    {
        int temp = 0;
        for (int i = n; i <= 1000; i++)
        {
//            cout << i << " ";
            if(checknt(i))
            {
                temp = i;
                break;
//                cout << temp << endl;
            }
        }
//        cout << endl;
        int a[101][101];
        memset(a, 0, sizeof(a));
        a[1][1] = temp - 1;
        a[1][2] = 1;
        a[n][1] = 1;
        for(int i = 3; i <= n; i++) a[1][i] = 0;
        for(int i = 2; i <= n; i++) a[i][i] = a[1][1];
        for(int i = 1; i <= n-1; i++) a[i][i+1] = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        process();
    }
}


#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

bool checknt(int n)
{
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int t;
    cin >> t;
    for(int z = 1; z <= t; z++)
    {
        int k, n, a[101][101];
        cin >> n;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                a[i][j] = 0;
            }
        }
        for(int i = 1; i <= n; i++)
        {
            a[i][i] = n;
        }
        k = 2*n - 1;
        while (checknt(k) == false)
        {
            k++;
        }

        for(int i = 1; i <= n-1; i++)
        {
            a[i][i+1] = k - 2*n + 2;
        }
        a[n][1] = k - 2*n + 2;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(a[i][j] == 0)
                {
                    a[i][j] = 1;
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
}


#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

bool checknt(int n)
{
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int t;
    cin >> t;
    for(int z = 1; z <= t; z++)
    {
        int k, n, a[101][101];
        memset(a, 0, sizeof(0));
        cin >> n;
        for(int i = 1; i <= n; i++)
        {
            a[i][i] = n;
        }
        k = 2*n - 1;
        while (checknt(k) == false)
        {
            k++;
        }

        for(int i = 1; i <= n-1; i++)
        {
            a[i][i+1] = k - 2*n + 2;
        }
        a[n][1] = k - 2*n + 2;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(a[i][j] == 0)
                {
                    a[i][j] = 1;
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
}


#include <iostream>
#include <algorithm>

using namespace std;

bool checknt(int n)
{
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int t;
    cin >> t;
    for(int z = 1; z <= t; z++)
    {
        int k, n, a[101][101];
        memset(a, 0, sizeof(0));
        cin >> n;
        for(int i = 1; i <= n; i++)
        {
            a[i][i] = n;
        }
        k = 2*n - 1;
        while (checknt(k) == false)
        {
            k++;
        }

        for(int i = 1; i <= n-1; i++)
        {
            a[i][i+1] = k - 2*n + 2;
        }
        a[n][1] = k - 2*n + 2;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(a[i][j] == 0)
                {
                    a[i][j] = 1;
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstring>

using namespace std;

bool checknt(int n)
{
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            return false;
        }
    }
    return true;
}

void process()
{
    int n;
    cin >> n;
    if(checknt(n))
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << "1 ";
            }
            cout << endl;
        }
    }
    else
    {
        int temp = 0;
        for (int i = n; i <= 1000; i++)
        {
//            cout << i << " ";
            if(checknt(i))
            {
                temp = i;
                break;
//                cout << temp << endl;
            }
        }
//        cout << endl;
        int a[101][101];
        memset(a, 0, sizeof(a));
        a[1][1] = temp - 1;
        a[1][2] = 1;
        a[n][1] = 1;
        for(int i = 3; i <= n; i++) a[1][i] = 0;
        for(int i = 2; i <= n; i++) a[i][i] = a[1][1];
        for(int i = 1; i <= n-1; i++) a[i][i+1] = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        process();
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

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

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n;
    cin >> n;

    ll a[n][n];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j] = 0;
            if(i<2||j<2)
            {
                a[i][j] = 1;
            }
        }
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   cin >> t;
    while(t--)
    {
       tc();
    }

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

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

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n;
    cin >> n;

    ll a[n][n];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j] = 0;
        }
    }

    if(n%2)
    {
        a[0][n/2] = 1;
    }

    for(int i=0;i<n;i++)
    {
        for(int i=0;i<n;i++)
        {
            if(i==j || i+j == n-1)
            {
                cout << 1 << " ";
            }
            else
            {
                cout << a[i] << " ";
            }
        }
        cout << endl;
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   cin >> t;
    while(t--)
    {
       tc();
    }

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

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

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n;
    cin >> n;

    ll a[n][n];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j] = 0;
            if(i<2&&j<2)
            {
                a[i][j] = 1;
            }
        }
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   cin >> t;
    while(t--)
    {
       tc();
    }

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

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

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n;
    cin >> n;

    ll a[n][n];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j] = 0;
        }
    }

    if(n%2)
    {
        a[0][n/2] = 1;
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j || i+j == n-1)
            {
                cout << 1 << " ";
            }
            else
            {
                cout << a[i][j] << " ";
            }
        }
        cout << endl;
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   cin >> t;
    while(t--)
    {
       tc();
    }

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

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

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n;
    cin >> n;

    ll a[n][n];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j] = 0;
        }
    }

    if(n%2)
    {
        a[n/2][0] = 1;
        a[0][n/2] = 1;
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j || i+j == n-1)
            {
                cout << 1 << " ";
            }
            else
            {
                cout << a[i][j] << " ";
            }
        }
        cout << endl;
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   cin >> t;
    while(t--)
    {
       tc();
    }

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

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

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n;
    cin >> n;

    ll a[n][n];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j] = 0;
        }
    }

    if(n%2)
    {
        a[n/2][0] = 1;
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j || i+j == n-1)
            {
                cout << 1 << " ";
            }
            else
            {
                cout << a[i][j] << " ";
            }
        }
        cout << endl;
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   cin >> t;
    while(t--)
    {
       tc();
    }

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

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

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n;
    cin >> n;

    ll a[n][n];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j] = 0;
        }
    }

    if(n%2)
    {
        a[0][n/2] = 1;
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j || i+j == n-1)
            {
                cout << 1 << " ";
            }
            else
            {
                cout << a[i] << " ";
            }
        }
        cout << endl;
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   cin >> t;
    while(t--)
    {
       tc();
    }

}


#include <bits/stdc++.h>
#define ll long long
#define MAX 105

using namespace std;

ll arr[MAX];

vector<ll> primes;

void solve() {

    for(ll i = 2; i <= 10005; i++) {
        ll is_prime = 1;
        for(ll j = 2; j*j <= i; j++) {
            if(i == j)
                continue;
            if(i % j == 0)
                is_prime = 0;
        }
        if (is_prime) {

            primes.push_back(i);
        }
    }

}

int main() {
    ll t;
    cin>>t;
    solve();
    while(t--) {
        ll n;
        cin>>n;
        //Solve here
        ll res[MAX][MAX];
        for(ll i = 1; i<= n; i++)
            for(ll j = 1; j <=n; j++)
                res[i][j] = 2;
        //populate at critical points
        ll idx = 1;
        ll adhoc = 2*(n - 1) + 1;
        ll next_prime = upper_bound(primes.begin(), primes.end(), adhoc) - primes.begin();
        ll delta = primes[next_prime] - adhoc;
        //cout<<adhoc<<" "<<primes[next_prime]<<" "<<delta<<endl;
        while(idx <= n) {
            res[idx][idx] = 1;
            idx++;
        }

        idx = n;

       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++) {
                if (i + j == n + 1)
                    res[i][j] += delta;
            }
       }
       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++)
                cout<<res[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define MAX 105

using namespace std;

ll arr[MAX];

vector<ll> primes;

void solve() {

    for(ll i = 2; i <= 10005; i++) {
        ll is_prime = 1;
        for(ll j = 2; j*j <= i; j++) {
            if(i == j)
                continue;
            if(i % j == 0)
                is_prime = 0;
        }
        if (is_prime) {

            primes.push_back(i);
        }
    }

}

int main() {
    ll t;
    cin>>t;
    solve();
    while(t--) {
        ll n;
        cin>>n;
        //Solve here
        ll res[MAX][MAX];
        for(ll i = 1; i<= n; i++)
            for(ll j = 1; j <=n; j++)
                res[i][j] = 0;
        //populate at critical points

        for(ll i = 1; i<= n; i++) {
            for(ll j = 1; j <=n; j++) {
                if (i + j == n + 1 || i  == j)
                    res[i][j] = 1;
            }
        }

       if(n%2)
        res[n/2 + 1][n/2 + 1] = 2;
       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++)
                cout<<res[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define MAX 105

using namespace std;

ll arr[MAX];

vector<ll> primes;

void solve() {

    for(ll i = 2; i <= 10005; i++) {
        ll is_prime = 1;
        for(ll j = 2; j*j <= i; j++) {
            if(i == j)
                continue;
            if(i % j == 0)
                is_prime = 0;
        }
        if (is_prime) {

            primes.push_back(i);
        }
    }

}

int main() {
    ll t;
    cin>>t;
    solve();
    while(t--) {
        ll n;
        cin>>n;
        //Solve here
        ll res[MAX][MAX];
        for(ll i = 1; i<= n; i++)
            for(ll j = 1; j <=n; j++)
                res[i][j] = 0;
        //populate at critical points

        for(ll i = 1; i<= n; i++) {
            for(ll j = 1; j <=n; j++) {
                if (i + j == n + 1 || i  == j)
                    res[i][j] = 1;
            }
        }

       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++)
                cout<<res[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define MAX 105

using namespace std;

ll arr[MAX];

vector<ll> primes;

void solve() {

    for(ll i = 2; i <= 10005; i++) {
        ll is_prime = 1;
        for(ll j = 2; j*j <= i; j++) {
            if(i == j)
                continue;
            if(i % j == 0)
                is_prime = 0;
        }
        if (is_prime) {

            primes.push_back(i);
        }
    }

}

int main() {
    ll t;
    cin>>t;
    solve();
    while(t--) {
        ll n;
        cin>>n;
        //Solve here
        ll res[MAX][MAX];
        for(ll i = 1; i<= n; i++)
            for(ll j = 1; j <=n; j++)
                res[i][j] = 0;
        //populate at critical points

        for(ll i = 1; i<= n; i++) {
            for(ll j = 1; j <=n; j++) {
                if (i + j == n + 1 || i  == j)
                    res[i][j] = 1;
            }
        }

       if(n%2) {
            res[1][n/2 + 1] = 1;
            res[n/2 + 1][1] = 1;
       }
       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++)
                cout<<res[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define MAX 105

using namespace std;

ll arr[MAX];

int main() {
    ll t;
    cin>>t;
    while(t--) {
        ll n;
        cin>>n;
        //Solve here
        ll res[MAX][MAX];
        for(ll i = 1; i<= n; i++)
            for(ll j = 1; j <=n; j++)
                res[i][j] = 2;
        //populate at critical points
        ll idx = 1;
        ll val = 809 - 2*(n - 1);
        while(idx <= n) {
            res[idx][idx] = val;
            idx++;
        }
        for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++)
                cout<<res[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define MAX 105

using namespace std;

ll arr[MAX];

vector<ll> primes;

void solve() {

    for(ll i = 2; i <= 10005; i++) {
        ll is_prime = 1;
        for(ll j = 2; j*j <= i; j++) {
            if(i == j)
                continue;
            if(i % j == 0)
                is_prime = 0;
        }
        if (is_prime) {

            primes.push_back(i);
        }
    }

}

int main() {
    ll t;
    cin>>t;
    solve();
    while(t--) {
        ll n;
        cin>>n;
        //Solve here
        ll res[MAX][MAX];
        for(ll i = 1; i<= n; i++)
            for(ll j = 1; j <=n; j++)
                res[i][j] = 4;
        //populate at critical points
        ll idx = 1;
        ll adhoc = 4*(n - 1) + 1;
        ll next_prime = upper_bound(primes.begin(), primes.end(), adhoc) - primes.begin();
        ll delta = primes[next_prime] - adhoc;
        //cout<<adhoc<<" "<<primes[next_prime]<<" "<<delta<<endl;
        while(idx <= n) {
            res[idx][idx] = 1;
            idx++;
        }

        idx = n;

       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++) {
                if (i + j == n + 1)
                    res[i][j] += delta;
            }
       }
       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++)
                cout<<res[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define MAX 105

using namespace std;

ll arr[MAX];

vector<ll> primes;

void solve() {

    for(ll i = 2; i <= 10005; i++) {
        ll is_prime = 1;
        for(ll j = 2; j*j <= i; j++) {
            if(i == j)
                continue;
            if(i % j == 0)
                is_prime = 0;
        }
        if (is_prime) {

            primes.push_back(i);
        }
    }

}

int main() {
    ll t;
    cin>>t;
    solve();
    while(t--) {
        ll n;
        cin>>n;
        //Solve here
        ll res[MAX][MAX];
        for(ll i = 1; i<= n; i++)
            for(ll j = 1; j <=n; j++)
                res[i][j] = 0;
        //populate at critical points

        for(ll i = 1; i<= n; i++) {
            for(ll j = 1; j <=n; j++) {
                if (i + j == n + 1 || i  == j)
                    res[i][j] = 1;
            }
        }

       if(n%2)
            res[1][n/2 + 1] = 1;
       for(ll i = 1; i <= n; i++) {
            for(ll j = 1; j <=n; j++)
                cout<<res[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return false;

    return true;

  }
  void solve()
  {
    int n;
    cin>>n;

    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 9;

    int temp = 9*n;

      while(!check(temp))
        temp++;

      final = temp - 9*n;

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    if(n == 1)
      return true;
    if(n <4)
      return false;

    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return true;

    return false;

  }
  void solve()
  {
    int n;
    cin>>n;

    if(n == 2)
    {
      cout<<1<<" "<<1<<endl<<1<<" "<<1<<endl;
      return;
    }
    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 1;

    int temp = n;

      aa:
      while(check(temp))
      {
        temp++;
      }

      final = temp - n ;

      if(!check(final + 1))
      {
        temp++;
        goto aa;
      }

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return false;

    return true;

  }
  void solve()
  {

   int n; cin>>n;

    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 9;

    if(n%2)
    {
      for(int i=0;i<n-1;i++)
        v[i][n-1] = 10;

      v[n-1][0] = 10;
    }

      int temp = 9*n;

      if(n%2)
        temp++;

      while(!check(temp))
        temp++;

      final = temp - 9*n;
      if(n%2)
        final--;

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return false;

    return true;

  }
  void solve()
  {
    int n;
    cin>>n;

    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 1;

    int temp = n;

      while(!check(temp))
        temp++;

      final = temp - n;

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  void solve()
  {
    int n; cin>>n;
    int v[n][n];

    memset(v,0,sizeof(v));

    int x = 0;
    for(int i=0;i<n;i++)
    {
      v[i][i] =9;
      v[i][(i+1)%n] = 8;
    }

    for(int i = 0 ;i < n;i ++ )
    {
      for(int j=0 ;j <n ;j++)
        cout<<v[i][j]<<" ";
      cout<<endl;
    }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return false;

    return true;

  }
  void solve()
  {

   int n; cin>>n;

    int v[n][n];

    int put ;

    if(n%2 == 0)
      put = 9;
    else
      put = 1;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = put;

    int temp;

    if(n%2 == 0)
      temp = 9*n;
    else
      temp = n;

      while(!check(temp))
        temp++;

    if(n%2==0)
      final = temp - 9*n;
    else
      final = temp - n;

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }

  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return false;

    return true;

  }
  void solve()
  {
    int n;
    cin>>n;

    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 9;

    int temp = 9*n;

      while(!check(temp))
        temp++;

      final = temp - n;

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    if(n == 1)
      return true;
    if(n <4)
      return false;

    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return true;

    return false;

  }
  void solve()
  {
    int n;
    cin>>n;

    if(n == 2)
    {
      cout<<1<<" "<<1<<endl<<1<<" "<<1<<endl;
      return;
    }
    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 1;

    int temp = n;

      aa:
      while(check(temp))
      {
        temp++;
      }

      final = temp - n + 1;

      if(!check(final + 1))
      {
        temp++;
        goto aa;
      }

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return false;

    return true;

  }
  void solve()
  {

   int n; cin>>n;

    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 9;

    if(n%2)
    {
      for(int i=0;i<n;i++)
        v[i][n-i-1] = 10;
    }

      int temp = 9*n;

      if(n%2)
        temp++;

      while(!check(temp))
        temp++;

      final = temp - 9*n;
      if(n%2)
        final--;

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          if(i == n/2 && j==n/2 && n%2)
            v[i][j] = v[0][0] + 10;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


  #include<bits/stdc++.h>
  #define pb push_back
  #define all(v) v.begin(),v.end()
  #define allr(v) v.rbegin(),v.rend()
  #define SORT(v) sort(all(v))
  #define ll long long
  #define int ll
  #define ff first
  #define ss second
  #define ios ios::sync_with_stdio(0);cin.tie(0);
  #define mod 1000000007

  using namespace std;

  void oj()
    {
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
    }

  int final = 0;

  bool check(int n)
  {
    if(n == 1)
      return true;
    if(n <4)
      return false;

    for(int i=2;i*i<=n;i++)
      if(n%i == 0)
        return true;

    return false;

  }
  void solve()
  {
    int n;
    cin>>n;

    int v[n][n];

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        v[i][j] = 1;

    int temp = n;

      aa:
      while(check(temp))
      {
        temp++;
      }

      final = temp - n + 1;

      if(!check(final + 1))
      {
        temp++;
        goto aa;
      }

      for(int i=0;i<n;i++)
      {
        for(int j=0;j<n;j++)
        {
          if(i==j)
            v[i][j] += final;

          cout<<v[i][j]<<" ";
        }
        cout<<endl;
      }
  }
  signed main()
  {
    oj(); ios;
    int t; cin>>t;
    while(t--)
      solve();

    return 0;
  }


#include <bits/stdc++.h>

using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int n,i,j;
cin>>n;
if(n%2!=0){for(i=0;i<n;i++){for(j=0;j<n;j++){cout<<"1 ";}cout<<"\n";}}
else{for(i=0;i<n;i++){for(j=0;j<n;j++){if(i!=j){cout<<"1 ";}
	else{cout<<n<<" ";}
}cout<<"\n";}

}

}	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int n,i,j;
cin>>n;
if(n%2!=0||n==2){for(i=0;i<n;i++){for(j=0;j<n;j++){cout<<"1 ";}cout<<"\n";}}
else{for(i=0;i<n;i++){for(j=0;j<n;j++){if(i!=j){cout<<"1 ";}
	else{cout<<n<<" ";}
}cout<<"\n";}

}

}	return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n%2!=0){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        a[n/2][(n/2)-1] =1; a[n/2][(n/2)+1]=1;

        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else if(n%2==0){
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n%2!=0){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        rep(i,0,n){
            a[n/2][i]=9; a[i][n/2]=9;
        }

        a[n/2][n/2]=1;
        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else{
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }

return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n%2!=0){
        l a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        a[n/2][n/2] = 2;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else if(n%2==0){
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }

return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n%2!=0){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        a[n/2][n/2] = 2;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else if(n%2==0){
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }

return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n%2!=0){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        a[n/2][(n/2)-1] =1; a[n/2][(n/2)+1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else if(n%2==0){
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }

return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n&1){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        rep(i,0,n){
            a[n/2][i]=9;
        }
        rep(i,0,n){
            a[i][n/2]=9;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else{
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }

return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n%2!=0){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        a[n/2][(n+1)/2] = 1 ; a[(n+1)/2][((n+1)/2)+1]=1;
        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else if(n%2==0){
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }

return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n&1){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        rep(i,0,n){
            a[n/2][i]=9;
        }
        rep(i,0,n){
            a[i][n/2]=9;
        }
        a[n/2][n/2]=1;
        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else{
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }

return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
typedef long long int ll;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD (ll)(1000000000+7)
#define MAXN 100000+1
#define pb push_back
#define mp make_pair
#define TEST_CASES  ll t;cin >> t;while(t--)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,b,a) for(ll i=(b-1);i>=a;i--)
bool sortbysec(const pair<ll,ll>&a,const pair<ll,ll>&b){ return (a.second < b.second) ; }
ll max(ll a, ll b){ if(a>b) return a ; return b; }
ll min(ll a, ll b){ if(a<b) return a ; return b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);  }
void cp(){
    ll n;cin>>n;
    if(n%2!=0){
        ll a[n][n];memset(a,0,sizeof(a));
        rep(i,0,n){
            a[i][i]=1;
            a[i][n-i-1]=1;
        }
        a[(n/2)-1][n/2] =1; a[n/2][(n/2)+1]=1;

        rep(i,0,n){
            rep(j,0,n){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    else if(n%2==0){
        ll b[n][n];memset(b,0,sizeof(b));
        rep(i,0,n){
            b[i][i]=1;
            b[i][n-i-1]=1;
        }
        rep(i,0,n){
            rep(j,0,n){
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    return;
}
int main(){
fio ;
TEST_CASES{
cp();
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;

    if((n%2==0 orr n%3==0 orr n%5==0)&&n>3){
        loop(i,0,n){
            loop(j,0,n){
                if(i==j)cout<<"1"<<" ";
                else cout<<n<<" ";
            }
            cout<<endl;
        }

    }

    else if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    if(n>3){
    if(n%2==0 orr n%3==0){
        loop(i,0,n){
            loop(j,0,n){
                if(i==j)cout<<"1"<<" ";
                else cout<<n<<" ";
            }
            cout<<endl;
        }

    }
    }
    else if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
      if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }

    else {
        loop(i,0,n){
            loop(j,0,n){
                if(i==j)cout<<"28"<<" ";
               else if(i==0 && j==n-1){
                    cout<<"1"<<" ";
                }
                else if(i-j==1){
                    cout<<"1"<<" ";
                }
                else{
                    cout<<"0"<<" ";
                }
            }
            cout<<endl;
        }

    }

}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    if(n>3){
    if(n%2==0 orr n%3==0){
        loop(i,0,n){
            loop(j,0,n-1)cout<<n<<" ";
            cout<<"1"<<endl;
        }

    }
    }
    else if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
      if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }

    else if((n%2==0 orr n%3==0 orr n%5==0 orr n%7==0)&&n>3){
        loop(i,0,n){
            loop(j,0,n){
                if(i==j)cout<<"1"<<" ";
                else cout<<n<<" ";
            }
            cout<<endl;
        }

    }

}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;

    if((n%2==0 orr n%3==0 orr n%5==0 n%7==0)&&n>3){
        loop(i,0,n){
            loop(j,0,n){
                if(i==j)cout<<"1"<<" ";
                else cout<<n<<" ";
            }
            cout<<endl;
        }

    }

    else if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;

    if((n%2==0 orr n%3==0 orr n%5==0 orr n%7==0)&&n>3){
        loop(i,0,n){
            loop(j,0,n){
                if(i==j)cout<<"1"<<" ";
                else cout<<n<<" ";
            }
            cout<<endl;
        }

    }

    else if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define orr ||
#define int long long
#define pb push_back
#define usi unordered_set<int>
#define umii unordered_map<int,int>
#define mp make_pair
#define pl pair<int,int>
#define vl vector<int>
#define endl ("\n")
#define pi (3.141592653589)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define loop(i,k,n) for(int i=k;i<n;i++)

void fastio()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cout<<fixed<<setprecision(10);
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}
int32_t main(){
    fastio();

int t;
cin>>t;
while(t--){
    int n;
    cin>>n;

    if((n%2==0 orr n%3==0)&&n>3){
        loop(i,0,n){
            loop(j,0,n){
                if(i==j)cout<<"1"<<" ";
                else cout<<n<<" ";
            }
            cout<<endl;
        }

    }

    else if(isPrime(n)){
        loop(i,0,n){
            loop(j,0,n)cout<<"1"<<" ";
            cout<<endl;
        }

    }
}
return 0;
}


#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
bool isPrime(int n)
{
    if (n < 2)
    {
        return false;
    }
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    //freopen("light.in", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        if (isPrime(n))
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    printf("1%c", (j == n - 1) ? '\n' : ' ');
                }
            }
        }
        else
        {
            int x = 1;
            while (1)
            {
                if (!isPrime(x) && isPrime(n - 1 + x))
                {
                    break;
                }
            }
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (i == j)
                    {
                        printf("%d", x);
                    }
                    else
                    {
                        printf("1");
                    }
                    printf("%c", j == n ? '\n' : ' ');
                }
            }
        }
    }
    return 0;
}
/*
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
*/


#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
bool isPrime(int n)
{
    if (n < 2)
    {
        return false;
    }
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    //freopen("light.in", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        if (isPrime(n))
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    printf("1%c", (j == n - 1) ? '\n' : ' ');
                }
            }
        }
        else
        {
            int x = 1;
            while (1)
            {
                if (!isPrime(x) && isPrime(n - 1 + x))
                {
                    break;
                }
                x++;
            }
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (i == j)
                    {
                        printf("%d", x);
                    }
                    else
                    {
                        printf("1");
                    }
                    printf("%c", j == n ? '\n' : ' ');
                }
            }
        }
    }
    return 0;
}
/*
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
*/


#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
bool isPrime(int n)
{
    if (n < 2)
    {
        return false;
    }
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    //freopen("light.in", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                printf("%d%c", (j == i || (n-j+1 ==i)), j == n ? '\n' : ' ');
            }
        }
    }
    return 0;
}
/*
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
*/


#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main()
{
    //freopen("light.in", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        int temp = n;
        for (int i = n;; i++)
        {
            int f = 0;
            for (int j = 2; j <= sqrt(i); j++)
            {
                if (i % j == 0)
                {
                    f = 1;
                    break;
                }
            }
            if (!f)
            {
                temp = i;
                break;
            }
        }
        temp = temp - n;
        if (temp & 1)
        {
            temp /= 2;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == 0)
                    {
                        printf("4");
                    }
                    else
                    {
                        if (temp)
                        {
                            printf("3");
                        }
                        else
                        {
                            printf("1");
                        }
                    }
                    printf("%c", j == n - 1 ? '\n' : ' ');
                }
                temp--;
            }
        }
        else
        {
            temp /= 2;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (temp)
                    {
                        printf("3");
                    }
                    else
                    {
                        printf("1");
                    }
                    printf("%c", j == n - 1 ? '\n' : ' ');
                }
                temp--;
            }
        }
    }
    return 0;
}
/*
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
*/


#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
bool isPrime(int n)
{
    if (n < 2)
    {
        return false;
    }
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    //freopen("light.in", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                printf("%d%c", (j == i || (n-j+1 ==i)||((n&1)&&j==(n/2+1)&&i==1)), j == n ? '\n' : ' ');
            }
        }
    }
    return 0;
}
/*
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
*/


#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(),a.end()
#define push_back pb

typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;

const ll MOD = 1e9 + 7;

ll mod_mul(ll x , ll y) {return ((x%MOD)*(y%MOD)) % MOD; }
ll mod_add(ll x , ll y) {return ((x%MOD)+(y%MOD)) % MOD; }

// Lets write some important stuff

// Returns factorial of n the mod way
ll fact(ll n)
{
    ll res = 1;
    for (ll i = 2; i <= n; i++)
        res = mod_mul(res , i );
    return res;
}

// nPr the mod way
ll nPr( ll n , ll r)
{
	ll res = 1;
	for(ll i = n , j = 1 ; j <=r  ; i-- , j ++ )
	{
		res = mod_mul( res , i );
	}
	return res % MOD;
}

/*/ CODE BEGINS FROM HERE    */

bool isPrime(int num){
    bool flag=true;
    for(int i = 2; i <= num / 2; i++) {
       if(num % i == 0) {
          flag = false;
          break;
       }
    }
    return flag;
}

ll nextPrime(ll sum)
{
	for(ll i = sum ; ; i ++)
	{
		if(isPrime(i))
		{
			return i ;
		}
	}
}

void solve()
{
	ll n ;
	cin >> n;
	ll matrix[n][n];

	for( ll i = 0 ; i < n-1 ; i ++)
	{
		for( ll j = 1 ; j < n ; j ++)
		{
			matrix[i][j] = 2;
		}
	}

	ll sum = 0 ;
	for(ll i = 0 ; i < n - 1 ; i ++ )
	{
		sum = 0;
		for(ll j = 1 ; j < n ; j ++) sum += matrix[i][j];
		matrix[i][0] = nextPrime(sum);
	}

	for(ll j = 1 ; j < n ; j++)
	{
		sum = 0 ;
		for(ll i = 0 ; i < n-1 ; i ++) sum += matrix[i][j];
		matrix[n][j] = nextPrime(sum);
	}

	for(ll i = 0 ; i < n ; i ++)
	{
		for( ll j = 0 ; j < n ; j++)
		{
			cout << matrix[i][j] << " ";
		}
		cout << "\n" ;
	}

	return ;
}

int main()
{
	ll t ;
	cin >> t ;
	for(ll i = 0 ; i < t ; i ++)
	{
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(),a.end()
#define push_back pb

typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;

const ll MOD = 1e9 + 7;

ll mod_mul(ll x , ll y) {return ((x%MOD)*(y%MOD)) % MOD; }
ll mod_add(ll x , ll y) {return ((x%MOD)+(y%MOD)) % MOD; }

// Lets write some important stuff

// Returns factorial of n the mod way
ll fact(ll n)
{
    ll res = 1;
    for (ll i = 2; i <= n; i++)
        res = mod_mul(res , i );
    return res;
}

// nPr the mod way
ll nPr( ll n , ll r)
{
	ll res = 1;
	for(ll i = n , j = 1 ; j <=r  ; i-- , j ++ )
	{
		res = mod_mul( res , i );
	}
	return res % MOD;
}

/*/ CODE BEGINS FROM HERE    */

bool isPrime(int num){
    bool flag=true;
    for(int i = 2; i <= num / 2; i++) {
       if(num % i == 0) {
          flag = false;
          break;
       }
    }
    return flag;
}

ll nextPrime(ll sum)
{
	for(ll  j = 0 ; ; j ++)
	{
		if(isPrime(sum + j ))
		{
			if( !isPrime(j))
				return j ;
			else
				continue;
		}
	}
}

ll matrix[105][105];
void solve()
{
	ll n ;
	cin >> n;
	memset(matrix, 0 , sizeof(matrix));

	for( ll i = 1 ; i <= n-1 ; i ++)
	{
		for( ll j = 2 ; j <= n ; j ++)
		{
			matrix[i][j] = 4;
		}
	}

	ll temp = 4*(n-1);
	ll get = nextPrime(temp);
	for(ll i = 1 ; i <= n-1; i ++)
	{
		matrix[i][1] = get;
	}
	for(ll j = 2 ; j <= n ; j ++)
	{
		matrix[n][j] = get;
	}

	temp = get*(n-1);
	get = nextPrime(temp);
	matrix[n][1] = get;

	for(ll i = 1 ; i <= n ; i ++)
	{
		for( ll j = 1 ; j <= n ; j++)
		{
			cout << matrix[i][j] << " ";
		}
		cout << "\n" ;
	}

	return ;
}

int main()
{
	ll t ;
	cin >> t ;
	for(ll i = 0 ; i < t ; i ++)
	{
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(),a.end()
#define push_back pb

typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;

const ll MOD = 1e9 + 7;

ll mod_mul(ll x , ll y) {return ((x%MOD)*(y%MOD)) % MOD; }
ll mod_add(ll x , ll y) {return ((x%MOD)+(y%MOD)) % MOD; }

// Lets write some important stuff

// Returns factorial of n
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = mod_mul(res , i );
    return res;
}

int nPr( int n , int r)
{
	ll res = 1;
	for(int i = n , j = 1 ; j <=r  ; i-- , j ++ )
	{
		res = mod_mul( res , i );
	}
	return res % MOD;
}

int nCr(int n, int r)
{
    return (fact(n) / (fact(r) * fact(n - r)));
}

int main()
{
	int n , x , pos;
	cin >> n >> x >> pos ;

	int low = 0 , high = 0 , left = 0 , right = n ;
	int middle = left ;

	while( left < right  )
	{

		middle = ( left + right ) / 2 ;
		if( middle <= pos)
		{
			left = middle + 1;
			if ( middle != pos)
			{
				low ++;
			}
		}
		else
		{
			right = middle ;
			high ++;
		}
	}

	if( low >= x || high >= n- x + 1)
	{
		cout << "0" << endl;
		return 0;
	}

	int ans;

	ans = (mod_mul( mod_mul(nPr(n-x, high),nPr(x-1,low)) , fact( n - low - high - 1)) );
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(),a.end()
#define push_back pb

typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;

const ll MOD = 1e9 + 7;

ll mod_mul(ll x , ll y) {return ((x%MOD)*(y%MOD)) % MOD; }
ll mod_add(ll x , ll y) {return ((x%MOD)+(y%MOD)) % MOD; }

// Lets write some important stuff

// Returns factorial of n the mod way
ll fact(ll n)
{
    ll res = 1;
    for (ll i = 2; i <= n; i++)
        res = mod_mul(res , i );
    return res;
}

// nPr the mod way
ll nPr( ll n , ll r)
{
	ll res = 1;
	for(ll i = n , j = 1 ; j <=r  ; i-- , j ++ )
	{
		res = mod_mul( res , i );
	}
	return res % MOD;
}

/*/ CODE BEGINS FROM HERE    */

bool isPrime(int num){
    bool flag=true;
    for(int i = 2; i <= num / 2; i++) {
       if(num % i == 0) {
          flag = false;
          break;
       }
    }
    return flag;
}

ll nextPrime(ll sum)
{
	for(ll  j = 0 ; ; j ++)
	{
		if(isPrime(sum + j ))
		{
			if( !isPrime(j))
				return j ;
			else
				continue;
		}
	}
}

ll matrix[105][105];
void solve()
{
	ll n ;
	cin >> n;
	memset(matrix, 0 , sizeof(matrix));

	for( ll i = 0 ; i < n-1 ; i ++)
	{
		for( ll j = 1 ; j < n ; j ++)
		{
			matrix[i][j] = 2;
		}
	}

	ll sum = 0 ;
	for(ll i = 0 ; i < n - 1 ; i ++ )
	{
		sum = 0;
		for(ll j = 1 ; j < n ; j ++) {sum += matrix[i][j];}
		matrix[i][0] = nextPrime(sum);
	}

	for(ll j = 1 ; j < n ; j++)
	{
		sum = 0 ;
		for(ll i = 0 ; i < n-1 ; i ++) {sum += matrix[i][j];}
		matrix[n][j] = nextPrime(sum);
	}

	for(ll i = 0 ; i < n ; i ++)
	{
		for( ll j = 0 ; j < n ; j++)
		{
			cout << matrix[i][j] << " ";
		}
		cout << "\n" ;
	}

	return ;
}

int main()
{
	ll t ;
	cin >> t ;
	for(ll i = 0 ; i < t ; i ++)
	{
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(),a.end()
#define push_back pb

typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;

const ll MOD = 1e9 + 7;

ll mod_mul(ll x , ll y) {return ((x%MOD)*(y%MOD)) % MOD; }
ll mod_add(ll x , ll y) {return ((x%MOD)+(y%MOD)) % MOD; }

// Lets write some important stuff

// Returns factorial of n the mod way
ll fact(ll n)
{
    ll res = 1;
    for (ll i = 2; i <= n; i++)
        res = mod_mul(res , i );
    return res;
}

// nPr the mod way
ll nPr( ll n , ll r)
{
	ll res = 1;
	for(ll i = n , j = 1 ; j <=r  ; i-- , j ++ )
	{
		res = mod_mul( res , i );
	}
	return res % MOD;
}

/*/ CODE BEGINS FROM HERE    */

bool isPrime(int num){
    bool flag=true;
    for(int i = 2; i <= num / 2; i++) {
       if(num % i == 0) {
          flag = false;
          break;
       }
    }
    return flag;
}

ll nextPrime(ll sum)
{
	for(ll i = sum ; ; i ++)
	{
		if(isPrime(i))
		{
			return i ;
		}
	}
}

ll matrix[105][105];
void solve()
{
	ll n ;
	cin >> n;
	memset(matrix, 0 , sizeof(matrix));

	for( ll i = 0 ; i < n-1 ; i ++)
	{
		for( ll j = 1 ; j < n ; j ++)
		{
			matrix[i][j] = 2;
		}
	}

	ll sum = 0 ;
	for(ll i = 0 ; i < n - 1 ; i ++ )
	{
		sum = 0;
		for(ll j = 1 ; j < n ; j ++) {sum += matrix[i][j];}
		matrix[i][0] = nextPrime(sum);
	}

	for(ll j = 1 ; j < n ; j++)
	{
		sum = 0 ;
		for(ll i = 0 ; i < n-1 ; i ++) {sum += matrix[i][j];}
		matrix[n][j] = nextPrime(sum);
	}

	for(ll i = 0 ; i < n ; i ++)
	{
		for( ll j = 0 ; j < n ; j++)
		{
			cout << matrix[i][j] << " ";
		}
		cout << "\n" ;
	}

	return ;
}

int main()
{
	ll t ;
	cin >> t ;
	for(ll i = 0 ; i < t ; i ++)
	{
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(),a.end()
#define push_back pb

typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;

const ll MOD = 1e9 + 7;

ll mod_mul(ll x , ll y) {return ((x%MOD)*(y%MOD)) % MOD; }
ll mod_add(ll x , ll y) {return ((x%MOD)+(y%MOD)) % MOD; }

// Lets write some important stuff

// Returns factorial of n the mod way
ll fact(ll n)
{
    ll res = 1;
    for (ll i = 2; i <= n; i++)
        res = mod_mul(res , i );
    return res;
}

// nPr the mod way
ll nPr( ll n , ll r)
{
	ll res = 1;
	for(ll i = n , j = 1 ; j <=r  ; i-- , j ++ )
	{
		res = mod_mul( res , i );
	}
	return res % MOD;
}

/*/ CODE BEGINS FROM HERE    */

bool isPrime(int num){
    bool flag=true;
    for(int i = 2; i <= num / 2; i++) {
       if(num % i == 0) {
          flag = false;
          break;
       }
    }
    return flag;
}

ll nextPrime(ll sum)
{
	for(ll  j = 0 ; ; j ++)
	{
		if(isPrime(sum + j ))
		{
			if( !isPrime(j))
				return j ;
			else
				continue;
		}
	}
}

ll matrix[105][105];
void solve()
{
	ll n ;
	cin >> n;
	memset(matrix, 0 , sizeof(matrix));

	for( ll i = 0 ; i < n-1 ; i ++)
	{
		for( ll j = 1 ; j < n ; j ++)
		{
			matrix[i][j] = 4;
		}
	}

	ll sum = 0 ;
	for(ll i = 0 ; i < n - 1 ; i ++ )
	{
		sum = 0;
		for(ll j = 1 ; j < n ; j ++) {sum += matrix[i][j];}
		matrix[i][0] = nextPrime(sum);
	}

	for(ll j = 1 ; j < n ; j++)
	{
		sum = 0 ;
		for(ll i = 0 ; i < n-1 ; i ++) {sum += matrix[i][j];}
		matrix[n][j] = nextPrime(sum);
	}

	for(ll i = 0 ; i < n ; i ++)
	{
		for( ll j = 0 ; j < n ; j++)
		{
			cout << matrix[i][j] << " ";
		}
		cout << "\n" ;
	}

	return ;
}

int main()
{
	ll t ;
	cin >> t ;
	for(ll i = 0 ; i < t ; i ++)
	{
		solve();
	}

	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define lop(i,n) for(i=0;i<n;i++)
#define lop1(i,n) for(i=1;i<=n;i++)
#define lopxy(i,x,y) for(i=x;i<y;i++)
#define ll long long
#define vl vector<long long>
#define pll pair<long long,long long>
#define sl set<long long>
#define uset unordered_set<long long>
#define umap unordered_map
#define prq priority_queue
#define pqll priority_queue<ll>
#define hell 1000000007
#define mod 998244353
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define N 100000
#define N2 200000
#define bpc(x) __builtin_popcount(x)
#define inf 1e16
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define all(v) (v).begin(),(v).end()
#define D1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<endl
#define D2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define D3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
using namespace std;
ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return (power(x, y / 2) * power(x, y / 2)) % mod;
    else
        return ((x * power(x, y / 2)) % mod * power(x, y / 2)) % mod;
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
    return power(n, p-2, p);
}

// Returns nCr % p using Fermat's little theorem.
ll fac[N+1];
void pre(ll p){
    fac[0] = 1;
    for (ll i=1 ; i<=N; i++)
        fac[i] = (fac[i-1]*i)%p;
}
ll nCrModPFermat(ll n, ll r, ll p)
{
   if (r==0)
      return 1;
    if(r>n)
      return 0;
    return (fac[n]* modInverse(fac[r], p) % p * modInverse(fac[n-r], p) % p) % p;
}
ll primes[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113};
bool isprime(ll n){
    if(n<=1)
        return false;
    for(ll i=2;i<=sqrt(n);i++){
        if(n%i==0)
            return false;
    }
    return true;
}
ll find(ll n){
    for(auto it:primes){
        if(it-n-1<0)
            continue;
        if(!isprime(it-n-1))
            return it-n-1;
    }
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt.txt","r",stdin);
    freopen("output.txt.txt","w",stdout);
    freopen("error.txt","w",stderr);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll t,n,i,j;
    cin>>t;
    while(t--){
        cin>>n;
        ll x=find(n);
        ll c=0;
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(j==i)
                    cout<<x;
                else cout<<1;
                cout<<" ";
            }
            cout<<endl;
        }
    }
    cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
    return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define lop(i,n) for(i=0;i<n;i++)
#define lop1(i,n) for(i=1;i<=n;i++)
#define lopxy(i,x,y) for(i=x;i<y;i++)
#define ll long long
#define vl vector<long long>
#define pll pair<long long,long long>
#define sl set<long long>
#define uset unordered_set<long long>
#define umap unordered_map
#define prq priority_queue
#define pqll priority_queue<ll>
#define hell 1000000007
#define mod 998244353
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define N 100000
#define N2 200000
#define bpc(x) __builtin_popcount(x)
#define inf 1e16
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define all(v) (v).begin(),(v).end()
#define D1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<endl
#define D2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define D3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
using namespace std;
ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return (power(x, y / 2) * power(x, y / 2)) % mod;
    else
        return ((x * power(x, y / 2)) % mod * power(x, y / 2)) % mod;
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
    return power(n, p-2, p);
}

// Returns nCr % p using Fermat's little theorem.
ll fac[N+1];
void pre(ll p){
    fac[0] = 1;
    for (ll i=1 ; i<=N; i++)
        fac[i] = (fac[i-1]*i)%p;
}
ll nCrModPFermat(ll n, ll r, ll p)
{
   if (r==0)
      return 1;
    if(r>n)
      return 0;
    return (fac[n]* modInverse(fac[r], p) % p * modInverse(fac[n-r], p) % p) % p;
}
vl primes;
void sieve(){
    ll pr[100001];
    mem0(pr);
    for(i=2;i<=n;i++){
        if(pr[i]==1)
            continue;
        for(ll j=i*i;j<=n;j++){
            pr[j]=1;
        }
        if(pr[i]==0)
            primes.pb(i);
    }
}
bool isprime(ll n){
    if(n<=1)
        return false;
    for(ll i=2;i<=sqrt(n);i++){
        if(n%i==0)
            return false;
    }
    return true;
}
ll find(ll n){
    for(auto it:primes){
        if(it-n+1<0)
            continue;
        if(!isprime(it-n+1))
            return it-n+1;
    }
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt.txt","r",stdin);
    freopen("output.txt.txt","w",stdout);
    freopen("error.txt","w",stderr);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll t,n,i,j;
    cin>>t;
    while(t--){
        cin>>n;
        ll x=find(n);
        ll c=0;
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(j==i)
                    cout<<x;
                else cout<<1;
                cout<<" ";
            }
            cout<<endl;
        }
    }
    cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
    return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define lop(i,n) for(i=0;i<n;i++)
#define lop1(i,n) for(i=1;i<=n;i++)
#define lopxy(i,x,y) for(i=x;i<y;i++)
#define ll long long
#define vl vector<long long>
#define pll pair<long long,long long>
#define sl set<long long>
#define uset unordered_set<long long>
#define umap unordered_map
#define prq priority_queue
#define pqll priority_queue<ll>
#define hell 1000000007
#define mod 998244353
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define N 100000
#define N2 200000
#define bpc(x) __builtin_popcount(x)
#define inf 1e16
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define all(v) (v).begin(),(v).end()
#define D1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<endl
#define D2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define D3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
using namespace std;
ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return (power(x, y / 2) * power(x, y / 2)) % mod;
    else
        return ((x * power(x, y / 2)) % mod * power(x, y / 2)) % mod;
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
    return power(n, p-2, p);
}

// Returns nCr % p using Fermat's little theorem.
ll fac[N+1];
void pre(ll p){
    fac[0] = 1;
    for (ll i=1 ; i<=N; i++)
        fac[i] = (fac[i-1]*i)%p;
}
ll nCrModPFermat(ll n, ll r, ll p)
{
   if (r==0)
      return 1;
    if(r>n)
      return 0;
    return (fac[n]* modInverse(fac[r], p) % p * modInverse(fac[n-r], p) % p) % p;
}
vl primes;
void sieve(){
    ll pr[100001];
    mem0(pr);
    for(ll i=2;i<=N;i++){
        if(pr[i]==1)
            continue;
        for(ll j=i*i;j<=N;j++){
            pr[j]=1;
        }
        if(pr[i]==0)
            primes.pb(i);
    }
}
bool isprime(ll n){
    if(n<=1)
        return false;
    for(ll i=2;i<=sqrt(n);i++){
        if(n%i==0)
            return false;
    }
    return true;
}
ll find(ll n){
    for(auto it:primes){
        if(it-n+1<0)
            continue;
        if(!isprime(it-n+1))
            return it-n+1;
    }
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt.txt","r",stdin);
    freopen("output.txt.txt","w",stdout);
    freopen("error.txt","w",stderr);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll t,n,i,j;
    cin>>t;
    sieve();
    while(t--){
        cin>>n;
        ll x=find(n);
        ll c=0;
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(j==i)
                    cout<<x;
                else cout<<1;
                cout<<" ";
            }
            cout<<endl;
        }
    }
    cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
    return 0;
}