static void Solution(TextReader sIn, TextWriter sOut)
int cases = int.Parse(sIn.ReadLine());
for (int c = 0; c < cases; ++c) {
int size = int.Parse(sIn.ReadLine());
bool evenM = (size & 1) == 0;
for (int row = 0; row < size; ++row) {
bool oddR = (row & 1) == 1;
for (int col = 0; col < size; ++col) {
bool last = col == size - 1;
int num = (row * size) + col + 1;
num = (num - col) + (size - col) - 1;
sOut.Write(String.Format("{0} ",num));
sOut.Write(String.Format("{0} ",num));
using(StreamWriter sw = new StreamWriter("text.txt"))
public static void Main()
StreamReader sr = new StreamReader("text.txt");
Solution(sr,Console.Out);