/*
By WIL
*/
#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <math.h>
#include <stack>
#include <limits.h>
using namespace std;
#define zero(x) memset(x,0,sizeof(x))
#define mone(x) memset(x,-1,sizeof(x))
#define ll long long
#define base 31
#define mod 10
#define X first
#define Y second
#define fo(i,n) for(int i=0;i<n;i++)
#define foo(i,n) for(int i=1;i<=n;i++)
#define fa(i,I,n) for(int i=I;i<n;i++)
#define sz(x) x.size()
#define pb push_back
#define endl '\n'
int n,m;
struct matInt{
int t;
int arr[31][31];
matInt(int T){
t=T;
fo(i,t)fo(j,t)arr[i][j]=0;
}
matInt(){
t=30;
fo(i,t)fo(j,t)arr[i][j]=0;
}
}I,O;
matInt operator+(matInt o,matInt p){
int t=o.t;
matInt ret=matInt(t);
fo(i,t)fo(j,t)ret.arr[i][j]=(o.arr[i][j]+p.arr[i][j])%mod;
return ret;
}
matInt operator*(matInt o,matInt p){
int t=o.t;
matInt ret=matInt(t);
fo(i,t)fo(j,t)fo(k,t){
ret.arr[i][j]+=(o.arr[i][k]*p.arr[k][j])%mod;
ret.arr[i][j]%=mod;
}
return ret;
}
struct mat{
int t;
matInt arr[2][2];
mat(int T){
t=T;
fo(i,t)fo(j,t)arr[i][j]=O;
}
};
mat operator*(mat o,mat p){
int t=o.t;
mat ret=mat(t);
fo(i,t)fo(j,t)fo(k,t){
ret.arr[i][j]=ret.arr[i][j]+(o.arr[i][k]*p.arr[k][j]);
}
return ret;
}
mat pot(mat a,int b){
//if(b==0)return I;
if(b==1)return a;
mat c=pot(a,b/2);
c=c*c;
if(b&1)return c*a;
return c;
}
int t;
int main(){
scanf("%d",&t);
O=matInt(30);
I=matInt(30);
fo(i,30)I.arr[i][i]=1;
int cas=1;
while(t--){
//cout<<"S\n";
scanf("%d%d",&n,&m);
matInt A=matInt(n);
fo(i,n)fo(j,n){
int v;
scanf("%d",&v);
A.arr[i][j]=v%mod;
}
O.t=I.t=n;
mat me=mat(2);
me.arr[0][0]=I;
me.arr[0][1]=I;
me.arr[1][0]=O;
me.arr[1][1]=A;
me=pot(me,m);
//fo(i,n){fo(j,n)cout<< me.arr[0][1].arr[i][j] <<" ";cout<<endl;}
matInt sol=(A*me.arr[0][1]);
printf("Case %d:\n",cas++);
fo(i,n){fo(j,n)printf("%d",sol.arr[i][j]);printf("\n");}
}
return 0;
}
LyoKQnkgV0lMCiovCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxtZW1vcnkuaD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8bWF0aC5oPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxsaW1pdHMuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgemVybyh4KSBtZW1zZXQoeCwwLHNpemVvZih4KSkKI2RlZmluZSBtb25lKHgpIG1lbXNldCh4LC0xLHNpemVvZih4KSkKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBiYXNlIDMxCiNkZWZpbmUgbW9kIDEwCiNkZWZpbmUgWCBmaXJzdAojZGVmaW5lIFkgc2Vjb25kCiNkZWZpbmUgZm8oaSxuKSBmb3IoaW50IGk9MDtpPG47aSsrKQojZGVmaW5lIGZvbyhpLG4pIGZvcihpbnQgaT0xO2k8PW47aSsrKQojZGVmaW5lIGZhKGksSSxuKSBmb3IoaW50IGk9STtpPG47aSsrKQojZGVmaW5lIHN6KHgpIHguc2l6ZSgpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgZW5kbCAnXG4nCmludCBuLG07CnN0cnVjdCBtYXRJbnR7CiAgICBpbnQgdDsKICAgIGludCBhcnJbMzFdWzMxXTsKICAgIG1hdEludChpbnQgVCl7CiAgICAgICAgdD1UOwogICAgICAgIGZvKGksdClmbyhqLHQpYXJyW2ldW2pdPTA7CiAgICB9CiAgICBtYXRJbnQoKXsKICAgICAgICB0PTMwOwogICAgICAgIGZvKGksdClmbyhqLHQpYXJyW2ldW2pdPTA7CiAgICB9Cn1JLE87Cm1hdEludCBvcGVyYXRvcisobWF0SW50IG8sbWF0SW50IHApewogICAgaW50IHQ9by50OwogICAgbWF0SW50IHJldD1tYXRJbnQodCk7CiAgICBmbyhpLHQpZm8oaix0KXJldC5hcnJbaV1bal09KG8uYXJyW2ldW2pdK3AuYXJyW2ldW2pdKSVtb2Q7CiAgICByZXR1cm4gcmV0Owp9Cm1hdEludCBvcGVyYXRvcioobWF0SW50IG8sbWF0SW50IHApewogICAgaW50IHQ9by50OwogICAgbWF0SW50IHJldD1tYXRJbnQodCk7CiAgICBmbyhpLHQpZm8oaix0KWZvKGssdCl7CiAgICAgICAgcmV0LmFycltpXVtqXSs9KG8uYXJyW2ldW2tdKnAuYXJyW2tdW2pdKSVtb2Q7CiAgICAgICAgcmV0LmFycltpXVtqXSU9bW9kOwogICAgfQogICAgcmV0dXJuIHJldDsKfQpzdHJ1Y3QgbWF0ewogICAgaW50IHQ7CiAgICBtYXRJbnQgYXJyWzJdWzJdOwogICAgbWF0KGludCBUKXsKICAgICAgICB0PVQ7CiAgICAgICAgZm8oaSx0KWZvKGosdClhcnJbaV1bal09TzsKICAgIH0KfTsKbWF0IG9wZXJhdG9yKihtYXQgbyxtYXQgcCl7CiAgICBpbnQgdD1vLnQ7CiAgICBtYXQgcmV0PW1hdCh0KTsKICAgIGZvKGksdClmbyhqLHQpZm8oayx0KXsKICAgICAgICByZXQuYXJyW2ldW2pdPXJldC5hcnJbaV1bal0rKG8uYXJyW2ldW2tdKnAuYXJyW2tdW2pdKTsKICAgIH0KICAgIHJldHVybiByZXQ7Cn0KbWF0IHBvdChtYXQgYSxpbnQgYil7CiAgICAvL2lmKGI9PTApcmV0dXJuIEk7CiAgICBpZihiPT0xKXJldHVybiBhOwogICAgbWF0IGM9cG90KGEsYi8yKTsKICAgIGM9YypjOwogICAgaWYoYiYxKXJldHVybiBjKmE7CiAgICByZXR1cm4gYzsKfQppbnQgdDsKaW50IG1haW4oKXsKICAgIHNjYW5mKCIlZCIsJnQpOwogICAgTz1tYXRJbnQoMzApOwogICAgST1tYXRJbnQoMzApOwogICAgZm8oaSwzMClJLmFycltpXVtpXT0xOwogICAgaW50IGNhcz0xOwogICAgd2hpbGUodC0tKXsKICAgICAgICAvL2NvdXQ8PCJTXG4iOwogICAgICAgIHNjYW5mKCIlZCVkIiwmbiwmbSk7CiAgICAgICAgbWF0SW50IEE9bWF0SW50KG4pOwogICAgICAgIGZvKGksbilmbyhqLG4pewogICAgICAgICAgICBpbnQgdjsKICAgICAgICAgICAgc2NhbmYoIiVkIiwmdik7CiAgICAgICAgICAgIEEuYXJyW2ldW2pdPXYlbW9kOwogICAgICAgIH0KICAgICAgICBPLnQ9SS50PW47CiAgICAgICAgbWF0IG1lPW1hdCgyKTsKICAgICAgICBtZS5hcnJbMF1bMF09STsKICAgICAgICBtZS5hcnJbMF1bMV09STsKICAgICAgICBtZS5hcnJbMV1bMF09TzsKICAgICAgICBtZS5hcnJbMV1bMV09QTsKICAgICAgICBtZT1wb3QobWUsbSk7CiAgICAgICAgLy9mbyhpLG4pe2ZvKGosbiljb3V0PDwgbWUuYXJyWzBdWzFdLmFycltpXVtqXSA8PCIgIjtjb3V0PDxlbmRsO30KICAgICAgICBtYXRJbnQgc29sPShBKm1lLmFyclswXVsxXSk7CiAgICAgICAgcHJpbnRmKCJDYXNlICVkOlxuIixjYXMrKyk7CiAgICAgICAgZm8oaSxuKXtmbyhqLG4pcHJpbnRmKCIlZCIsc29sLmFycltpXVtqXSk7cHJpbnRmKCJcbiIpO30KICAgIH0KICAgIHJldHVybiAwOwp9Cg==