A tennis tournament is about to take place with $N$ players participating in it.
Every player plays with every other player exactly once and there are no ties. That is, every match has a winner and a loser. 
With Naman's birthday approaching, he wants to make sure that each player wins the same number of matches so that nobody gets disheartened.  
Your task is to determine if such a scenario can take place and if yes find one such scenario.

-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow. 
- Each testcase contains of a single integer $N$ denoting number of players.

-----Output:-----
- If it's impossible for everyone to win the same number of matches, print "NO" (without quotes).
- Otherwise print "YES" (without quotes) and then print $N$ lines , each line should consist of a string containing only 0s and 1s and should be of size $N$. 
- If the jth character in the ith line is 1 then it means in the match between $i$ and $j$ , $i$ wins. 
- You will get a WA if the output does not correspond to a valid tournament, or if the constraints are not satisfied.
- You will get also WA verdict if any 2 lines have contradicting results or if a player beats himself.

-----Constraints-----
- $1 \leq T \leq 100$
- $2 \leq N \leq 100$

-----Subtasks-----
- 10 points : $2 \leq N \leq 6$
- 90 points :  Original Constraints. 

-----Sample Input:-----
2
3
2

-----Sample Output:-----
YES
010
001
100
NO

-----Explanation:-----
One such scenario for $N$ = $3$ is when player $1$ beats player $2$, player $2$ to beats player $3$ and player $3$ beats player $1$. Here all players win exactly $1$ match.

Difficulty: MEDIUM
Tags: Constructive algorithms

Ad-hoc

Starter code:

Solutions:
from math import comb
a = int(input())
for i in range(a):
	N = int(input())
	matche = comb(N, 2)
	win = matche / N
	ans = []
	if win != int(win):
		print('NO')
		continue
	print('YES')
	for k in range(N):
		list = []
		for j in range(N):
			list.append(0)
		ans.append(list)
		list = []
	for k in range(N):
		temp = win
		index = 0
		while temp != 0:
			if index + 1 != k + 1 and ans[k][index] != 'set':
				ans[k][index] = 1
				ans[index][k] = 'set'
				temp -= 1
			index += 1
	for k in ans:
		for j in k:
			if j == 'set':
				print(0, end='')
			else:
				print(j, end='')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	if n % 2 == 0 or n <= 2:
		print('NO')
	else:
		print('YES')
		num = n // 2
		j = 1
		k = 0
		for i in range(n):
			if n - num < j:
				print((k + 1) * str(1) + (num + 1) * str(0) + (num - k - 1) * str(1))
				k = k + 1
			else:
				print(j * str(0) + num * str(1) + (n - i - 1 - num) * str(0))
			j = j + 1


def inparr():
	tmparr = [int(i) for i in input().split()]
	return tmparr
