p00593 JPEG Compression_28778
The fundamental idea in the JPEG compression algorithm is to sort coeffi- cient of given image by zigzag path and encode it. In this problem, we don’t discuss about details of the algorithm, but you are asked to make simple pro- gram. You are given single integer N , and you must output zigzag path on a matrix where size is N by N . The zigzag scanning is start at the upper-left corner (0, 0) and end up at the bottom-right corner. See the following Figure and sample output to make sure rule of the zigzag scanning. For example, if you are given N = 8, corresponding output should be a matrix shown in right-side of the Figure. This matrix consists of visited time for each element.

<image>



Input

Several test cases are given. Each test case consists of one integer N (0 < N < 10) in a line. The input will end at a line contains single zero.

Output

For each input, you must output a matrix where each element is the visited time. All numbers in the matrix must be right justified in a field of width 3. Each matrix should be prefixed by a header “Case x:” where x equals test case number.

Example

Input

3
4
0


Output

Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7
  3  5  8 13
  4  9 12 14
 10 11 15 16
# coding: utf-8
# Here your code !
case = 1
while True:
    N = int(raw_input())
    if N == 0: break
    mat = [[0 for i in xrange(N)] for j in xrange(N)]
    n = 1
    for i in range(N): # upper half
        for j in reversed(xrange(i+1)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
                 
    for i in range(N,2*N-1): # lower half
        for j in reversed(xrange(i-N+1,N)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
    print 'Case %d:'%(case); case += 1
    for s in mat:
        print ''.join(map(str,[str(st).rjust(3) for st in s]))
3
4
0 3
7
0 3
3
0 3
0
0 3
1
0 3
2
0 3
-1
0 3
5
0 3
8
0 3
6
0 3
9
0 3
0
-1 3
0
1 3
0
-2 3
0
2 3
0
-4 3
0
-5 3
0
3 3
0
-3 3
0
4 3
0
8 3
0
5 3
0
16 3
0
30 3
0
6 3
0
-8 3
0
-6 3
0
22 3
0
18 3
0
-11 3
0
25 3
0
-16 3
0
-30 3
0
-24 3
0
7 3
0
10 3
0
14 3
0
26 3
0
28 3
0
24 3
0
-20 3
0
-35 3
0
12 3
0
-7 3
0
50 3
0
11 3
0
20 3
0
19 3
0
9 3
0
-9 3
0
36 3
0
-10 3
0
32 3
0
23 3
0
17 3
0
43 3
0
13 3
0
-17 3
0
55 3
0
33 3
0
-21 3
0
45 3
0
44 3
0
40 3
0
-12 3
0
15 3
0
-36 3
0
29 3
0
46 3
0
31 3
0
-15 3
0
63 3
0
35 3
0
90 3
0
-13 3
0
56 3
0
54 3
0
-60 3
0
76 3
0
34 3
0
49 3
0
-14 3
0
-18 3
0
51 3
0
-19 3
0
21 3
0
85 3
0
38 3
0
62 3
0
159 3
0
88 3
0
77 3
0
-68 3
0
-22 3
0
248 3
0
-48 3
0
160 3
0
37 3
0
48 3
0
74 3
0
-26
Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7
  3  5  8 13
  4  9 12 14
 10 11 15 16 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7 15 16 28
  3  5  8 14 17 27 29
  4  9 13 18 26 30 39
 10 12 19 25 31 38 40
 11 20 24 32 37 41 46
 21 23 33 36 42 45 47
 22 34 35 43 44 48 49
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2
  3  4
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7 15
  3  5  8 14 16
  4  9 13 17 22
 10 12 18 21 23
 11 19 20 24 25
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7 15 16 28 29
  3  5  8 14 17 27 30 43
  4  9 13 18 26 31 42 44
 10 12 19 25 32 41 45 54
 11 20 24 33 40 46 53 55
 21 23 34 39 47 52 56 61
 22 35 38 48 51 57 60 62
 36 37 49 50 58 59 63 64
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7 15 16
  3  5  8 14 17 26
  4  9 13 18 25 27
 10 12 19 24 28 33
 11 20 23 29 32 34
 21 22 30 31 35 36
 Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7 15 16 28 29 45
  3  5  8 14 17 27 30 44 46
  4  9 13 18 26 31 43 47 60
 10 12 19 25 32 42 48 59 61
 11 20 24 33 41 49 58 62 71
 21 23 34 40 50 57 63 70 72
 22 35 39 51 56 64 69 73 78
 36 38 52 55 65 68 74 77 79
 37 53 54 66 67 75 76 80 81
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
 Case 1:
  1  2  6
  3  5  7
  4  8  9
