• Source
    1.  
    2. #include <bits/stdc++.h>
    3.  
    4. using namespace std;
    5.  
    6. #define int long long int
    7. #define F first
    8. #define S second
    9. #define pb push_back
    10. #define si set <int>
    11. #define vi vector <int>
    12. #define pii pair <int, int>
    13. #define vpi vector <pii>
    14. #define vpp vector <pair<int, pii>>
    15. #define mii map <int, int>
    16. #define mpi map <pii, int>
    17. #define spi set <pii>
    18. #define endl "\n"
    19. #define sz(x) ((int) x.size())
    20. #define all(p) p.begin(), p.end()
    21. #define double long double
    22. #define que_max priority_queue <int>
    23. #define que_min priority_queue <int, vi, greater<int>>
    24. #define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
    25. #define print(a) for(auto x : a) cout << x << " "; cout << endl
    26. #define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
    27. #define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
    28.  
    29. inline int power(int a, int b)
    30. {
    31. int x = 1;
    32. while (b)
    33. {
    34. if (b & 1) x *= a;
    35. a *= a;
    36. b >>= 1;
    37. }
    38. return x;
    39. }
    40.  
    41. template <typename Arg1>
    42. void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
    43. template <typename Arg1, typename... Args>
    44. void __f (const char* names, Arg1&& arg1, Args&&... args)
    45. {
    46. const char* comma = strchr (names + 1, ',');
    47. cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
    48. }
    49.  
    50. const int N = 200005;
    51. bool cmp(pair<int,int>a,pair<int,int>b){
    52. if((a.first+a.second)%2==0 && (b.first+b.second)%2==0){
    53. if(a.first!=b.first)return b.first>a.first;
    54. else return b.second>a.second;
    55. }
    56. if((a.first+a.second)%2!=0 && (b.first+b.second)%2!=0){
    57. if(a.first!=b.first)return b.first>a.first;
    58. else return b.second>a.second;
    59. }
    60. return (a.first+a.second)%2==0;
    61.  
    62.  
    63. }
    64.  
    65. void solve() {
    66.  
    67. int n;
    68. cin>>n;
    69. if(n==1)cout<<1<<endl;
    70. else if(n==2)cout<<-1<<endl;
    71. else{
    72. vector<pair<int,int>>v;
    73. for (int i = 0; i < n; i++)
    74. {
    75. for (int j = 0; j < n; j++)
    76. {
    77. v.pb({i,j});
    78. }
    79.  
    80. }
    81. vector<vector<int>>ans(n,vector<int>(n,0));
    82. sort(v.begin(),v.end(),cmp);
    83. int k=1;
    84. for (int i = 0; i < n*n; i++)
    85. {
    86. ans[v[i].first][v[i].second]=k;
    87. k++;
    88.  
    89. }
    90. for (int i = 0; i < n; i++)
    91. {
    92. print(ans[i]);
    93. }
    94.  
    95.  
    96. }
    97.  
    98.  
    99.  
    100.  
    101. }
    102.  
    103. int32_t main()
    104. {
    105. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    106.  
    107.  
    108. clock_t z = clock();
    109.  
    110. int t = 1;
    111. cin >> t;
    112. while (t--) solve();
    113.  
    114.  
    115.  
    116. return 0;
    117. }
    118.