for _ in range(int(input())):
	N = int(input())
	if N % 2:
		print('YES')
		mat = [[0] * N for i in range(N)]
		for i in range(N):
			curr = 1
			for j in range(i + 1, N):
				mat[i][j] = curr
				curr = 0 if curr == 1 else 1
				mat[j][i] = curr
		for i in mat:
			for j in i:
				print(j, end='')
			print()
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	if n % 2 != 0:
		print('YES')
		k = n // 2
		a = [['0' for _ in range(n)] for _ in range(n)]
		for i in range(n):
			d = 0
			for j in range(i + 1, n):
				a[i][j] = '1'
				d += 1
				if d == k:
					break
			if d < k:
				for j in range(k - d):
					a[i][j] = '1'
		for u in range(n):
			for p in range(n):
				print(a[u][p], end='')
			print()
	else:
		print('NO')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = [0] * n
	totalmatch = (n - 1) * n // 2
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		for i in range(1, n // 2 + 1):
			l[i] = 1
		l = l * 2
		for i in range(n, 0, -1):
			for j in l[i:n + i]:
				print(j, end='')
			print()


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		m = int(n / 2)
		print('YES')
		l = []
		s = '0' + m * '1' + (n - m - 1) * '0'
		for i in range(n):
			l.append(s)
			s = s[-1] + s[0:-1]
		for i in range(n):
			print(l[i])


t = int(input())
for _ in range(t):
	n = int(input())
	a = (n - 1) // 2
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		for i in range(n - a):
			print('0' * (i + 1) + '1' * a + '0' * (n - i - 1 - a))
		for i in range(a):
			print('1' * (i + 1) + '0' * (n - a) + '1' * (a - i - 1))


t = int(input())
for i in range(t):
	N = int(input())
	L = [0] * N
	if N % 2 == 1:
		print('YES')
		for i in range(0, N):
			for k in range(1, int((N + 1) / 2)):
				L[(i + k) % N] = 1
			for m in L:
				print(m, sep='', end='')
			print('')
			L = [0] * N
	else:
		print('NO')


import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
listin = lambda : list(map(int, input().split()))
mapin = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	n1 = n * (n - 1) // 2
	if n1 % n:
		print('NO')
	else:
		print('YES')
		x = [0]
		for i in range(n1 // n):
			x.append(1)
		for i in range(n1 // n):
			x.append(0)
		for i in range(n):
			print(''.join(map(str, x)))
			x = [x[-1]] + x[:-1]


def calc(n):
	a = n * (n - 1)
	a = int(a / 2)
	return a
t = int(input())
tt = 0
while tt < t:
	tt = tt + 1
	n = int(input())
	num = calc(n)
	if num % n != 0:
		print('NO')
	else:
		win = int(num / n)
		print('YES')
		i = 0
		while i < n:
			i = i + 1
			j = i
			str = ''
			k = 0
			while k < n:
				str = str + '0'
				k = k + 1
			k = 0
			while k < win:
				if (j + k) % n == 0:
					str = '1' + str[1:n]
				elif (j + k) % n == n - 1:
					str = str[:n - 1] + '1'
				else:
					kkk = (j + k) % n
					str = str[0:kkk] + '1' + str[kkk + 1:]
				k = k + 1
			print(str)


t = int(input())
for i in range(t):
	n = int(input())
	j = n - 1
	if j % 2 != 0:
		print('NO')
	else:
		print('YES')
		for i in range(1, n + 1):
			k = 1
			for j in range(1, n + 1):
				if i != j:
					if i % 2 != 0:
						if k % 2 != 0:
							print('1', end='')
							k += 1
						else:
							print('0', end='')
							k += 1
					elif k % 2 == 0:
						print('1', end='')
						k += 1
					else:
						print('0', end='')
						k += 1
				else:
					print('0', end='')
			print()


t = int(input())
for i in range(t):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		s = ''
		s = s + '0'
		for j in range(n // 2):
			s += '1'
		for j in range(n // 2 + 1, n):
			s += '0'
		print(s)
		k = n - 1
		for j in range(n - 1):
			s1 = s[0:len(s) - 1]
			s2 = s[len(s) - 1:]
			s = s2 + s1
			print(s)


t = int(input())
for i in range(0, t):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		s = '0'
		for j in range(0, n // 2):
			s += '1'
		for j in range(0, n - (n // 2 + 1)):
			s += '0'
		for j in range(0, n):
			print(s)
			c = s[n - 1]
			s = s[0:n - 1]
			s = c + s


try:
	for _ in range(int(input())):
		n = int(input())
		if n % 2 == 0:
			print('NO')
		else:
			z = int((n - 1) / 2)
			print('YES')
			for i in range(n):
				cnt = 0
				N = []
				while cnt < z:
					N.append((i + 1 + cnt) % n)
					cnt += 1
				for j in range(n):
					if j in N:
						print(1, end='')
					else:
						print(0, end='')
				print()
except EOFError:
	pass


for t in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
		continue
	print('YES')
	for i in range(n - 1):
		comp1 = '1' * max(0, i - n + n // 2 + 1)
		comp2 = '0' * min(i + 1, n // 2 + 1)
		comp3 = '1' * (n // 2)
		comp4 = '0' * n
		print((comp1 + comp2 + comp3 + comp4)[:n])
	print('1' * (n // 2) + '0' * (n - n // 2))


def Solve(n):
	total_matches = n * (n - 1) // 2
	if total_matches % n != 0:
		print('NO')
		return
	print('YES')
	ans = [['0' for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(1, n // 2 + 1):
			ans[i][(i + j) % n] = '1'
	for i in ans:
		print(''.join(i))
for _ in range(int(input())):
	n = int(input())
	Solve(n)


t = int(input())
for _ in range(t):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		mm = [['0' for _ in range(n)] for _ in range(n)]
		for r in range(n):
			wins = n // 2
			for c in range(n):
				if r != c and wins and (mm[r][c] == '0') and (mm[c][r] == '0'):
					mm[r][c] = '1'
					wins -= 1
		print('YES')
		for loop in range(n):
			print(''.join(mm[loop]))


for _ in range(int(input())):
	n = int(input())
	if n % 2 != 0:
		print('YES')
		no_ones = (n - 1) // 2
		str1 = ''
		for i in range(n):
			if i < no_ones:
				str1 += '1'
			else:
				str1 += '0'
		for j in range(n):
			str1 = str1[-1] + str1[:n - 1]
			print(str1)
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	games = n * (n - 1) // 2
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			out = [0] * n
			for j in range(n // 2):
				u = (i + j + 1) % n
				out[u] = 1
			for i in range(n):
				print(out[i], end='')
			print()


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		w = int(n / 2)
		l = []
		for i in range(n):
			col = []
			for j in range(n):
				col.append(0)
			l.append(col)
		for i in range(n):
			p = w
			for j in range(n):
				if i == j:
					print(0, end='')
				elif j > i:
					if p > 0:
						print(1, end='')
						l[i][j] = 0
						p = p - 1
					else:
						print(0, end='')
						l[i][j] = 1
				elif i > j:
					print(l[j][i], end='')
			print('')


T = int(input())
for i in range(T):
	N = int(input())
	if N % 2 == 0:
		print('NO')
	else:
		print('YES')
		wins = (N - 1) // 2
		score = 1 * '0' + wins * '1' + (N - 1 - wins) * '0'
		for i in range(N):
			print(score)
			score = score[-1] + score[0:-1]


try:
	n = int(input())
	for _ in range(n):
		case = int(input())
		if case % 2 == 0:
			print('NO')
		else:
			print('YES')
			for i in range(case // 2 + 1):
				arr = [0] * case
				for j in range(i + 1, case // 2 + i + 1):
					arr[j] = 1
				print(*arr, sep='')
			for i in range(case // 2):
				arr = [0] * case
				for j in range(i + 1):
					arr[j] = 1
				for k in range(1, case // 2 - i):
					arr[-k] = 1
				print(*arr, sep='')
except EOFError:
	pass


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		lst = []
		for i in range(n):
			lst.append([0] * n)
		for i in range(n):
			if i % 2 == 0:
				for j in range(i):
					if j % 2 == 0:
						lst[i][j] = 1
				for k in range(i):
					if k % 2 != 0:
						lst[k][i] = 1
			else:
				for j in range(i):
					if j % 2 != 0:
						lst[i][j] = 1
				for k in range(i):
					if k % 2 == 0:
						lst[k][i] = 1
		for i in range(n):
			ans = ''
			for j in range(n):
				ans += str(lst[i][j])
			print(ans)


arr = [[0 for i in range(101)] for i in range(101)]
for i in range(1, 100):
	k = 0
	for j in range(i + 1, 101):
		arr[i][j] = k
		k = 1 - k
		arr[j][i] = k
t = int(input())
while t:
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		a = [arr[i][-n:] for i in range(-n, 0)]
		for i in range(0, n):
			print(*a[i], sep='')
	t -= 1


for _ in range(int(input())):
	N = int(input())
	if N % 2 == 0:
		print('NO')
	else:
		print('YES')
		n = N // 2
		for i in range(n + 1):
			print('0' * (i + 1) + '1' * n + '0' * (n - i))
		for i in range(n):
			print('1' * (i + 1) + '0' * (n + 1) + '1' * (n - i - 1))


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
		continue
	print('YES')
	r = -(-n // 2)
	for i in range(1, r + 1):
		x = '0' * i + (r - 1) * '1'
		x += '0' * (n - len(x))
		print(x)
	for j in range(r + 1, n + 1):
		x = (r - 1 - (n - j)) * '1'
		x += (j - len(x)) * '0'
		x += '1' * (n - len(x))
		print(x)


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		d = {}
		k = n * (n - 1) // 2
		r = k // n
		l = [[0 for i in range(n)] for j in range(n)]
		for i in range(1, n):
			for j in range(i + 1, n + 1):
				if d.get(i, 0) < r:
					l[i - 1][j - 1] = 1
					d[i] = d.get(i, 0) + 1
				else:
					l[j - 1][i - 1] = 1
					d[j] = d.get(j, 0) + 1
		print('YES')
		for i in range(n):
			for j in range(n):
				print(l[i][j], end='')
			print()


t = int(input())
for i in range(t):
	n = int(input())
	if n % 2 == 0 or n == 1:
		print('NO')
		continue
	else:
		print('YES')
		p = int((n - 1) / 2)
		for j in range(n):
			c = p + j - n + 1
			for k in range(n):
				if j + p >= n and c >= 1:
					print('1', end='')
					c = c - 1
				elif k >= j + 1 and k <= j + p:
					print('1', end='')
				else:
					print('0', end='')
			print('\n', end='')


for _ in range(int(input())):
	N = int(input())
	if N % 2 == 0:
		print('NO')
	else:
		k = int((N - 1) / 2)
		print('YES')
		S = [''] * N
		for i in range(N):
			if i % 2 == 0:
				S[0] += '0'
				S[1] += '1'
			else:
				S[0] += '1'
				S[1] += '0'
		S[1] = S[1][1:]
		S[1] = '0' + S[1]
		for i in range(2, N):
			if i % 2 == 0:
				S[i] = '1' + S[i - 1]
				S[i] = S[i].rstrip(S[i - 1][-1])
			else:
				S[i] = '0' + S[i - 1]
				S[i] = S[i].rstrip(S[i - 1][-1])
		for i in range(N):
			print(S[i])


for _ in range(int(input())):
	a = int(input())
	if a % 2 == 0:
		print('NO')
	else:
		print('YES')
		l = []
		for i in range(a):
			count = a // 2
			b = []
			for j in range(a):
				b.append(0)
				if i == j:
					b[j] = 0
				elif j > i and count != 0:
					b[j] = 1
					count = count - 1
				elif j < a / 2:
					if l[j][i] == 1:
						b[j] = 0
					else:
						b[j] = 1
			l.append(b)
			print(*b, sep='')


t = int(input())
for _ in range(t):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		zero = n // 2
		lst = n - zero - 1
		val = '0' + zero * '1' + '0' * lst
		for i in range(n):
			print(val)
			lst = val[-1]
			val = lst + val[:-1]


for i in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		for j in range(n):
			a = [0] * n
			for x in range(n // 2):
				a[(j + x + 1) % n] = 1
			for _ in a:
				print(_, end='')
			print()


for _ in range(int(input())):
	N = int(input())
	if (N - 1) % 2 != 0:
		print('NO')
	else:
		print('YES')
		for i in range(N):
			counter = 0
			for j in range(N):
				if i + (N - 1) / 2 >= N and j <= (N - 1) / 2 + i - N:
					print(1, end='')
					counter += 1
				elif j == i:
					print(0, end='')
				elif counter < (N - 1) / 2 and j > i:
					print(1, end='')
					counter += 1
				else:
					print(0, end='')
			print()


tc = int(input())
for _ in range(tc):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		wins = (n - 1) // 2
		for i in range(1, n + 1):
			if i + wins <= n:
				for j in range(n):
					if j >= i and j < i + wins:
						print(1, end='')
					else:
						print(0, end='')
			else:
				for j in range(n):
					if j <= (i + wins - 1) % n or j >= i:
						print(1, end='')
					else:
						print(0, end='')
			print('')


for q in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
		continue
	s = ''
	for i in range(n // 2):
		s += '01'
	s += '0'
	ans = []
	print('YES')
	for i in range(n):
		t = s[-i:] + s[:-i]
		print(t)


def combi(n):
	return int(n * (n - 1) / 2)
for _ in range(int(input())):
	n = int(input())
	com = combi(n)
	if com % n != 0:
		print('NO')
	else:
		ans = [['0' for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(1, n // 2 + 1):
				ans[i][(i + j) % n] = '1'
		print('YES')
		for line in ans:
			print(''.join(line))


t = int(input())
for i in range(0, t):
	a = int(input())
	b = int(a / 2)
	n = a - b - 1
	s = []
	c = '0'
	s.append(c)
	if a % 2 != 0:
		print('YES')
		for j in range(0, n):
			d = '1'
			s.append(d)
		for l in range(0, n):
			s.append(c)
		str1 = ''
		for ele in s:
			str1 += ele
		print(str1)
		for k in range(0, a - 1):
			temp = s[a - 1]
			for l in range(a - 1, 0, -1):
				s[l] = s[l - 1]
			s[0] = temp
			str2 = ''
			for ele1 in s:
				str2 += ele1
			print(str2)
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			z = ['0'] * n
			for j in range(1, n // 2 + 1):
				z[(i + j) % n] = '1'
			print(''.join(z))


T: int = int(input())
for q in range(T):
	n: int = int(input())
	x: int = n * (n - 1) // 2
	if x % n != 0:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			op: str = ''
			for j in range(n):
				if j >= i + 1 and j <= i + x // n:
					op += '1'
				elif i + x // n >= n:
					c = i + x // n - n
					if j <= c:
						op += '1'
					else:
						op += '0'
				else:
					op += '0'
			print(op)


for _ in range(int(input())):
	N = int(input())
	if N % 2 == 0:
		print('NO')
	else:
		print('YES')
		t = int((N - 1) / 2)
		A = [0] * N
		for i in range(t):
			A[i + 1] = 1
		for i in range(N):
			outp = ''.join((str(item) for item in A))
			print(outp)
			A.insert(0, A.pop(N - 1))


for t in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		n1 = n // 2
		for i in range(n):
			s = ['0'] * n
			c = 0
			for j in range(i + 1, n):
				s[j] = '1'
				c += 1
				if c == n1:
					break
			if c < n1:
				for j in range(n1 - c):
					s[j] = '1'
			k = ''.join(s)
			print(k)


a = int(input())
for i in range(a):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		for i1 in range(n):
			li = [0] * n
			b = str()
			for i2 in range((n - 1) // 2):
				li[(i1 + i2 + 1) % n] += 1
			for i3 in range(len(li)):
				b += str(li[i3])
			print(b)


for _ in range(int(input())):
	n = int(input())
	l = [['0'] * n for i in range(n)]
	tot = n * (n - 1) // 2
	if (n - 1) % 2 == 0:
		print('YES')
		for i in range(n):
			res = ''
			for j in range(1, n // 2 + 1):
				l[i][(i + j) % n] = '1'
		for i in l:
			print(''.join(i))
	else:
		print('NO')


t = int(input())
for e in range(t):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		total = 0
		for g in range(n):
			total += g
		num = total // n
		for i in range(n):
			l = [0] * n
			for j in range(i + 1, i + 1 + num):
				try:
					l[j] = 1
				except IndexError:
					k = j - n
					l[k] = 1
			for b in range(len(l)):
				print(l[b], end='')
			print()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	matches = n * (n - 1) // 2
	if matches % n != 0:
		print('NO')
	else:
		print('YES')
		wins = matches // n
		A = []
		for i in range(n):
			a = []
			for j in range(n):
				a.append(0)
			A.append(a)
		for i in range(n):
			start = i + 1
			if start + wins < n:
				A[i][start:start + wins] = [1] * wins
			else:
				wins1 = n - 1 - i
				wins2 = wins - wins1
				A[i][start:] = [1] * wins1
				A[i][:wins2] = [1] * wins2
		for i in range(n):
			for j in range(n):
				print(A[i][j], end='')
			print()


for _ in range(int(input())):
	n = int(input())
	if (n - 1) % 2 != 0:
		print('NO')
	else:
		print('YES')
		b = [0] * n
		for i in range((n - 1) // 2):
			b[i + 1] = 1
		print(*b, sep='')
		for j in range(n - 1, 0, -1):
			print(*b[j:] + b[:j], sep='')


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		m = (n - 1) // 2
		for i in range(n):
			l = i + 1
			u = l + m
			if l >= n:
				l -= n
			if u > n:
				u -= n
			for j in range(n):
				if l < u:
					if j in range(l, u):
						print(1, end='')
					else:
						print(0, end='')
				elif j in range(l, n) or j in range(0, u):
					print(1, end='')
				else:
					print(0, end='')
			print()


T = int(input())
while T > 0:
	players = int(input())
	matches = (players ** 2 - players) / 2
	if matches % players == 0:
		print('YES')
		should_win = int(matches / players)
		for index in range(0, players):
			lst = [0] * players
			start_position = index + 1
			if start_position >= players:
				start_position = start_position - players
			end_position = start_position + should_win - 1
			if end_position < players:
				lst[start_position:end_position + 1] = [1] * should_win
			else:
				end_ones = players - start_position
				lst[start_position:] = [1] * end_ones
				remaining = should_win - end_ones
				lst[:remaining] = [1] * remaining
			for number in lst:
				print(number, end='')
			print('\n', end='')
	else:
		print('NO')
	T -= 1


test_cases = int(input())
for i in range(test_cases):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		l = [[str(0) for i in range(n)] for j in range(n)]
		for i in range(len(l)):
			win = 1
			for j in range(i + 1, len(l)):
				if win:
					l[i][j] = str(1)
				else:
					l[j][i] = str(1)
				if win:
					win = 0
				else:
					win = 1
			print(''.join(l[i]))


import sys
R = lambda : map(int, next(sys.stdin).split())
T = int(input())
for i in range(T):
	N = int(input())
	if N % 2 == 0:
		print('NO')
	else:
		print('YES')
		wins = (N - 1) // 2
		score = 1 * '0' + wins * '1' + (N - 1 - wins) * '0'
		for i in range(N):
			print(score)
			score = score[-1] + score[0:-1]


for t in range(int(input())):
	n = int(input())
	a = n * (n - 1) / 2
	if a % n == 0:
		print('YES')
		r = int(a / n)
		rem = n - r - 1
		l = '0' + '1' * r + '0' * rem
		for u in range(n):
			print(l)
			l = l[-1] + l[:-1]
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	FLAG = 'NO'
	if (n - 1) % 2 == 0:
		FLAG = 'YES'
	print(FLAG)
	if FLAG == 'YES':
		l = [[0 for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(n):
				if (n + i + 1) % n == j:
					for k in range(int((n - 1) / 2)):
						l[i][(n + i + 1 + k) % n] = 1
		for x in l:
			print(*x, sep='')
	else:
		pass


t = int(input())
for s in range(t):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		a = [[0 for i in range(n)] for i in range(n)]
		i = 0
		j = 1
		k = 1
		while k < n:
			i = 0
			j = k
			while i < n and j < n:
				a[i][j] = 1
				i += 1
				j += 1
			k += 2
		i = 1
		j = 0
		k = 1
		while k < n:
			i = k
			j = 0
			while i < n and j < n:
				if a[j][i] == 0:
					a[i][j] = 1
				i += 1
				j += 1
			k += 1
		for i in range(n):
			for item in a[i]:
				print(item, end='')
			print(' ')


import numpy as np
for i in range(int(input())):
	N = int(input())
	if (N - 1) % 2 == 0:
		print('YES')
		grid = np.zeros([N, N])
		for j in range(N - 1):
			if j < N / 2:
				grid[j, :] = np.concatenate((np.zeros(j + 1), np.ones(int((N - 1) / 2)), np.zeros(int((N - 1) / 2) - j)))
			else:
				grid[j, :] = np.concatenate((np.zeros(j + 1), np.ones(N - j - 1)))
		for j in range(N):
			for k in range(j):
				if grid[k, j] == 1:
					grid[j, k] = 0
				if grid[k, j] == 0:
					grid[j, k] = 1
		for j in range(N):
			print(''.join(list(map(str, list(map(int, grid[j]))))))
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		lis = [[0 for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(n):
				if i == j:
					lis[i][j] = 0
				if j - i >= 1 and j - i <= n // 2:
					lis[i][j] = 1
				if i >= n // 2 + 1 and i - j > n // 2:
					lis[i][j] = 1
		print('YES')
		for i in lis:
			for j in i:
				print(j, end='')
			print()


for _ in range(int(input())):
	no = int(input())
	if no % 2 == 0:
		print('NO')
	else:
		print('YES')
		t = no // 2
		s = '1' * t + '0' * (no - t)
		for i in range(1, no + 1):
			print(s[-i:] + s[:-i])


for _ in range(int(input())):
	n = int(input())
	sm = n * (n - 1) // 2
	if sm % n != 0:
		print('NO')
	else:
		wins = sm // n
		print('YES')
		x = '0' * (n - wins) + '1' * wins
		for i in range(n):
			print(x)
			x = x[-1] + x[:n - 1]


for _ in range(int(input())):
	a = int(input())
	s = ''
	if a % 2 == 0:
		print('NO')
	else:
		b = (a - 1) // 2
		print('YES')
		for i in range(a):
			if i < b:
				s = s + '1'
			else:
				s += '0'
		for j in range(a):
			s = s[-1] + s[:a - 1]
			print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			for j in range(n):
				if i == j:
					print(0, end='')
				elif i < n - n // 2:
					if j > i and j <= i + n // 2:
						print(1, end='')
					else:
						print(0, end='')
				elif j > i and j <= i + n // 2:
					print(1, end='')
				else:
					a = n // 2 - (n - i - 1)
					if j < a:
						print(1, end='')
					else:
						print(0, end='')
			print()


T = int(input())
for _ in range(T):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		count = 0
		for _ in range(n):
			count += 1
			d = [0 for _ in range(n)]
			z = int((n - 1) / 2)
			for i in range(z):
				if count + i < n - 1:
					d[count + i] = 1
				else:
					d[count + i - n] = 1
			for i in d:
				print(i, end='')
			print()


t = int(input())
while t > 0:
	n = int(input())
	res = n * (n - 1) // 2
	if res % n != 0:
		print('NO')
	else:
		l = [[0 for i in range(n)] for j in range(n)]
		c = 0
		m = res // n
		print('YES')
		for i in range(n):
			j = (i + 1) % n
			while c < m:
				l[i][j] = 1
				c += 1
				j = (j + 1) % n
			c = 0
			i += 1
		for i in range(n):
			for j in range(n):
				print(l[i][j], end='')
			print()
	t -= 1


T = int(input())
for x in range(T):
	n = int(input())
	if n % 2 == 0:
		print('NO')
	else:
		print('YES')
		k = int((n - 1) / 2)
		s = [''] * n
		for i in range(n):
			if i % 2 == 0:
				s[0] += '0'
				s[1] += '1'
			else:
				s[0] += '1'
				s[1] += '0'
		s[1] = s[1][1:]
		s[1] = '0' + s[1]
		for i in range(2, n):
			if i % 2 == 0:
				s[i] = '1' + s[i - 1]
				s[i] = s[i].rstrip(s[i - 1][-1])
			else:
				s[i] = '0' + s[i - 1]
				s[i] = s[i].rstrip(s[i - 1][-1])
		for i in range(n):
			print(s[i])


t = int(input())
for _ in range(t):
	n = int(input())
	total = round(n * (n - 1) / 2)
	if total % n == 0:
		print('YES')
		win = round(total / n)
		matches = [['0'] * n for _ in range(n)]
		s_pos = 1
		for r in range(n):
			c_pos = s_pos
			for c in range(win):
				matches[r][c_pos] = '1'
				c_pos = (c_pos + 1) % n
			s_pos = (s_pos + 1) % n
		for itr in matches:
			print(''.join(itr))
	else:
		print('NO')