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

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Data structures


Solutions:
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	rows = set()
	cols = set()
	for i in range(k):
		(r, c) = map(int, input().split())
		rows.add(r)
		cols.add(c)
	res_rows = []
	res_cols = []
	for i in range(1, n + 1):
		if i not in rows:
			res_rows.append(i)
		if i not in cols:
			res_cols.append(i)
	print(min(len(res_rows), len(res_cols)), end=' ')
	for i in range(len(res_rows)):
		print(res_rows[i], res_cols[i], end=' ')
	print()


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())
for _ in range(t):
	(n, k) = map(int, input().split())
	row = [0] * (n + 1)
	col = [0] * (n + 1)
	for i in range(k):
		(a, b) = map(int, input().split())
		row[a] = col[b] = 1
	print(n - k, end=' ')
	i = 1
	j = 1
	while i <= n and j <= n:
		if row[i]:
			i += 1
		elif col[j]:
			j += 1
		else:
			print(i, j, end=' ')
			i += 1
			j += 1
	print()


import sys
from math import gcd, sqrt, log10, log2, floor, ceil
from collections import deque, defaultdict
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

def il():
	return [int(a) for a in input().strip().split()]

def ip():
	return input().strip()

def ii():
	return int(input())

def ot(in_p, *args, e=' '):
	if type(in_p) == list:
		print(*in_p, end=e)
	if type(in_p) == str or type(in_p) == int:
		print(in_p, end=e)
		for l in args:
			print(l, end=e)
	print()

def rg(*ag):
	if len(ag) == 3:
		return range(ag[0], ag[1], ag[2])
	elif len(ag) == 2:
		return range(ag[0], ag[1])
	elif len(ag) == 1:
		return range(ag[0])
	else:
		raise 'invalid input'

def ispal(i, j, st):
	while i <= j:
		if st[i] == st[j]:
			i = i + 1
			j = j - 1
		else:
			return False
	return True

class SegmentTree:

	def __init__(self, arr):
		self.narr = [-1]
		narr = [-1]
		for l in rg(1, 2 * len(arr) + 1 + 1):
			self.narr.append(-1)
		self.arr = arr

	def buildTree(self, i, j, l):
		if i == j:
			self.narr[l] = self.arr[i]
			return self.arr[i]
		mid = (i + j) // 2
		lt = self.buildTree(self.narr, self.arr, i, mid, 2 * l)
		ri = self.buildTree(self.narr, self.arr, mid + 1, j, 2 * l + 1)
		self.narr[l] = lt + ri
		return lt + ri

	def rangeUpdate(self, i, j, L, R, l, val):
		mid = (i + j) // 2
		if L <= i and j <= R:
			self.narr[l] += val
			return
		if L <= i:
			self.rangeUpdate(i, mid, L, R, 2 * l, val)
		if mid + 1 <= R:
			self.rangeUpdate(mid + 1, j, L, R, 2 * l + 1, val)

	def rangeQuery(self, i, j, L, R, l):
		if L <= i and j <= R:
			return self.narr[l]
		mid = (i + j) // 2
		ans = 0
		if L <= mid:
			ans += self.rangeQuery(i, mid, L, R, 2 * l)
		if mid + 1 <= R:
			ans += self.rangeQuery(mid + 1, j, L, R, 2 * l + 1)
		return ans
mod = 1000000000 + 7
for _ in range(ii()):
	(n, k) = il()
	r = set()
	c = set()
	for l in range(1, n + 1):
		r.add(l)
		c.add(l)
	for t in range(k):
		(rk, ck) = il()
		r.remove(rk)
		c.remove(ck)
	r = sorted(list(r))
	c = sorted(list(c))
	print(len(r), end=' ')
	for l in range(len(r)):
		print(r[l], c[l], end=' ')
	print()


from sys import stdin, stdout
import math
from collections import Counter
import functools
import time
import bisect

def tr():
	return range(int(line()))

def sm():
	return map(int, line().split())

def ln():
	return list(sm())

def nl():
	return int(line())

def ssm():
	return map(str, line().split())

def line():
	return stdin.readline().rstrip()

def b(x):
	return bin(x).replace('0b', '')

def o(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + '\n')

def osp(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + ' ')

def ol(x):
	stdout.write(' '.join(map(str, x)) + '\n')

def wline():
	return stdin.read().split()

