Help me in solving EVENM problem

My issue

what is wrong with this code
for _ in range(int(input())):
n=int(input())
for i in range(n):
l=
for j in range(n):
if i>=j:
l.append(((max(i,j)+1)**2)-j)
else:
l.append(((max(i,j)+1)**2)-j-abs(i-j))
print(*l)
the outputs are
Sample Input
1
5
Your Output
1 2 5 10 17
4 3 6 11 18
9 8 7 12 19
16 15 14 13 20
25 24 23 22 21 it works well for all the possible values. but not accepting

My code

for _ in range(int(input())):
    n=int(input())
    for i in range(n):
        l=[]
        for j in range(n):
            if i>=j:
                l.append(((max(i,j)+1)**2)-j)
            else:
                l.append(((max(i,j)+1)**2)-j-abs(i-j))
        print(*l)

Problem Link: Even Matrix Practice Coding Problem - CodeChef

@inwrsk
here plzz refer my c++ code for better understanding of the logic

#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define nl "\n"
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define db double
#define mod 1e9 + 7
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf 1e18
#define sp " "
#define pb push_back
#define test lli t; cin>>t; while(t--)
int main()
{
   /*#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);*/
test
{
   lli n,c=1;
   cin>>n;
   lli mat[n][n];
   for(lli i=0;i<n;i++)
   {
       if(i%2==0)
       {
           for(lli j=0;j<n;j++)
           {
               mat[i][j]=c;
               c++;
           }
       }
       else
       {
           for(lli j=n-1;j>=0;j--)
           {
               mat[i][j]=c;
               c++;
           }
       }
   }
   for(lli i=0;i<n;i++)
   {
       for(lli j=0;j<n;j++)
       {
           cout<<mat[i][j]<<sp;
       }
       cout<<nl;
   }
}
}