Consider the following $4 \times 4$ pattern:
1  2  4  7
3  5  8 11
6  9 12 14
10 13 15 16

You are given an integer $N$. Print the $N \times N$ pattern of the same kind (containing integers $1$ through $N^2$).

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single integer $N$.

-----Output-----
For each test case, print $N$ lines; each of them should contain $N$ space-separated integers.

-----Constraints-----
- $1 \le T \le 10$
- $1 \le N \le 100$

-----Subtasks-----
Subtask #1 (100 points): Original constraints

-----Example Input-----
1
4

-----Example Output-----
1 2 4 7
3 5 8 11
6 9 12 14
10 13 15 16

-----Explanation-----

Difficulty: interview

Starter code:

Solutions:
for i in range(int(input())):
 t=int(input())
 n=0
 for i in range(1,t+1):
  n=n+i
  x=[n]
  y=n
  for j in range(i,t+i-1):
   if j<t:
    z=y+j
   else:
    z=y+(2*t-j-1)
   x.append(z)
   y=z
  print(*x)

for i in range(int(input())):
 n=int(input())
 t=0
 for i in range(1,n+1):
  t=t+i
  m=[t]
  k=t
  for j in range(i,n+i-1):
   if j<n:
    a=k+j
   else:
    a=k+(2*n-j-1)
   m.append(a)
   k=a
  print(*m)

try:
 T=int(input())
 P=""
 for i in range (T):
  N=int(input())
  A=[[0 for i in range (N)]for j in range(N)]
  a=1
  K=0
  for x in range(2*N):
   if K<N:
    i=0
    j=K
    for y in range (K+1):
     if i+j==K and i>=0 and j>=0:
      A[i][j]=a
      i=i+1
      j=j-1
      a=a+1
    K=K+1
   else:
    J=N-1
    I=K-(J)
    for y in range(2*N-K-1):
     A[I][J]=a
     I=I+1
     J=J-1
     a=a+1
    K=K+1
  for a in range(0,N):
   for b in range(0,N):
    if b!=N-1:
     P=P+str(A[a][b])+" "
    else:
     P=P+str(A[a][N-1])+"\n"
 print(P)
except:
 pass

def fill_val(i, j, n, mat):
 nonlocal val
 while j >= 0 and i < n:
  mat[i][j] = val
  val += 1
  i += 1
  j -= 1

for _ in range(int(input())):
 n = int(input())
 arr = [[0 for c in range(n)] for r in range(n)]
 val = 1

 for i in range(n):
  for j in range(n):
   if arr[i][j] == 0:
    fill_val(i, j, n, arr)

 for i in range(n):
  for j in range(n):
   print(arr[i][j], end=" ")

  print()

def fill_val(i, j, n, mat):
 nonlocal val
 while j >= 0 and i < n:
  mat[i][j] = val
  val += 1
  i += 1
  j -= 1

for _ in range(int(input())):
 n = int(input())
 arr = [[0 for c in range(n)] for r in range(n)]
 val = 1

 for i in range(n):
  for j in range(n):
   if arr[i][j] == 0:
    fill_val(i, j, n, arr)

 for i in range(n):
  for j in range(n):
   print(arr[i][j], end=" ")

  print()

# cook your dish here
try:
 for u in range(int(input())):
  n = int(input())
  li = []
  lid = []
  a = 1
  t = 0
  l = 0
  for i in range(n*n):
   t+=1
   lid.append(i+1)
   if a == t:
    if l!=-1:
     a+=1
     t = 0
    else:
     a-=1
     t = 0
    li.append([int(k) for k in lid])
    lid.clear()
   if a==n:
    l = -1
  li2 = li.copy()
  for i in li:
   if len(li2)!=0:
    a = 0
    li3 = li2.copy()
    for j in li2:
     if a == n:
      break
     a+=1
     if len(j)!=0:
      print(j[0],end=" ")
      j.remove(j[0])
    li2.clear()
    li2 = li3.copy()
    if len(j)!=0:
     print()
    else:
     break
    li2.remove(li2[0])
  print()
except:
 pass

# cook your dish here
for i in range(int(input())):
 n=int(input())
 st=0
 for i in range(1,n+1):
  st=st+i
  l=[st]
  k=st
  for j in range(i,n+i-1):
   if j < n:
    x=k+j
   else:
    x=k+(2*n-j-1) 
   l.append(x)
   k=x 
  print(*l)