def main():
	for _ in tr():
		(n, k) = sm()
		rows = set(range(1, n + 1))
		cols = set(range(1, n + 1))
		for i in range(k):
			(r, c) = sm()
			rows.remove(r)
			cols.remove(c)
		print(n - k, end=' ')
		while rows:
			print(rows.pop(), end=' ')
			print(cols.pop(), end=' ')
		print('')
main()


def index(lst, k, n):
	low = 0
	high = n - 1
	while low <= high:
		mid = (low + high) // 2
		if lst[mid] == k:
			return mid
		elif lst[mid] < k:
			low = mid + 1
		else:
			high = mid - 1
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	rows = [i for i in range(1, n + 1)]
	cols = [i for i in range(1, n + 1)]
	if k > 0:
		for t in range(k):
			(i, j) = map(int, input().split())
			rows.pop(index(rows, i, n - t))
			cols.pop(index(cols, j, n - t))
	print(n - k, end=' ')
	for (i, j) in zip(rows, cols):
		print(i, j, end=' ')
	print('\n', end='')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	row = set([x for x in range(1, n + 1)])
	col = row.copy()
	for i in range(k):
		(r, c) = map(int, input().split())
		row -= {r}
		col -= {c}
	r = len(row)
	res = str(r) + ' '
	row = list(row)
	col = list(col)
	row.sort()
	col.sort()
	for i in range(r):
		res += str(row[i]) + ' ' + str(col[i]) + ' '
	print(res)


T = int(input())
for i in range(T):
	(n, k) = map(int, input().split())
	row = set()
	col = set()
	for i in range(k):
		(r, c) = map(int, input().split())
		row.add(r)
		col.add(c)
	fullrow = [i for i in range(1, n + 1)]
	rows = set(fullrow) - row
	cols = set(fullrow) - col
	rows = sorted(list(rows))
	cols = sorted(list(cols))
	print(len(rows), end=' ')
	for i in range(len(rows)):
		print(rows[i], cols[i], end=' ')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	uc = set()
	ur = set()
	for i in range(k):
		(r, c) = map(int, input().split())
		ur.add(r)
		uc.add(c)
	g = [i for i in range(1, n + 1)]
	row = set(g) - ur
	col = set(g) - uc
	row = sorted(list(row))
	col = sorted(list(col))
	print(len(row), end=' ')
	for i in range(len(row)):
		print(row[i], col[i], end=' ')
	print()


for _ in range(int(input())):
	(N, K) = map(int, input().split())
	lst_r = []
	lst_c = []
	for i in range(K):
		(r, c) = map(int, input().split())
		lst_r.append(r)
		lst_c.append(c)
	setm = set((i for i in range(1, N + 1)))
	set_r = set(lst_r)
	set_c = set(lst_c)
	r = list(setm ^ set_r)
	s = list(setm ^ set_c)
	print(N - K, end=' ')
	for i in range(len(r)):
		print(r[i], s[i], end=' ')
	print('')


for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	a = []
	b = []
	nnn = list(range(1, n + 1))
	for i in range(k):
		(x, y) = map(int, input().split(' '))
		a.append(x)
		b.append(y)
	q = list(set(nnn) ^ set(a))
	w = list(set(nnn) ^ set(b))
	print(len(q), end=' ')
	for i in range(len(q)):
		print(q[i], w[i], end=' ')
	print('')


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) = list(map(int, input().split()))
	r = [0] * (n + 1)
	c = [0] * (n + 1)
	for i in range(k):
		(r1, c1) = list(map(int, input().split()))
		r[r1] = 1
		c[c1] = 1
	r2 = []
	c2 = []
	for i in range(1, n + 1):
		if r[i] == 0:
			r2.append(i)
		if c[i] == 0:
			c2.append(i)
	print(n - k, end=' ')
	for i in range(n - k):
		print(r2[i], c2[i], end=' ')
	print('')


from math import ceil
for i in range(int(input())):
	(n, k) = list(map(int, input().split()))
	data = [0] * (n + 1)
	memo = [0] * (n + 1)
	for j in range(k):
		(x, y) = map(int, input().split())
		data[x] = True
		memo[y] = True
	print(n - k, end=' ')
	k = 1
	for j in range(1, n + 1):
		if data[j] == False:
			for l in range(k, n + 1):
				if memo[l] == False:
					print(j, l, end=' ')
					data[j] = True
					memo[l] = True
					k = l + 1
					break
	print('')


