Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well.

Arya has a chessboard with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$); a square in row $r$ and column $c$ is denoted by $(r, c)$.

Arya has already placed $K$ rooks on the chessboard in such a way that no two rooks attack each other. Note that two rooks attack each other if they are in the same row or in the same column and there is no rook between them. She is busy learning how to tame a dragon right now, so she asked you to place as many other rooks as possible on the chessboard in such a way that afterwards, no two rooks on the chessboard attack each other. Help Arya and choose the positions of these rooks.

------  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 line of each test case contains two space-separated integers $N$ and $K$.
Each of the next $K$ lines contains two space-separated integers $r$ and $c$ denoting that Arya placed a rook on the square $(r, c)$.

------  Output ------
For each test case:
Let's assume that you placed $P$ rooks on squares $(r_{1}, c_{1}), (r_{2}, c_{2}), \ldots, (r_{P}, c_{P})$.
You should print a single line containing $2P+1$ space-separated integers $P, r_{1}, c_{1}, \ldots, r_{P}, c_{P}$.
$P$ must be maximum possible.
If there are multiple possible answers, the sequence $r_{1}, c_{1}, \ldots, r_{P}, c_{P}$ should be lexicographically smallest.

------  Constraints  ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{6}$
$0 ≤ K ≤ N$
$1 ≤ r_{i}, c_{i} ≤ N$ for each valid $i$
no two initially placed rooks attack each other
the positions of all initially placed rooks are pairwise distinct
the sum of $N$ over all test cases does not exceed $10^{6}$

------  Subtasks ------
Subtask #1 (10 points):
$T = 10$
$N ≤ 8$

Subtask #2 (20 points):
$T = 10$
$N ≤ 1,000$

Subtask #3 (70 points): original constraints

----- Sample Input 1 ------ 
2
4 2
1 4
2 2
4 0
----- Sample Output 1 ------ 
2 3 1 4 3
4 1 1 2 2 3 3 4 4

Difficulty: MEDIUM_HARD

Solutions:
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	rows = set(range(n))
	cols = set(range(n))
	for _ in range(k):
		(r, c) = map(int, input().split())
		rows.remove(r - 1)
		cols.remove(c - 1)
	p = min(len(rows), len(cols))
	result = [p]
	rl = list(rows)
	rl.sort()
	cl = list(cols)
	cl.sort()
	for i in range(p):
		result.append(rl[i] + 1)
		result.append(cl[i] + 1)
	print(*result)