# cook your dish here
tests = int(input())
for test in range(tests):
 n = int(input())

 a = [[0 for i in range(n)] for i in range(n)]
 count = 1
 for k in range(n):
  i = 0
  for j in range(k, -1, -1):
   a[i][j] = count
   count += 1
   i += 1
 for k in range(1, n):
  i = k
  j = n-1
  for i in range(k, n):
   a[i][j] = count
   count += 1
   j -= 1

 my_str = ''
 for i in range(n):
  for j in range(n):
   my_str += str(a[i][j])
   my_str += ' '
  my_str += '\n'
 
 print(my_str)

for _ in range(int(input())):
 n=int(input())
 k=0
 for i in range(1,n+1):
  k+=i
  j=1
  l=k
  inc=i
  while (i+j-1)%n:
    print(l,end=' ')
    l = l + inc
    inc+=1
    j+=1
  while j<=n:
   print(l,end=' ')
   inc-=1
   l = l + inc
   j+=1
  print()


for _ in range(int(input())):
 n=int(input())
 st=0
 for i in range(1,n+1):
  st=st+i
  l=[st]
  k=st
  for j in range(i,n+i-1):
   if j < n:
    x=k+j
   else:
    x=k+(2*n-j-1) 
   l.append(x)
   k=x 
  print(*l)


#from sys import stdin,stdout
#input=stdin.readline 
#import math,bisect
#from itertools import permutations
#from collections import Counter

for _ in range(int(input())):
 n=int(input())
 l=[[0 for i in range(n)] for j in range(n)]
 c=1
 cur=0
 while(1):
  i=0
  j=cur
  while(j>=0):
   l[i][j]=c
   c+=1
   j-=1
   i+=1
  cur+=1
  if cur==n:
   break

 cur=n-1
 curi=1
 while(1):
  i=curi
  j=cur
  if curi==n:
   break
  while(i!=n):
   l[i][j]=c
   c+=1
   i+=1
   j-=1
  curi+=1
  


 for i in range(n):
  for j in range(n):
   print(l[i][j],end=" ")
  print()

def printPattern(n):
 k=0
 for i in range(1,n+1):
  k+=i
  j=1
  l=k
  inc=i
  while (i+j-1)%n:
    print(l,end=' ')
    l = l + inc
    inc+=1
    j+=1
  while j<=n:
   print(l,end=' ')
   inc-=1
   l = l + inc
   j+=1
  print()
for tc in range(int(input())):
 n=int(input())
 printPattern(n)



# cook your dish here
t=int(input())
for z in range(0,t):
 N=int(input())
 li=[[i for i in range(0,N)] for j in range(0,N)]
 #print(li)
 c=1
 for j1 in range(0,N):
  i=0
  j=j1
  while j!=-1:
   li[i][j]=c
   i+=1
   j-=1
   c+=1
 for i1 in range(1,N):
  j=N-1
  i=i1
  while i!=N:
   li[i][j]=c
   i+=1
   j-=1
   c+=1
 for i in range(0,N):
  for j in range(0,N):
   print(li[i][j],end=" ")
  print()
   
 
   
  
 
 

# cook your dish here
t=int(input())
for z in range(0,t):
 N=int(input())
 li=[[i for i in range(0,N)] for j in range(0,N)]
 #print(li)
 c=1
 for j1 in range(0,N):
  i=0
  j=j1
  while j!=-1:
   li[i][j]=c
   i+=1
   j-=1
   c+=1
 for i1 in range(1,N):
  j=N-1
  i=i1
  while i!=N:
   li[i][j]=c
   i+=1
   j-=1
   c+=1
 for i in range(0,N):
  for j in range(0,N):
   print(li[i][j],end=" ")
  print()
   
 
   
  
 
 

for _ in range(int(input())):
 n=int(input())
 st=0
 for i in range(1,n+1):
  st=st+i
  l=[st]
  k=st
  for j in range(i,n+i-1):
   if j < n:
    x=k+j
   else:
    x=k+(2*n-j-1) 
   l.append(x)
   k=x 
  print(*l)

for _ in range(int(input())):
 n=int(input())
 x=1
 y=n
 a=[[0 for i in range(n)] for j in range(n)]
 for indx1,i in enumerate(a):
  if indx1==0:
   for indx2,j in enumerate(i):
    if indx2==0:
     a[indx1][indx2] = 1
    else:
     a[indx1][indx2] = a[indx1][indx2-1]+x
     x+=1
  else:
   for indx2,j in enumerate(i):
    if indx2==n-1:
     a[indx1][indx2] = a[indx1-1][indx2]+y
     y-=1
    else:
     a[indx1][indx2] = a[indx1-1][indx2+1]+1
 for i in a:
  for j in i:
   print(j,end=" ")
  print()