test = int(input())
for _ in range(test):
	(n, k) = map(int, input().split())
	row = [0 for i in range(n + 1)]
	column = [0 for i in range(n + 1)]
	for i in range(k):
		(r, c) = map(int, input().split())
		row[r] = 1
		column[c] = 1
	print(n - k, end=' ')
	column1 = []
	row1 = []
	for i in range(1, n + 1):
		if row[i] == 0:
			row1.append(i)
		if column[i] == 0:
			column1.append(i)
	for i in range(n - k):
		print(row1[i], column1[i], end=' ')
	print()


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))


for t in range(int(input())):
	a = input().split()
	(n, k) = (int(a[0]), int(a[1]))
	row = column = set((i + 1 for i in range(n)))
	(x, y) = (set(), set())
	for i in range(k):
		a = input().split()
		x.add(int(a[0]))
		y.add(int(a[1]))
	row = list(row - x)
	column = list(column - y)
	row.sort()
	column.sort()
	print(n - k, end=' ')
	for i in range(n - k):
		print(row[i], column[i], end=' ')


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])


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	markr = [1] * n
	markc = [1] * n
	ans_list = []
	for _ in range(k):
		(a, b) = map(int, input().split())
		markr[a - 1] = 0
		markc[b - 1] = 0
	(row, col) = ([], [])
	for i in range(n):
		if markr[i] == 1:
			row.append(i + 1)
		if markc[i] == 1:
			col.append(i + 1)
	print(len(row), end=' ')
	for i in range(len(row)):
		print(row[i], col[i], end=' ')
	print()


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	a = [x for x in range(1, n + 1)]
	b = [x for x in range(1, n + 1)]
	for i in range(m):
		(x, y) = [int(x) for x in input().split()]
		a[x - 1] = 0
		b[y - 1] = 0
	ss = n - m
	print(ss, end=' ')
	i = 0
	j = 0
	while ss:
		while a[i] == 0:
			i += 1
		while b[j] == 0:
			j += 1
		print(a[i], b[j], end=' ')
		i += 1
		j += 1
		ss -= 1
	print()


import collections
t = input()
for _ in range(int(t)):
	(n, k) = map(int, input().split())
	pres_row = collections.defaultdict(lambda : 0)
	pres_col = collections.defaultdict(lambda : 0)
	for i in range(k):
		(r, c) = map(int, input().split())
		pres_row[r] = 1
		pres_col[c] = 1
	(rowlist, collist) = ([], [])
	for i in range(1, n + 1):
		if pres_row[i] == 0:
			rowlist.append(i)
		if pres_col[i] == 0:
			collist.append(i)
	print(len(rowlist), end=' ')
	for i in range(len(rowlist)):
		print(f'{rowlist[i]} {collist[i]}', end=' ')
	print('')


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))


from math import *

def main():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		print(n - k, end=' ')
		pos = []
		x = 1
		y = 1
		rows = [0 for i in range(n + 1)]
		cols = [0 for i in range(n + 1)]
		for i in range(k):
			(r, c) = map(int, input().split())
			pos.append((r, c))
			rows[r] = 1
			cols[c] = 1
		for i in range(n - k):
			rr = 0
			cc = 0
			for j in range(x, n + 1):
				if rows[j] == 0:
					rr = j
					rows[j] = 1
					x = j
					break
			for j in range(y, n + 1):
				if cols[j] == 0:
					cc = j
					cols[j] = 1
					y = j
					break
			print(rr, end=' ')
			print(cc, end=' ')
		print()
main()


import itertools
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	fr = {}
	fc = {}
	for i in range(1, n + 1):
		fr[i] = 1
		fc[i] = 1
	ocr = []
	occ = []
	for i in range(k):
		(a, b) = map(int, input().split())
		ocr.append(a)
		occ.append(b)
	for i in ocr:
		del fr[i]
	for i in occ:
		del fc[i]
	print(n - k, end=' ')
	for (i, j) in zip(fr, fc):
		print(i, j, end=' ')
	print()


t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	l3 = []
	l4 = []
	l1 = {}
	l2 = {}
	for i in range(n):
		l1[i + 1] = 1
	for i in range(n):
		l2[i + 1] = 1
	for i in range(k):
		(a, b) = map(int, input().split())
		if a in l1:
			l1[a] = 0
		if b in l2:
			l2[b] = 0
	print(n - k, end=' ')
	for i in l1:
		if l1[i] != 0:
			l3.append(i)
	for i in l2:
		if l2[i] != 0:
			l4.append(i)
	for i in range(n - k):
		print(l3[i], l4[i], end=' ')
	print()


