package LuyenDe;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class QuaCau {
static int N, a[][], ans;
static int van;
static int Sx[], Sy[], visit[][], d[][];
static int dx[] = { -1, -1, -1 };
static int dy[] = { -1, 0, 1 };
public static void main(String[] args) {
try {
System.setIn(new FileInputStream("quacau"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
N = sc.nextInt();
a = new int[N + 2][5];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = sc.nextInt();
}
}
van = 1;
Sx = new int[1000];
Sy = new int[1000];
d = new int[N + 2][5];
visit = new int[N + 2][5];
ans = 0;
// for (int l = 1; l <= 3; l++) {
DFS(N, 2);
for (int k = 0; k < 5; k++) {
ans = Math.max(ans, d[0][k]);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(d[i][j] + " ");
}
System.out.println();
}
System.out.println();
// }
System.out.println("#" + tc + " " + ans);
}
}
public static int mrank(int x, int y) {
if (a[x][y] == 1)
return 1;
if (a[x][y] == 0)
return 0;
if (a[x][y] == 2 && van > 0)
return 0;
if (a[x][y] == 2 && van < 1)
return -1;
return -1;
}
public static void DFS(int x, int y) {
int top = 0;
visit[x][y] = 1;
d[x][y] = mrank(x, y);
Sx[top] = x;
Sy[top] = y;
top++;
while (top >0) {
top--;
int aa = Sx[top];
int bb = Sy[top];
for (int i = 0; i < 3; i++) {
int xx = aa + dx[i];
int yy = bb + dy[i];
if (xx >= 0 && xx < N && yy >= 0 && yy < 5 && visit[xx][yy] == 0 && mrank(xx, yy) != -1) {
d[xx][yy] = d[aa][bb] + mrank(xx, yy);
visit[xx][yy] = 1;
Sx[top] = xx;
Sy[top] = yy;
top++;
if (a[xx][yy] == 2)
van = 0;
}
}
}
}
}