for _ in range(int(input())):
 n=int(input())
 l=[[0 for i in range(n)]for i in range(n)]
 l[0][0]=1
 for i in range(1,n):
  l[0][i]=l[0][i-1]+i
 for i in range(1,n):
  for j in range(n):
   if j==n-1:
    l[i][j]=l[i][j-1]+n-i
   else:
    l[i][j]=l[i-1][j+1]+1
 for i in range(n):
  for j in range(n):
   print(l[i][j],end=" ")
  print()

try:
 # cook your code here
 for _ in range(int(input())):
  N=int(input())
  X=list(range(1,N+1))+list(range(N-1,0,-1))
  A=[[] for i in range(N)]
  count=1
  start=0
  # print(X)
  for x in range(len(X)):
   if x>N-1:
    start+=1
   # print(A,start,X[x],count)
   for i in range(start,start+X[x]):
    A[i].append(count)
    count+=1
  for a in A:
   print(*a)
  # print(X)
 
  # print(A)
  # for a in A:
  #     print(*a)
 

except:
 pass

for i in range(int(input())):
 n = int(input())
 arr = [[0]*n for j in range(n)]
 c = 1
 for j in range(n):
  for k in range(j+1):
   arr[k][j-k] = c
   c+=1
 for j in range(1,n):
  for k in range(n-j):
   arr[j+k][n-k-1] = c
   c+=1
 for j in arr:
  print(*j)

for T in range(0,int(input())):
 if T<=10:
  N=int(input())
  if N<=100:
   L1=[[0 for i in range(0,N)] for j in range(0,N)]
   c=1
   for i in range(0,N):
    a=0
    for j in range(i,-1,-1):
     L1[a][j]=c
     c+=1
     a+=1
   
   
   for x in range(1,N):
    k=x
    for y in range(N-x):
     L1[k][N-y-1]=c
     c+=1
     k+=1
   for mat in L1:
    print(*mat) 
    


for _ in range(int(input())):
 n=int(input())
 l=[[0 for i in range(n)] for j in range(n)]
 #print(l)
 c=1
 for i in range(n):
  for j in range(i+1):
   l[j][i-j]=c
   c+=1
 for i in range(1,n):
  for j in range(n-i):
   l[i+j][n-j-1]=c
   c+=1
 for i in range(n):
  for j in range(n):
   print(l[i][j],end=" ")
  print()

# cook your dish here
t=int(input())
while t>0:
 n=int(input())
 l=[]
 for i in range(n):
  l1=[]
  for j in range(n):
   l1.append(0)
  l.append(l1)
 c=1
 for j in range(n):
  k=0
  for i in range(j,-1,-1):
   l[k][i]=c
   c+=1
   k+=1
 for i in range(1,n):
  m=i
  for j in range(n-i):
   l[m][n-j-1]=c
   c+=1
   m+=1
 for p in l:
  print(*p)
   
   
   
   
   
   
   
   
   
  
 t-=1

# cook your dish here
try:
 for i in range(int(input())):
  n=int(input())
  a=[[0 for i in range(n)] for j in range(n)]
  p=1
  for k in range(n):
   j=k
   i=0
   while(j>=0):
    a[i][j]=p
    p=p+1
    i=i+1
    j=j-1
  for k in range(1,n,1):
   i=k
   j=n-1
   f=k
   while(j>=f):
    a[i][j]=p
    p=p+1
    i=i+1
    j=j-1
  for i in range(n):
   for j in range(n):
    print(a[i][j],end=" ")
   print("\n")
except:
 pass

for test_ in range(int(input())):
 n = int(input())
 op = [[0 for i in range(n)] for i in range(n)]
 #num = list(range(1,n*n+1))
 i = 0
 j = 0
 num = 1
 while i<n and j<n:
  temp1 = i
  temp2 = j
  while temp1<=j:
   op[temp1][temp2] = num
   num += 1
   temp1 += 1
   temp2 -= 1
  if j!=n-1:
   i = 0
   j += 1
  else:
   i += 1
   j = n-1
 for i in op:
  print(*i)