for _ in range(int(input())):
	(N, K) = map(int, input().split())
	column = [0] * N
	row = [0] * N
	for i in range(K):
		(r, c) = map(int, input().split())
		column[c - 1] = 1
		row[r - 1] = 1
	column_left = []
	row_left = []
	for i in range(N):
		if column[i] == 0:
			column_left.append(i + 1)
		if row[i] == 0:
			row_left.append(i + 1)
	print(len(column_left), end=' ')
	for i in range(len(column_left)):
		print(row_left[i], column_left[i], end=' ')


import sys
from math import ceil, floor
RI = lambda : [int(x) for x in sys.stdin.readline().split()]
rw = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	(n, m) = RI()
	row = [False] * n
	col = [False] * n
	r = {}
	c = {}
	for i in range(n):
		r[i] = 1
	for i in range(n):
		c[i] = 1
	for i in range(m):
		(a, b) = RI()
		a -= 1
		b -= 1
		r.pop(a)
		c.pop(b)
	ans = []
	r = list(r.keys())
	c = list(c.keys())
	for i in range(n - m):
		ans.append(r[i] + 1)
		ans.append(c[i] + 1)
	print(len(ans) // 2, end=' ')
	for i in ans:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = n - k
	r = [0]
	c = [0]
	for i in range(n):
		r.append(0)
		c.append(0)
	for i in range(k):
		(y, z) = map(int, input().split())
		r[y] = 1
		c[z] = 1
	y = []
	z = []
	for i in range(1, n + 1):
		if r[i] == 0:
			y.append(i)
		if c[i] == 0:
			z.append(i)
	print(a, end=' ')
	for i in range(a):
		print(y[i], z[i], end=' ')
	print()


t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	k = int(l[1])
	row = [0 for i in range(n)]
	col = [0 for i in range(n)]
	for i in range(k):
		l = input().split()
		row[int(l[0]) - 1] = 1
		col[int(l[1]) - 1] = 1
	count = 0
	i = 0
	j = 0
	print(n - k, end=' ')
	while count < n - k:
		while i < n and row[i]:
			i += 1
		while j < n and col[j]:
			j += 1
		print(i + 1, end=' ')
		i += 1
		print(j + 1, end=' ')
		j += 1
		count += 1
	print()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s1 = set(range(n))
	s2 = set(range(n))
	for i in range(k):
		(r, c) = map(int, input().split())
		s1.remove(r - 1)
		s2.remove(c - 1)
	(l1, l2) = (list(s1), list(s2))
	print(n - k, end=' ')
	for i in range(len(l1)):
		print(l1[i] + 1, l2[i] + 1, end=' ')
	print()


t = int(input())
for i in range(t):
	result = []
	(n, k) = map(int, input().split())
	Rows = {i: False for i in range(n)}
	Columns = {i: False for i in range(n)}
	for i in range(k):
		(a, b) = map(int, input().split())
		Rows[a - 1] = True
		Columns[b - 1] = True
	result = []
	rookToPlaceInRow = [d for d in Rows if Rows[d] == False]
	rookToPlaceInColumn = [d for d in Columns if Columns[d] == False]
	result.append(n - k)
	for i in range(n - k):
		result.append(rookToPlaceInRow[i] + 1)
		result.append(rookToPlaceInColumn[i] + 1)
	for i in range(len(result)):
		print(result[i], end=' ')
	print(' ')


t = int(input())
for test in range(t):
	(n, k) = map(int, input().split())
	row = [False for i in range(n + 1)]
	col = [False for i in range(n + 1)]
	p = n - k
	print(p, end=' ')
	for i in range(k):
		(r, c) = map(int, input().split())
		row[r] = True
		col[c] = True
	(i, j, k) = (1, 1, 1)
	while k <= p:
		if row[i]:
			i += 1
		elif col[j]:
			j += 1
		else:
			print(i, j, end=' ')
			k += 1
			i += 1
			j += 1
	print()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	sr = set()
	sc = set()
	for i in range(k):
		(a, b) = map(int, input().split())
		sr.add(a)
		sc.add(b)
	ans = [n - k]
	a = 1
	b = 1
	for i in range(n - k):
		while True:
			if a not in sr:
				sr.add(a)
				break
			a += 1
		while True:
			if b not in sc:
				sc.add(b)
				break
			b += 1
		ans.append(a)
		ans.append(b)
	for i in ans:
		print(i, end=' ')


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)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l1 = [0] * n
	l2 = [0] * n
	for i in range(k):
		(a, b) = map(int, input().split())
		l1[a - 1] = 1
		l2[b - 1] = 1
	print(n - k, end=' ')
	p1 = 0
	p2 = 0
	for i in range(n - k):
		while l1[p1] != 0:
			p1 = p1 + 1
		while l2[p2] != 0:
			p2 = p2 + 1
		p1 += 1
		p2 += 1
		print(p1, p2, end=' ')
	print()


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)