import bisect
for _ in range(int(input())):
	(N, K) = list(map(int, input().split()))
	rows = [i + 1 for i in range(N)]
	columns = [i + 1 for i in range(N)]
	popcols = []
	poprows = []
	for i in range(K):
		(r, c) = list(map(int, input().split()))
		bisect.insort(poprows, r - 1)
		bisect.insort(popcols, c - 1)
	popcols = popcols[::-1]
	poprows = poprows[::-1]
	for i in popcols:
		columns.pop(i)
	for i in poprows:
		rows.pop(i)
	columns = columns[::-1]
	rows = rows[::-1]
	return_thing = []
	while len(columns) != 0 and len(rows) != 0:
		return_thing.append(rows[-1])
		return_thing.append(columns[-1])
		rows.pop()
		columns.pop()
	print(len(return_thing) // 2, *return_thing)


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	row = [0] * (n + 1)
	col = [0] * (n + 1)
	for i in range(k):
		(r, c) = map(int, input().split())
		row[r] = col[c] = 1
	M = []
	i = 1
	j = 1
	while i <= n and j <= n:
		if row[i]:
			i += 1
		elif col[j]:
			j += 1
		else:
			M.append(i)
			M.append(j)
			i += 1
			j += 1
	M.insert(0, len(M) // 2)
	print(*M)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	visited1 = [0] * (n + 1)
	visited2 = [0] * (n + 1)
	for i in range(k):
		(r, c) = map(int, input().split())
		visited1[r] = 1
		visited2[c] = 1
	ans = [n - k]
	count = 0
	i = 1
	j = 1
	for s in range(n - k):
		while visited1[i]:
			i += 1
		while visited2[j]:
			j += 1
		ans.append(i)
		ans.append(j)
		i += 1
		j += 1
	print(*ans)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	rli = set(list(range(1, n + 1)))
	cli = rli
	r1 = set()
	c1 = set()
	for i in range(k):
		(a, b) = map(int, input().split())
		r1.add(a)
		c1.add(b)
	rli = rli.difference(r1)
	cli = cli.difference(c1)
	rli = list(rli)
	rli.sort()
	cli = list(cli)
	cli.sort()
	ans = n - k
	ansli = list()
	for j in range(ans):
		ansli.append(rli[j])
		ansli.append(cli[j])
	print(ans, *ansli)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = [0] * (n + 1)
	b = [0] * (n + 1)
	for j in range(k):
		(r, c) = map(int, input().split())
		a[r] = b[c] = 1
	m = []
	x = 1
	y = 1
	while x <= n and y <= n:
		if a[x]:
			x += 1
		elif b[y]:
			y += 1
		else:
			m.append(x)
			m.append(y)
			x += 1
			y += 1
	m.insert(0, len(m) // 2)
	print(*m)


def solve():
	res = []
	free_col = []
	free_row = []
	for i in range(n - 1, -1, -1):
		if i not in rows:
			free_row.append(i)
		if i not in cols:
			free_col.append(i)
	for i in range(n - k):
		res.append(free_row.pop() + 1)
		res.append(free_col.pop() + 1)
	print(len(res) // 2, *res)
t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	rows = set()
	cols = set()
	for i in range(k):
		(r, c) = list(map(int, input().split()))
		rows.add(r - 1)
		cols.add(c - 1)
	solve()


T = int(input())
ans = []
for _ in range(T):
	(N, K) = [int(i) for i in input().split()]
	R = [False for i in range(N)]
	C = [False for i in range(N)]
	for i in range(K):
		(r, c) = [int(i) for i in input().split()]
		R[r - 1] = True
		C[c - 1] = True
	P = N - K
	pr = 0
	pc = 0
	s = str(P)
	while True:
		while pr < N and R[pr] == True:
			pr += 1
		while pc < N and C[pc] == True:
			pc += 1
		if pr == N or pc == N:
			break
		s += ' {} {}'.format(pr + 1, pc + 1)
		R[pr] = True
		C[pc] = True
	ans.append(s)
for i in ans:
	print(i)


for _ in range(int(input())):
	(N, K) = [int(x) for x in input().split()]
	r = []
	c = []
	for i in range(K):
		raw = input().split()
		r.append(int(raw[0]))
		c.append(int(raw[1]))
	full_set = set((x for x in range(1, N + 1)))
	rinp_set = set(r)
	cinp_set = set(c)
	rout_set = full_set - rinp_set
	cout_set = full_set - cinp_set
	rook_pos = []
	for (r, c) in zip(sorted(list(rout_set)), sorted(list(cout_set))):
		rook_pos.append(str(r))
		rook_pos.append(str(c))
	print(N - K, ' '.join(rook_pos))


tc = int(input())
for _ in range(tc):
	(n, k) = list(map(int, input().split()))
	(rows, cols) = (set(), set())
	cfull = rfull = set((i for i in range(1, n + 1)))
	for kk in range(k):
		(x, y) = list(map(int, input().split()))
		rows.add(x)
		cols.add(y)
	rfull = list(rfull - rows)
	cfull = list(cfull - cols)
	rfull.sort()
	cfull.sort()
	ans = []
	for i in range(len(rfull)):
		ans.append(rfull[i])
		ans.append(cfull[i])
	ans.insert(0, len(rfull))
	ans = list(map(str, ans))
	print(' '.join(ans))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	rows = [0] * n
	cols = [0] * n
	for i in range(k):
		(r, c) = map(int, input().split())
		rows[r - 1] = 1
		cols[c - 1] = 1
	r = 0
	c = 0
	ans = []
	s = ''
	while r < n and c < n:
		if rows[r] == 0 and cols[c] == 0:
			ans.append((r + 1, c + 1))
			s += ' ' + str(r + 1) + ' ' + str(c + 1)
			r += 1
			c += 1
		else:
			if rows[r] == 1:
				r += 1
			if cols[c] == 1:
				c += 1
	print(str(len(ans)) + s)


for _ in range(int(input())):
	(N, K) = map(int, input().split())
	R = [False] * (N + 1)
	C = [False] * (N + 1)
	for i in range(K):
		(r, c) = map(int, input().split())
		R[r] = True
		C[c] = True
	remainingRows = []
	remainingColumns = []
	for i in range(1, N + 1):
		if not R[i]:
			remainingRows.append(i)
		if not C[i]:
			remainingColumns.append(i)
	P = len(remainingRows)
	S = str(P)
	for i in range(P):
		S += ' ' + str(remainingRows[i]) + ' ' + str(remainingColumns[i])
	print(S)


for _ in range(int(input())):
	(n, k) = input().split()
	n = int(n)
	k = int(k)
	rows = [0] * n
	cols = [0] * n
	p = k
	while k:
		(a, b) = input().split()
		a = int(a)
		b = int(b)
		rows[a - 1] = 1
		cols[b - 1] = 1
		k -= 1
	if p == n:
		print(0)
	else:
		print(n - p, end=' ')
		r = []
		c = []
		for i in range(len(rows)):
			if rows[i] != 1:
				r.append(i + 1)
		for i in range(len(cols)):
			if cols[i] != 1:
				c.append(i + 1)
		for i in range(len(r) - 1):
			print(r[i], c[i], end=' ')
		print(r[-1], c[-1])


T = int(input())
for i in range(T):
	(N, K) = list(map(int, input().split()))
	r1 = c1 = set([i for i in range(N)])
	c2 = set()
	r2 = set()
	for j in range(K):
		(r, c) = list(map(int, input().split()))
		r2.add(r - 1)
		c2.add(c - 1)
	c3 = sorted(list(c1 - c2))
	r3 = sorted(list(r1 - r2))
	ans = list(zip(r3, c3))
	s = []
	for i in ans:
		for j in i:
			s.append(str(j + 1))
	print(len(s) // 2, ' '.join(s))


for _ in range(int(input())):
	(N, K) = list(map(int, input().split()))
	all_rows = all_cols = set([i for i in range(N)])
	blk_col = set()
	blk_row = set()
	for j in range(K):
		(r, c) = list(map(int, input().split()))
		blk_row.add(r - 1)
		blk_col.add(c - 1)
	av_col = sorted(list(all_cols - blk_col))
	av_row = sorted(list(all_rows - blk_row))
	ans = list(zip(av_row, av_col))
	pr = []
	for i in ans:
		for j in i:
			pr.append(str(j + 1))
	print(len(pr) // 2, ' '.join(pr))


def singleRooks(n, rooks):
	rows = set((rook[0] for rook in rooks))
	cols = set((rook[1] for rook in rooks))
	result = [0]
	node = [1, 1]
	while node[0] <= n and node[1] <= n:
		if node[0] in rows:
			node[0] += 1
		elif node[1] in cols:
			node[1] += 1
		else:
			result[0] += 1
			result.extend(node)
			node = [node[0] + 1, node[1] + 1]
	return result
for _ in range(int(input())):
	(n, placedRooks) = [int(i) for i in input().split()]
	rooks = []
	for _ in range(placedRooks):
		rooks.append([int(i) for i in input().split()])
	print(*singleRooks(n, rooks))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	row = {}
	col = {}
	for i in range(1, n + 1):
		row[i] = 0
		col[i] = 0
	for i in range(k):
		(a, b) = map(int, input().split())
		row[a] = 1
		col[b] = 1
	x = []
	for i in range(1, n + 1):
		if not col[i]:
			x.append(i)
	ans = []
	j = 0
	for i in range(1, n + 1):
		if not row[i]:
			ans += [i, x[j]]
			j += 1
	print(n - k, *ans)


t = int(input())
for _ in range(t):
	s = input().split(' ')
	(n, k) = (int(s[0]), int(s[1]))
	x = []
	y = []
	r = {}
	c = {}
	for _ in range(k):
		s = input().split(' ')
		r[int(s[0])] = 'T'
		c[int(s[1])] = 'T'
	for i in range(1, n + 1):
		if i not in r:
			x.append(i)
		if i not in c:
			y.append(i)
	l = []
	l.append(int(len(x)))
	for i in range(len(x)):
		l.append(x[i])
		l.append(y[i])
	print(*l)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = []
	b = []
	c = [w for w in range(1, n + 1)]
	d = [w for w in range(1, n + 1)]
	for i in range(k):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	if n == k:
		print(0)
		continue
	else:
		c = list(set(c) - set(a))
		d = list(set(d) - set(b))
		c.sort()
		d.sort()
		print(len(c), end=' ')
		for i in range(len(c) - 1):
			print(c[i], d[i], end=' ')
		print(c[len(c) - 1], d[len(c) - 1])


from collections import defaultdict as dd
t = int(input())
for _ in range(t):
	d1 = dd(int)
	d2 = dd(int)
	ans = []
	(n, k) = map(int, input().strip().split(' '))
	row = set()
	col = set()
	for i in range(k):
		(r, c) = map(int, input().strip().split(' '))
		d1[r] = 1
		d2[c] = 1
	d2next = 1
	for i in range(1, n + 1):
		if d1[i] == 0:
			while d2[d2next] != 0:
				d2next += 1
			if d2next <= n:
				ans.append(i)
				d1[i] = 1
				ans.append(d2next)
				d2[d2next] = 1
			else:
				break
	print(len(ans) // 2, *ans)


t = int(input())
for T in range(t):
	(n, k) = [int(x) for x in input().split()]
	(p, j) = (n - k, 1)
	ans = [0] * (2 * p + 1)
	ans[0] = p
	r = [0] * k
	c = [0] * k
	for i in range(k):
		(r[i], c[i]) = [int(x) for x in input().split()]
	r1 = set(r)
	c1 = set(c)
	for i in range(1, n + 1):
		if j > 2 * p - 1:
			break
		if i not in r1:
			ans[j] = i
			j += 2
	j = 2
	for i in range(1, n + 1):
		if j > 2 * p:
			break
		if i not in c1:
			ans[j] = i
			j += 2
	print(*ans)


T = int(input())
for i in range(T):
	(N, K) = [int(x) for x in input().split()]
	diff = N - K
	Row = []
	Col = []
	L_Row = []
	L_Col = []
	for i in range(1, N + 1):
		L_Row.append(i)
		L_Col.append(i)
	for i in range(K):
		(a, b) = [int(x) for x in input().split()]
		Row.append(a)
		Col.append(b)
	S_Row = set(L_Row)
	S_Col = set(L_Col)
	I_Row = set(Row)
	I_Col = set(Col)
	A1 = S_Row - I_Row
	A2 = S_Col - I_Col
	Q1 = list(A1)
	Q2 = list(A2)
	Q1.sort()
	Q2.sort()
	PT = [diff]
	for i in range(diff):
		PT.append(Q1[i])
		PT.append(Q2[i])
	print(*PT)


t = int(input())
for _ in range(t):
	kk = list(map(int, input().split()))
	n = kk[0]
	q = kk[1]
	a0 = []
	a1 = []
	a = []
	for __ in range(q):
		k = list(map(int, input().split()))
		a.append(k)
		a0.append(k[0])
		a1.append(k[1])
	b0 = [0] * (n + 1)
	b1 = [0] * (n + 1)
	l = n - len(a)
	s = str(l)
	s += ' '
	for i in range(q):
		b0[a0[i]] = 1
		b1[a1[i]] = 1
	ans0 = []
	ans1 = []
	for i in range(1, n + 1):
		if b0[i] == 0:
			ans0.append(i)
		if b1[i] == 0:
			ans1.append(i)
	for i in range(len(ans1)):
		s += str(ans0[i])
		s += ' '
		s += str(ans1[i])
		s += ' '
	if s[len(s) - 1] == ' ':
		s = s[0:len(s) - 1]
	print(s)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(row, col) = ([], [])
	for i in range(k):
		(r, c) = map(int, input().split())
		(row.append(r), col.append(c))
	print(n - k, end=' ')
	(lr, lc, row, col) = ([], [], set(row), set(col))
	for i in range(1, n + 1):
		if i not in row:
			lr.append(i)
		if i not in col:
			lc.append(i)
	for i in range(n - k):
		if i == n - k - 1:
			print(lr[i], lc[i])
		else:
			print(lr[i], lc[i], end=' ')


def findNone(index, arr):
	for i in range(index + 1, len(arr)):
		if arr[i] == None:
			return i
	return -1
testCase = int(input(''))
for i in range(0, testCase):
	line = input('').split(' ')
	(N, K) = (int(line[0]), int(line[1]))
	r = [None] * N
	c = [None] * N
	for j in range(0, K):
		line1 = input('').split(' ')
		(r[int(line1[0]) - 1], c[int(line1[1]) - 1]) = (1, 1)
	ans = str(N - K) + ''
	(cI, cJ) = (findNone(-1, r), findNone(-1, c))
	while cI != -1:
		ans = ans + ' ' + str(cI + 1) + ' ' + str(cJ + 1)
		(cI, cJ) = (findNone(cI, r), findNone(cJ, c))
	print(ans)