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

int n, k;
int mod = 1e9 + 7;

struct mtran
{	
	ll x[15][15];
};

mtran z;
mtran p;

mtran nhan(mtran a, mtran b) 
{
	mtran c;
	for(int i = 0; i < n; i++) 
	{
		for(int j = 0; j < n; j++) 
		{
			ll s = 0;
			for(int t = 0; t < n; t++) 
			{
				s += (a.x[i][t] % mod * b.x[t][j] % mod) % mod;
				s %= mod;
			}
			c.x[i][j] = s;
		}
	}
	return c;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int t; cin >> t;
    while(t--) 
    {
    	cin >> n >> k;
    	for(int i = 0; i < n; i++) 
    	{
    		for(int j = 0; j < n; j++) 
    		{
    			cin >> z.x[i][j];
			}
		}
		p = z;
		for(int i = 1; i <= k - 1; i++) 
		{
			p = nhan(p,z);
		}
		for(int i = 0; i < n; i++) 
		{
			for(int j = 0; j < n; j++) 
			{
				cout << p.x[i][j] << ' ';
			}
			cout << "\n";
		}
	}
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}