import numpy as np
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	d = list()
	e = list()
	total_rows = []
	total_col = []
	for j in range(n):
		total_rows.append(j + 1)
		total_col.append(j + 1)
	rook_rows = []
	rook_columns = []
	for j in range(k):
		(e, f) = map(int, input().split())
		x = (e, f)
		rook_rows.append(e)
		rook_columns.append(f)
		d.append(x)
	rows_diff = np.setdiff1d(total_rows, rook_rows)
	col_diff = np.setdiff1d(total_col, rook_columns)
	if len(rows_diff) == len(col_diff):
		print(len(rows_diff), end=' ')
		for j in range(len(rows_diff)):
			print(rows_diff[j], end=' ')
			print(col_diff[j], end=' ')
	print('')


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)


def Diff(li1, li2):
	return list(set(li1) - set(li2))
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = []
	b = []
	for i in range(k):
		(r, c) = map(int, input().split())
		a.append(r)
		b.append(c)
	c = list(range(1, n + 1))
	d = list(range(1, n + 1))
	c = Diff(c, a)
	d = Diff(d, b)
	c.sort()
	d.sort()
	print(len(c), end=' ')
	for i in range(len(c)):
		print(c[i], d[i], end=' ')
	print()


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=' ')


t = int(input())
for i in range(0, t):
	(n, k) = map(int, input().split())
	row = [1] * n
	column = [1] * n
	for j in range(0, k):
		(a, b) = map(int, input().split())
		row[a - 1] = 0
		column[b - 1] = 0
	print(n - k, end=' ')
	r = 0
	c = 0
	for p in range(0, n - k):
		while row[r] == 0:
			r += 1
		while column[c] == 0:
			c += 1
		print(r + 1, c + 1, end=' ')
		row[r] = 0
		column[c] = 0


t = int(input())
for j in range(t):
	(n, k) = map(int, input().split())
	r = set()
	c = set()
	for i in range(n):
		r.add(i + 1)
		c.add(i + 1)
	for i in range(k):
		(rw, col) = map(int, input().split())
		r.remove(rw)
		c.remove(col)
	ll = len(r)
	print(ll, end=' ')
	for i in range(ll):
		r1 = r.pop()
		c1 = c.pop()
		print(r1, c1, end=' ')
	print()


test_cases = int(input())
while test_cases > 0:
	(row, col) = map(int, input().strip().split())
	nclis = []
	nrlis = []
	rlis = [0] * row
	clis = [0] * row
	if col == 0:
		print(row, end=' ')
		for i in range(1, row + 1):
			print(i, i, end=' ')
	else:
		while col > 0:
			(a, b) = map(int, input().strip().split())
			rlis[a - 1] = 1
			clis[b - 1] = 1
			col -= 1
		for i in range(row):
			if rlis[i] == 0:
				rlis[i] = 1
				nrlis.append(i + 1)
			if clis[i] == 0:
				clis[i] = 1
				nclis.append(i + 1)
		print(len(nrlis), end=' ')
		for i in range(len(nrlis)):
			print(nrlis[i], nclis[i], end=' ')
	test_cases -= 1


for t in range(int(input())):
	(n, k) = map(int, input().split())
	r = [0] * n
	c = [0] * n
	for i in range(k):
		(a, b) = map(int, input().split())
		r[a - 1] = 1
		c[b - 1] = 1
	print(n - k, end=' ')
	i = j = 0
	while i < n and j < n:
		if r[i] == 0 and c[j] == 0:
			print(i + 1, j + 1, end=' ')
			i += 1
			j += 1
		elif r[i] == 1:
			i += 1
		elif c[j] == 1:
			j += 1
	print()


