#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ powermod(long long **, long long **, long long, long long,
                            long long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, long long *, ...);
int main() {
  long long int i, j, k;
  long long int arr[100][100];
  long long int ans[100][100];
  long long int t, m, mod;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld %lld %lld", &m, &k, &mod);
    printf("\n\n");
    for (i = 1; i <= m; i++) {
      for (j = 1; j <= m; j++) {
        scanf("%lld", &arr[i][j]);
      }
    }
    powermod(arr, ans, m, k, mod);
    printf("\n\n");
    for (i = 1; i <= m; i++) {
      for (j = 1; j <= m; j++) {
        printf("%lld ", ans[i][j]);
      }
      printf("\n");
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x27140,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec1(%rip),%rdi
lea    -0x27128(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x27128(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x27128(%rbp)
cmp    $0x0,%rax
je     12f8 <main+0x1c8>
lea    0xe83(%rip),%rdi
lea    -0x27130(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x27138(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x27130(%rbp),%rax
jg     1228 <main+0xf8>
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x27130(%rbp),%rax
jg     1212 <main+0xe2>
lea    -0x138a0(%rbp),%rsi
imul   $0x320,-0x10(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11c5 <main+0x95>
jmp    1217 <main+0xe7>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11ac <main+0x7c>
lea    -0x138a0(%rbp),%rdi
lea    -0x27120(%rbp),%rsi
mov    -0x27130(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x27138(%rbp),%r8
call   1050 <powermod@plt>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x27130(%rbp),%rax
jg     12e5 <main+0x1b5>
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x27130(%rbp),%rax
jg     12c6 <main+0x196>
lea    -0x27120(%rbp),%rax
imul   $0x320,-0x10(%rbp),%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    127c <main+0x14c>
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1263 <main+0x133>
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1157 <main+0x27>
xor    %eax,%eax
add    $0x27140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x27128,%rsp
lea    0xea2(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
subq   $0x1,0x8(%rsp)
jae    1190 <main+0x40>
xor    %eax,%eax
add    $0x27128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe7e(%rip),%r13
lea    0xe71(%rip),%r15
lea    0xe65(%rip),%r12
jmp    11c2 <main+0x72>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
subq   $0x1,0x8(%rsp)
jb     117c <main+0x2c>
lea    0xe37(%rip),%rdi
mov    %rsp,%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
xor    %eax,%eax
call   1060 <scanf@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    (%rsp),%rdx
test   %rdx,%rdx
jle    1240 <main+0xf0>
mov    $0x1,%r14d
lea    0x13bc8(%rsp),%rbp
jmp    1213 <main+0xc3>
xchg   %ax,%ax
lea    0x1(%r14),%rax
add    $0x320,%rbp
cmp    %rdx,%r14
mov    %rax,%r14
jge    1240 <main+0xf0>
test   %rdx,%rdx
jle    1200 <main+0xb0>
mov    %rbp,%r13
xor    %ebx,%ebx
nopl   (%rax)
mov    %r12,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    (%rsp),%rdx
add    $0x1,%rbx
add    $0x8,%r13
cmp    %rdx,%rbx
jl     1220 <main+0xd0>
jmp    1200 <main+0xb0>
mov    0x18(%rsp),%rcx
mov    0x10(%rsp),%r8
lea    0x138a0(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   1070 <powermod@plt>
lea    0xdb2(%rip),%r13
mov    %r13,%rdi
call   1040 <puts@plt>
mov    (%rsp),%rax
test   %rax,%rax
jle    11b0 <main+0x60>
mov    $0x1,%r14d
lea    0x348(%rsp),%rbp
jmp    12b5 <main+0x165>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x1(%r14),%rcx
mov    (%rsp),%rax
add    $0x320,%rbp
cmp    %rax,%r14
mov    %rcx,%r14
jge    11b0 <main+0x60>
test   %rax,%rax
jle    1290 <main+0x140>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x0(%rbp,%rbx,8),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    (%rsp),%rbx
jl     12c0 <main+0x170>
jmp    1290 <main+0x140>

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [15 x i8] c"%lld %lld %lld\00"
@.str.2 = [3 x i8] c"\0A\0A\00"
@.str.3 = [6 x i8] c"%lld \00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca [100 x [100 x i64]]
%6 = alloca [100 x [100 x i64]]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 0, i32* %1
%10 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %7)
br label %11
11: ; preds = %74, %0
%12 = load i64, i64* %7
%13 = add nsw i64 %12, -1
store i64 %13, i64* %7
%14 = icmp ne i64 %12, 0
br i1 %14, label %15, label %76
15: ; preds = %11
%16 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64* %8, i64* %4, i64* %9)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i64 1, i64* %2
br label %18
18: ; preds = %37, %15
%19 = load i64, i64* %2
%20 = load i64, i64* %8
%21 = icmp sle i64 %19, %20
br i1 %21, label %22, label %40
22: ; preds = %18
store i64 1, i64* %3
br label %23
23: ; preds = %33, %22
%24 = load i64, i64* %3
%25 = load i64, i64* %8
%26 = icmp sle i64 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i64, i64* %2
%29 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %5, i64 0, i64 %28
%30 = load i64, i64* %3
%31 = getelementptr inbounds [100 x i64], [100 x i64]* %29, i64 0, i64 %30
%32 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %31)
br label %33
33: ; preds = %27
%34 = load i64, i64* %3
%35 = add nsw i64 %34, 1
store i64 %35, i64* %3
br label %23
36: ; preds = %23
br label %37
37: ; preds = %36
%38 = load i64, i64* %2
%39 = add nsw i64 %38, 1
store i64 %39, i64* %2
br label %18
40: ; preds = %18
%41 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %5, i64 0, i64 0
%42 = bitcast [100 x i64]* %41 to i64**
%43 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %6, i64 0, i64 0
%44 = bitcast [100 x i64]* %43 to i64**
%45 = load i64, i64* %8
%46 = load i64, i64* %4
%47 = load i64, i64* %9
%48 = call i32 @powermod(i64** %42, i64** %44, i64 %45, i64 %46, i64 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i64 1, i64* %2
br label %50
50: ; preds = %71, %40
%51 = load i64, i64* %2
%52 = load i64, i64* %8
%53 = icmp sle i64 %51, %52
br i1 %53, label %54, label %74
54: ; preds = %50
store i64 1, i64* %3
br label %55
55: ; preds = %66, %54
%56 = load i64, i64* %3
%57 = load i64, i64* %8
%58 = icmp sle i64 %56, %57
br i1 %58, label %59, label %69
59: ; preds = %55
%60 = load i64, i64* %2
%61 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %6, i64 0, i64 %60
%62 = load i64, i64* %3
%63 = getelementptr inbounds [100 x i64], [100 x i64]* %61, i64 0, i64 %62
%64 = load i64, i64* %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %64)
br label %66
66: ; preds = %59
%67 = load i64, i64* %3
%68 = add nsw i64 %67, 1
store i64 %68, i64* %3
br label %55
69: ; preds = %55
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %71
71: ; preds = %69
%72 = load i64, i64* %2
%73 = add nsw i64 %72, 1
store i64 %73, i64* %2
br label %50
74: ; preds = %50
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %11
76: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [15 x i8] c"%lld %lld %lld\00"
@.str.3 = [6 x i8] c"%lld \00"
@str.5 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca [100 x [100 x i64]]
%3 = alloca [100 x [100 x i64]]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = bitcast i64* %1 to i8*
%8 = bitcast [100 x [100 x i64]]* %2 to i8*
%9 = bitcast [100 x [100 x i64]]* %3 to i8*
%10 = bitcast i64* %4 to i8*
%11 = bitcast i64* %5 to i8*
%12 = bitcast i64* %6 to i8*
%13 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %4)
%14 = load i64, i64* %4
%15 = add nsw i64 %14, -1
store i64 %15, i64* %4
%16 = icmp eq i64 %14, 0
br i1 %16, label %70, label %17
17: ; preds = %0
%18 = bitcast [100 x [100 x i64]]* %2 to i64**
%19 = bitcast [100 x [100 x i64]]* %3 to i64**
br label %20
20: ; preds = %17, %65
%21 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64* %5, i64* %1, i64* %6)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.5, i64 0, i64 0))
%23 = load i64, i64* %5
%24 = icmp slt i64 %23, 1
br i1 %24, label %40, label %25
25: ; preds = %20, %36
%26 = phi i64 [ %37, %36 ], [ %23, %20 ]
%27 = phi i64 [ %38, %36 ], [ 1, %20 ]
%28 = icmp slt i64 %26, 1
br i1 %28, label %36, label %29
29: ; preds = %25, %29
%30 = phi i64 [ %33, %29 ], [ 1, %25 ]
%31 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %2, i64 0, i64 %27, i64 %30
%32 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %31)
%33 = add nuw nsw i64 %30, 1
%34 = load i64, i64* %5
%35 = icmp slt i64 %30, %34
br i1 %35, label %29, label %36
36: ; preds = %29, %25
%37 = phi i64 [ %26, %25 ], [ %34, %29 ]
%38 = add nuw nsw i64 %27, 1
%39 = icmp slt i64 %27, %37
br i1 %39, label %25, label %40
40: ; preds = %36, %20
%41 = phi i64 [ %23, %20 ], [ %37, %36 ]
%42 = load i64, i64* %1
%43 = load i64, i64* %6
%44 = call i32 @powermod(i64** %18, i64** %19, i64 %41, i64 %42, i64 %43)
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.5, i64 0, i64 0))
%46 = load i64, i64* %5
%47 = icmp slt i64 %46, 1
br i1 %47, label %65, label %48
48: ; preds = %40, %60
%49 = phi i64 [ %63, %60 ], [ %46, %40 ]
%50 = phi i64 [ %62, %60 ], [ 1, %40 ]
%51 = icmp slt i64 %49, 1
br i1 %51, label %60, label %52
52: ; preds = %48, %52
%53 = phi i64 [ %57, %52 ], [ 1, %48 ]
%54 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %3, i64 0, i64 %50, i64 %53
%55 = load i64, i64* %54
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %55)
%57 = add nuw nsw i64 %53, 1
%58 = load i64, i64* %5
%59 = icmp slt i64 %53, %58
br i1 %59, label %52, label %60
60: ; preds = %52, %48
%61 = call i32 @putchar(i32 10)
%62 = add nuw nsw i64 %50, 1
%63 = load i64, i64* %5
%64 = icmp slt i64 %50, %63
br i1 %64, label %48, label %65
65: ; preds = %60, %40
%66 = call i32 @putchar(i32 10)
%67 = load i64, i64* %4
%68 = add nsw i64 %67, -1
store i64 %68, i64* %4
%69 = icmp eq i64 %67, 0
br i1 %69, label %70, label %20
70: ; preds = %65, %0
ret i32 0
}