Processing math: 100%

Summing up Powers (II)

1 seconds
64 MB
Medium Hard
LOJ-1142 Udebug Debug
English

Shanto is learning how to power up numbers and he found an efficient way to find kth power of a matrix. He was quite happy with his discovery. Suddenly his sister Natasha came to him and asked him to find the summation of the powers. To be specific, his sister gave the following problem.

Let A be an n x n matrix. We define

AK=A×A×A××A(k times)

Where × stands for matrix multiplication. In this problem you are required to find the following

A+A2+A3++Ak

Shanto smiled and thought that it would be an easy one. But after a while he found that it's tough for him. Can you help him?

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with two integers n (1 ≤ n ≤ 30) and k (1 ≤ k ≤ 109). Each of the next n lines will contain n non-negative integers (not greater than 10).

Output

For each case, print the case number and the result matrix. For each cell, just print the last digit (least significant digit, if the result is 128, you just need to report 8). See the samples for more details.

Sample

Sample Input Sample Output

2 3 2 1 4 6 6 5 2 1 2 3 3 10 1 4 6 6 5 2 1 2 3

Case 1: 208 484 722 Case 2: 868 620 546