a = int(input())
for i in range(a):
	k = set()
	l = set()
	c = 0
	(x, y) = map(int, input().split())
	for i in range(x):
		k.add(i + 1)
		l.add(i + 1)
	for i in range(y):
		(p, q) = map(int, input().split())
		k.remove(p)
		l.remove(q)
	m = ''
	m = m + str(x - y) + ' '
	k = list(k)
	l = list(l)
	for i in range(len(k)):
		m = m + str(k[i]) + ' ' + str(l[i]) + ' '
	print(m)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	R = []
	C = []
	for j in range(k):
		(r, c) = map(int, input().split())
		R.append(r)
		C.append(c)
	print(n - k, end=' ')
	K = list(range(1, n + 1))
	r = sorted(list(set(K) - set(R)))
	c = sorted(list(set(K) - set(C)))
	for (x, y) in zip(r, c):
		print(x, y, end=' ')
	print()


a = int(input())
for i in range(a):
	(b, c) = map(int, input().split())
	e = []
	f = []
	for j in range(c):
		(g, h) = map(int, input().split())
		e.append(g)
		f.append(h)
	k = list(range(1, b + 1))
	p = sorted(list(set(k) - set(e)))
	l = sorted(list(set(k) - set(f)))
	print(b - c, end=' ')
	for (x, y) in zip(p, l):
		print(x, y, end=' ')


try:
	t = int(input())
	for case in range(t):
		l = input().split()
		n = int(l[0])
		k = int(l[1])
		rows = []
		columns = []
		for rook in range(k):
			l = input().split()
			rows.append(int(l[0]))
			columns.append(int(l[1]))
		p = n - k
		rows.sort()
		columns.sort()
		finstring = ''
		finstring += str(p) + ' '
		rindex = 0
		cindex = 0
		i = 1
		j = 1
		rowsfree = False
		colsfree = False
		while i <= n:
			if rowsfree and colsfree:
				finstring += str(i) + ' ' + str(j) + ' '
				i += 1
				j += 1
			elif rowsfree:
				while cindex < k and columns[cindex] < j:
					cindex += 1
				if cindex == k:
					colsfree = True
				elif columns[cindex] == j:
					j += 1
				else:
					finstring += str(i) + ' ' + str(j) + ' '
					i += 1
					j += 1
			elif colsfree:
				while rindex < k and rows[rindex] < i:
					rindex += 1
				if rindex == k:
					rowsfree = True
				elif rows[rindex] > i:
					finstring += str(i) + ' ' + str(j) + ' '
					j += 1
					i += 1
				else:
					i += 1
			else:
				while rindex < k and rows[rindex] < i:
					rindex += 1
				if rindex == k:
					rowsfree = True
				elif rows[rindex] > i:
					while cindex < k and columns[cindex] < j:
						cindex += 1
					if cindex == k:
						colsfree = True
					elif columns[cindex] == j:
						j += 1
					else:
						finstring += str(i) + ' ' + str(j) + ' '
						i += 1
						j += 1
				else:
					i += 1
		print(finstring)
except EOFError:
	pass


for i in range(int(input())):
	(n, k) = map(int, input().strip().split())
	r = set()
	c = set()
	t = k
	for i in range(1, n + 1):
		r.add(i)
		c.add(i)
	for i in range(k):
		(a, b) = map(int, input().strip().split())
		r.remove(a)
		c.remove(b)
	print(n - k, end=' ')
	u = []
	v = []
	for i in r:
		u.append(i)
	for i in c:
		v.append(i)
	l = []
	for i in range(len(r)):
		l.append(u[i])
		l.append(v[i])
	for i in l:
		print(i, end=' ')
	print()


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)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	x = [1 for i in range(n)]
	y = [1 for i in range(n)]
	for i in range(k):
		(a, b) = map(int, input().split())
		x[a - 1] = 0
		y[b - 1] = 0
	print(n - k, end=' ')
	(j, l) = (0, 0)
	for i in range(n - k):
		while x[j] == 0:
			j += 1
		print(j + 1, end=' ')
		j += 1
		while y[l] == 0:
			l += 1
		print(l + 1, end=' ')
		l += 1
	print()


for x in range(int(input())):
	f = input().split()
	n = int(f[0])
	k = int(f[1])
	r = []
	c = []
	p = []
	ans = str(n - k) + ' '
	for q in range(k):
		s = input().split()
		r.append(int(s[0]))
		c.append(int(s[1]))
	for q in range(n):
		p.append(q + 1)
	fr = set(r) ^ set(p)
	fc = set(c) ^ set(p)
	fc = list(fc)
	fr = list(fr)
	for d in range(n - k):
		ans += str(fr[d]) + ' ' + str(fc[d]) + ' '
	print(ans)