Read problems statements in Vietnamese  .

Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A.

The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}.

The brother needs your help in finding any such array B. Can you please find one such array for him?

Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2.

------ 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 an integer N denoting the length of the array A.
The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}.

------ Output ------ 

For each test case, print two lines.
The first line should contain the maximum possible Hamming distance that array B can have from array A.
The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{5}$
$The frequency of each integer in the array A will be at most 2.$

------ Subtasks ------ 

Subtask #1 (30 points): all elements in the array A are unique

Subtask #2 (30 points): 5 ≤ N ≤ 10^{5}

Subtask #3 (40 points): original constraints

----- Sample Input 1 ------ 
3

2

1 2

3

1 2 1

4

2 6 5 2
----- Sample Output 1 ------ 
2

2 1

2

2 1 1

4

6 2 2 5

Difficulty: MEDIUM_HARD

Solutions:
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	new_l = l[:]
	for i in range(n - 1):
		(l[i], l[i + 1]) = (l[i + 1], l[i])
	hd = n
	for i in range(n):
		if l[i] == new_l[i]:
			for j in range(n):
				if l[i] != l[j] and l[i] != new_l[j]:
					(l[i], l[j]) = (l[j], l[i])
					break
			else:
				hd -= 1
	print(hd)
	print(*l)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	z = [0] * n
	if n == 1:
		print(0)
		print(a[0])
	elif n == 2:
		if a[0] == a[1]:
			print(0)
			print(a[0], a[1])
		else:
			print(2)
			print(a[1], a[0])
	elif n == 3:
		if a[0] == a[1]:
			print(2)
			print(a[2], a[0], a[1])
		elif a[0] == a[2]:
			print(2)
			print(a[1], a[0], a[2])
		elif a[1] == a[2]:
			print(2)
			print(a[1], a[0], a[2])
		else:
			print(3)
			print(a[2], a[0], a[1])
	else:
		r = n % 4
		tt = n // 4
		if r == 3:
			if a[n - 3] == a[n - 2]:
				z[n - 1] = a[n - 3]
				z[n - 3] = a[n - 1]
			elif a[n - 3] == a[n - 1]:
				z[n - 3] = a[n - 2]
				z[n - 2] = a[n - 3]
			elif a[n - 1] == a[n - 2]:
				z[n - 2] = a[n - 3]
				z[n - 3] = a[n - 2]
			else:
				z[n - 3] = a[n - 1]
				z[n - 2] = a[n - 3]
				z[n - 1] = a[n - 2]
		for i in range(0, tt * 4 - 4 + 1, 4):
			if a[i + 2] == a[i + 3] or a[i] == a[i + 1]:
				z[i] = a[i + 3]
				z[i + 3] = a[i]
				z[i + 1] = a[i + 2]
				z[i + 2] = a[i + 1]
			elif a[i] == a[i + 2] or a[i] == a[i + 3] or a[i + 1] == a[i + 3] or (a[i + 1] == a[i + 2]):
				z[i] = a[i + 1]
				z[i + 1] = a[i]
				z[i + 2] = a[i + 3]
				z[i + 3] = a[i + 2]
			else:
				z[i] = a[i + 1]
				z[i + 1] = a[i]
				z[i + 2] = a[i + 3]
				z[i + 3] = a[i + 2]
		if z[n - 1] == 0 and z[n - 2] == 0:
			rmt = a[n - 2]
			smt = a[n - 1]
			z[n - 2] = z[0]
			z[n - 1] = z[1]
			z[0] = rmt
			z[1] = smt
		elif z[n - 1] == 0:
			for i in range(n):
				if a[n - 1] != z[i] and a[i] != a[n - 1]:
					z[n - 1] = z[i]
					z[i] = a[n - 1]
					break
		elif z[n - 2] == 0:
			for i in range(n):
				if a[n - 2] != z[i] and a[i] != a[n - 2]:
					z[n - 2] = z[i]
					z[i] = a[n - 2]
					break
		print(n)
		for i in range(n):
			print(z[i], end=' ')
		print()
	t = t - 1


t = int(input())
while t > 0:
	n = int(input())
	c = [0] * n
	b = list(map(int, input().split()))
	if n == 1:
		print(0)
		print(b[0])
	if n == 2:
		if b[0] == b[1]:
			print(0)
			print(b[0], b[1])
		else:
			print(2)
			print(b[1], b[0])
	if n == 3:
		if b[0] == b[1] or b[1] == b[2] or b[2] == b[0]:
			print(2)
			if b[0] == b[1]:
				print(b[2], b[0], b[1])
			elif b[1] == b[2]:
				print(b[1], b[0], b[2])
			elif b[0] == b[2]:
				print(b[1], b[0], b[2])
		else:
			print(3)
			print(b[2], b[0], b[1])
	if n >= 4:
		x = []
		print(n)
		for i in range(n):
			for j in range(i - 2, n):
				if c[j] == 0 and b[j] != b[i] and (j != i):
					if j >= 0:
						c[j] = b[i]
						break
		if c[n - 1] == 0:
			if c[n - 2] == 0:
				c[n - 1] = c[0]
				c[n - 2] = c[1]
				c[0] = b[n - 1]
				c[1] = b[n - 2]
			else:
				for i in range(n):
					if b[n - 1] != b[i] and c[i] != b[n - 1]:
						c[n - 1] = c[i]
						c[i] = b[n - 1]
						break
		if c[n - 1] != 0 and c[n - 2] == 0:
			for i in range(n):
				if b[n - 2] != b[i] and c[i] != b[n - 2]:
					c[n - 2] = c[i]
					c[i] = b[n - 2]
					break
		print(' '.join(map(str, c)))
	t = t - 1


test = int(input())
for _ in range(test):
	n = int(input())
	l = list(map(int, input().split()))
	nl = l[:]
	hammed = [0 for i in range(n)]
	cnt = 0
	for i in range(n - 1):
		if l[i] == nl[i]:
			(nl[i], nl[i + 1]) = (nl[i + 1], nl[i])
			if l[i] != nl[i]:
				cnt += 1
				hammed[i] = 1
			elif i + 2 < n:
				(nl[i], nl[i + 2]) = (nl[i + 2], nl[i])
				cnt += 1
				hammed[i] = 1
		else:
			cnt += 1
			hammed[i] = 1
	if l[-1] != nl[-1]:
		cnt += 1
		hammed[-1] = 1
	else:
		try:
			temp = hammed.index(1)
			if l[temp] != nl[-1] and nl[temp] != l[-1]:
				(nl[temp], nl[-1]) = (nl[-1], nl[temp])
				cnt += 1
			else:
				for i in range(temp + 1, n):
					if hammed[i] == 1 and nl[-1] != l[i] and (nl[i] != l[-1]):
						(nl[i], nl[-1]) = (nl[-1], nl[i])
						cnt += 1
						break
		except:
			pass
	for i in range(n - 1):
		if hammed[i] == 0:
			try:
				temp = hammed.index(1)
				if l[temp] != nl[i] and nl[temp] != l[i]:
					(nl[temp], nl[i]) = (nl[i], nl[temp])
					cnt += 1
				else:
					for j in range(temp + 1, n):
						if hammed[j] == 1 and nl[i] != l[j] and (nl[j] != l[i]):
							(nl[j], nl[i]) = (nl[i], nl[j])
							cnt += 1
							break
			except:
				pass
	print(cnt)
	print(*nl)


def swap(a, ind):
	(a[ind], a[ind - 2]) = (a[ind - 2], a[ind])
	(a[ind - 1], a[ind - 3]) = (a[ind - 3], a[ind - 1])
t = int(input())
for _ in range(t):
	hd = 0
	n = int(input())
	ar = list(map(int, input().split()))
	i = n - 1
	if n == 1:
		print(0)
		print(*ar)
	elif n == 2:
		if ar[0] == ar[1]:
			print(0)
			print(*ar)
		else:
			print(2)
			print(ar[1], ar[0])
	elif len(ar) == len(set(ar)):
		print(n)
		print(*ar[1:n], ar[0])
	elif n == 3:
		if ar[2] == ar[1] or ar[0] == ar[2]:
			(ar[0], ar[1]) = (ar[1], ar[0])
		elif ar[1] == ar[0]:
			(ar[1], ar[2]) = (ar[2], ar[1])
		print(2)
		print(*ar)
	else:
		tmp = ar[0]
		while i > 0:
			if ar[i] == ar[i - 1]:
				if i > 2:
					swap(ar, i)
					i = i - 4
					hd = hd + 4
				elif i == 2:
					(ar[2], ar[n - 1]) = (ar[n - 1], ar[2])
					(ar[1], ar[n - 2]) = (ar[n - 2], ar[1])
					(ar[0], ar[n - 3]) = (ar[n - 3], ar[0])
					hd = hd + 3
					i = 0
				elif i == 1:
					(ar[1], ar[n - 1]) = (ar[n - 1], ar[1])
					(ar[0], ar[n - 2]) = (ar[n - 2], ar[0])
					hd = hd + 2
					i = 0
			elif ar[i] != ar[i - 1]:
				(ar[i], ar[i - 1]) = (ar[i - 1], ar[i])
				hd = hd + 2
				i = i - 2
		if ar[0] == tmp:
			(ar[0], ar[n - 1]) = (ar[n - 1], ar[0])
			hd += 1
		print(n)
		print(*ar)


for t in range(int(input())):
	m = int(100000.0)
	n = int(input())
	ar = list(map(int, input().split()))
	freq = [0 for i in range(m)]
	idxs = [[] for i in range(m)]
	idx = 0
	for a in ar:
		freq[a - 1] += 1
		idxs[a - 1].append(idx)
		idx += 1
	counts = []
	for i in range(m):
		if freq[i] > 0:
			counts.append([freq[i], i])
	counts.sort(reverse=True)
	j = 0
	ans = 0
	while j < len(counts) and len(counts) > 1:
		if j == len(counts) - 1:
			if counts[j][0] == 2:
				temp = idxs[counts[j][1]][:]
				idxs[counts[j][1]] = idxs[counts[0][1]]
				idxs[counts[0][1]] = temp
				ans += 2
				j += 1
				continue
			else:
				temp = idxs[counts[j][1]][0]
				idxs[counts[j][1]][0] = idxs[counts[0][1]][0]
				idxs[counts[0][1]][0] = temp
				j += 1
				ans += 1
				continue
		if counts[j][0] == 2 and counts[j + 1][0] == 2:
			temp = idxs[counts[j][1]][:]
			idxs[counts[j][1]] = idxs[counts[j + 1][1]]
			idxs[counts[j + 1][1]] = temp
			j += 2
			ans += 4
			continue
		if counts[j][0] == 2 and counts[j + 1][0] == 1:
			if j + 2 < len(counts):
				temp = idxs[counts[j][1]][:]
				idxs[counts[j][1]][0] = idxs[counts[j + 1][1]][0]
				idxs[counts[j][1]][1] = idxs[counts[j + 2][1]][0]
				idxs[counts[j + 1][1]][0] = temp[0]
				idxs[counts[j + 2][1]][0] = temp[1]
				j += 3
				ans += 4
				continue
			else:
				temp = idxs[counts[j][1]][:]
				idxs[counts[j][1]][0] = idxs[counts[j + 1][1]][0]
				idxs[counts[j][1]][1] = idxs[counts[0][1]][0]
				idxs[counts[j + 1][1]][0] = temp[0]
				idxs[counts[0][1]][0] = temp[1]
				j += 2
				ans += 3
				continue
		if counts[j][0] == 1:
			temp = idxs[counts[j][1]][:]
			idxs[counts[j][1]] = idxs[counts[j + 1][1]]
			idxs[counts[j + 1][1]] = temp
			j += 2
			ans += 2
			continue
	br = [0 for i in range(n)]
	for i in range(m):
		idx = idxs[i]
		if len(idx) == 1:
			br[idx[0]] = i + 1
		if len(idx) == 2:
			br[idx[0]] = i + 1
			br[idx[1]] = i + 1
	ans = 0
	for (a, b) in zip(ar, br):
		if a != b:
			ans += 1
	print(ans)
	print(' '.join((str(x) for x in br)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = [None] * 100001
	s = [None, None]
	l = [None, None]
	cnt = 0
	b = 0
	for (i, x) in enumerate(a):
		if f[x] is not None:
			f[x] ^= 1
		else:
			cnt += 1
			if cnt == 3 and n > 3:
				b = 1
			f[x] = b
		j = f[x]
		if s[j] is None:
			s[j] = x
		else:
			a[l[j]] = x
		l[j] = i
	for i in (0, 1):
		if l[i] is not None:
			a[l[i]] = s[i]
	print(0 if cnt == 1 else cnt if n <= 3 else n)
	print(*a)


for i in range(int(input())):
	n = int(input())
	l = [int(s) for s in input().split()]
	if len(l) == 3:
		ans1 = 0
		ans = l[1:] + [l[0]]
		for w in range(3):
			if ans[w] != l[w]:
				ans1 += 1
		print(ans1)
		print(ans[0], ans[1], ans[2])
	elif len(l) == 2:
		ans = [l[1], l[0]]
		if ans == l:
			print(0)
			print(ans[0], ans[1])
		else:
			print(2)
			print(ans[0], ans[1])
	elif len(l) == 1:
		print(0)
		print(l[0])
	else:
		ans = l.copy()
		r = []
		s = 0
		if n % 2 * (n - 1) > 0:
			s = n % 2 * (n - 1)
		for j in range(0, n - 1, 2):
			if ans[j] != ans[j + 1]:
				a = ans[j + 1]
				ans[j + 1] = ans[j]
				ans[j] = a
			else:
				r += [j, j + 1]
		r1 = []
		for k in range(len(r)):
			if k % 2 == 0:
				if r[k] not in r1 and r[k] - 1 not in r1:
					b = ans[r[k] - 1]
					ans[r[k] - 1] = ans[r[k]]
					ans[r[k]] = b
					r1 += [r[k], r[k] - 1]
			elif r[k] not in r1 and (r[k] + 1) % n not in r1:
				d = ans[(r[k] + 1) % n]
				ans[(r[k] + 1) % n] = ans[r[k]]
				ans[r[k]] = d
				r1 += [r[k], (r[k] + 1) % n]
		if s != 0:
			c = ans[s]
			ans[s] = ans[2]
			ans[2] = c
		print(n)
		for v in ans:
			print(v, end=' ')
		print()


t = int(input())
while t > 0:
	n = int(input())
	inp = input()
	inp = inp.split(' ')
	l = list(inp)
	m = n // 2
	for i in range(n):
		j = i + 1
		while j < n and (inp[i] == l[j] or inp[j] == l[i]):
			j += 1
		if j != n:
			(l[i], l[j]) = (l[j], l[i])
	if l[n - 1] == inp[n - 1]:
		for o in range(n - 1):
			if inp[o] != l[n - 1] and l[o] != inp[n - 1]:
				(l[o], l[n - 1]) = (l[n - 1], l[o])
				break
	p = 0
	for i in range(n):
		if inp[i] != l[i]:
			p += 1
	print(p)
	for i in range(n - 1):
		print(int(l[i]), end=' ')
	print(l[n - 1])
	t -= 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	i = 0
	while i < n:
		while i < n and a[i] != b[i]:
			i += 1
		j = i + 1
		while j < n and b[j] == b[i]:
			j += 1
		if j < n:
			(b[i], b[j]) = (b[j], b[i])
		i += 1
	for i in range(n - 1, -1, -1):
		if b[i] == a[i]:
			j = i - 1
			while j >= 0 and (b[j] == b[i] or b[j] == a[i] or a[j] == b[i]):
				j -= 1
			if j >= 0:
				(b[j], b[i]) = (b[i], b[j])
		i += 1
	ans = 0
	for i in range(n):
		if a[i] != b[i]:
			ans += 1
	print(ans)
	print(*b)


tests = int(input())
results = []
for i in range(0, tests):
	n = int(input())
	numso = input().split(' ')
	nums = list(numso)
	n = len(nums)
	l = 0
	r = n - 1
	while l < r:
		if nums[l] != nums[r]:
			(nums[l], nums[r]) = (nums[r], nums[l])
			l += 1
			r -= 1
		elif l + 1 < r - 1:
			(nums[l], nums[l + 1]) = (nums[l + 1], nums[l])
			(nums[r], nums[r - 1]) = (nums[r - 1], nums[r])
			l += 2
			r -= 2
		elif l - 1 >= 0 and r + 1 <= n - 1:
			(nums[l], nums[l - 1]) = (nums[l - 1], nums[l])
			(nums[r], nums[r + 1]) = (nums[r + 1], nums[r])
			l += 2
			r -= 2
		else:
			break
	if n % 2 == 1 and n >= 3:
		m = int(n / 2)
		mv = nums[m]
		for p in range(0, n):
			if mv != nums[p] and mv != numso[p]:
				(nums[m], nums[p]) = (nums[p], nums[m])
				break
	dist = 0
	for p in range(0, n):
		if nums[p] != numso[p]:
			dist += 1
	results.append(dist)
	results.append(' '.join(nums))
for i in range(0, len(results)):
	if i % 2 == 1:
		print(results[i])
	else:
		ll = results[i]
		print(ll)


import random

def check(arr, li):
	count = 0
	for i in range(len(arr)):
		if arr[i] == li[i]:
			count += 1
	return count

def shuf(li):
	if len(li) > 3:
		arr = li[:]
		random.shuffle(arr)
		while True:
			random.shuffle(arr)
			if check(arr, li) == 0:
				break
		return arr
	elif len(li) == 1:
		return li
	elif len(li) == 2:
		if li[0] != li[1]:
			return li
		else:
			return li[::-1]
	elif len(li) == 3:
		if len(set(li)) == 3:
			return li
		else:
			return
t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	if len(li) > 3:
		ans = shuf(li)
		print(len(ans))
		print(*ans)
	elif len(li) == 1:
		print('0')
		print(*li)
	elif len(li) == 2:
		if li[0] != li[1]:
			print('2')
			print(li[1], li[0])
		else:
			print('0')
			print(*li)
	elif len(li) == 3:
		if li[0] == li[1] or li[1] == li[2]:
			print('2')
			print(li[2], li[1], li[0])
		elif li[0] == li[2]:
			print('2')
			print(li[1], li[2], li[0])
		else:
			print('3')
			print(li[2], li[0], li[1])


T = int(input())
for _testcase_ in range(T):
	N = int(input())
	A = [int(x) for x in input().split()]
	if len(A) <= 3:
		if len(A) == 1:
			B = [A[0]]
			print(0)
		if len(A) == 2:
			if A[0] == A[1]:
				B = [A[1], A[0]]
				print(0)
			else:
				B = [A[1], A[0]]
				print(2)
		if len(A) == 3:
			if len(set(A)) == 3:
				B = [A[1], A[2], A[0]]
				print(3)
			elif len(set(A)) == 2:
				print(2)
				single = min(A, key=lambda x: A.count(x))
				double = max(A, key=lambda x: A.count(x))
				singleI = A.index(single)
				if singleI == 0:
					B = [double, single, double]
				else:
					B = [single, double, double]
		print(' '.join((str(x) for x in B)))
		continue
	B = [A[-1]] + A[:-1]
	badValues = []
	badIndices = []
	for i in range(N):
		if A[i] == B[i]:
			badValues.append(B[i])
			badIndices.append(i)
	if len(badValues) > 1:
		badIndices = [badIndices[-1]] + badIndices[:-1]
		for i in range(len(badIndices)):
			badIndex = badIndices[i]
			badValue = badValues[i]
			B[badIndex] = badValue
	elif len(badValues) == 1:
		for newI in range(N):
			if A[newI] != badValues[0] and A[badIndices[0]] != B[newI]:
				(B[newI], B[badIndices[0]]) = (badValues[0], B[newI])
				break
	elif len(badValues) < 1:
		pass
	print(N)
	print(' '.join((str(x) for x in B)))
	continue


for q in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = list(set(a))
	c = [a[i] for i in range(n)]
	lb = len(b)
	flag = [0 for i in range(n)]
	count = 0
	count1 = 0
	if n == 1:
		print(0)
		for k in a:
			print(k, end=' ')
		print()
	elif n == lb:
		print(n)
		for k in range(1, n):
			print(a[k], end=' ')
		print(a[0])
	elif n == 2 and lb == 1:
		print(0)
		for k in a:
			print(k, end=' ')
		print()
	elif n == 3 and lb == 2:
		if a[0] == a[1]:
			print(2)
			print(a[0], end=' ')
			print(a[2], end=' ')
			print(a[1])
		if a[0] == a[2] or a[1] == a[2]:
			print(2)
			print(a[1], end=' ')
			print(a[0], end=' ')
			print(a[2])
	elif n == 4 and lb == 3 and (a[3] == a[2]):
		print(4)
		print(a[2], end=' ')
		print(a[3], end=' ')
		print(a[0], end=' ')
		print(a[1])
	else:
		print(n)
		i = 1
		while i < n:
			if i == n - 1 and flag[i - 1] == 1:
				break
			if i == n - 1 and flag[i - 1] == 0 and (c[i] != c[i - 1]):
				(c[i], c[i - 1]) = (c[i - 1], c[i])
				flag[i - 1] = 1
				flag[i] = 1
				break
			if c[i] == c[i - 1]:
				i += 1
			else:
				(c[i], c[i - 1]) = (c[i - 1], c[i])
				flag[i - 1] = 1
				flag[i] = 1
				i += 2
		for i in range(n):
			if c[i] != a[i]:
				count1 += 1
		if count1 == n:
			for d in c:
				print(d, end=' ')
			print()
		else:
			ind = []
			for i in range(n):
				if flag[i] == 0:
					count += 1
			if count % 2 == 0 and count > 0:
				for i in range(n):
					if flag[i] == 0:
						ind.append(i)
				l = len(ind)
				i = 1
				while i < l:
					(c[ind[i]], c[ind[i - 1]]) = (c[ind[i - 1]], c[ind[i]])
					flag[ind[i - 1]] = 1
					flag[ind[i]] = 1
					i += 2
			else:
				for i in range(n):
					if flag[i] == 0:
						ind.append(i)
				l = len(ind)
				if l == 1:
					for i in range(n):
						if c[ind[0]] != a[i] and c[ind[0]] != c[i]:
							(c[ind[0]], c[i]) = (c[i], c[ind[0]])
							flag[ind[0]] = 1
							break
				else:
					i = 1
					while i < l - 1:
						(c[ind[i]], c[ind[i - 1]]) = (c[ind[i - 1]], c[ind[i]])
						flag[ind[i - 1]] = 1
						flag[ind[i]] = 1
						i += 2
					for i in range(n):
						if c[ind[l - 1]] != a[i] and c[ind[l - 1]] != c[i]:
							(c[ind[l - 1]], c[i]) = (c[i], c[ind[l - 1]])
							flag[ind[l - 1]] = 1
							break
			for d in c:
				print(d, end=' ')
			print()


from operator import itemgetter

def get_tuples(A):
	d = {}
	for i in A:
		d[i] = d.get(i, 0) + 1
	return d

def get_bi_from(sorted_tup, ai):
	res = -1
	for (i, (k, v)) in enumerate(sorted_tup):
		if k == ai:
			continue
		else:
			res = k
			if v == 1:
				sorted_tup.pop(i)
			else:
				sorted_tup[i] = (k, v - 1)
			break
	return res

def find_swap(A, B, ind):
	n = len(A)
	t = B[ind]
	for i in range(n):
		if A[i] != t and A[ind] != B[i]:
			tmp = B[i]
			B[i] = t
			B[ind] = tmp
			return 1
	return 0
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	tup = get_tuples(A)
	sorted_tup = sorted(tup.items(), key=itemgetter(1), reverse=True)
	hammer_dist = len(A)
	B = [-1 for i in range(len(A))]
	for (i, ai) in enumerate(A):
		bi = get_bi_from(sorted_tup, ai)
		if bi == -1:
			hammer_dist -= 1
			bi = ai
		B[i] = bi
	if N >= 2:
		if A[-1] == B[-1]:
			r = find_swap(A, B, N - 1)
			hammer_dist += r
		if A[-2] == B[-2]:
			r = find_swap(A, B, N - 2)
			hammer_dist += r
	print(hammer_dist)
	print(' '.join(list(map(str, B))))


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split()
	s = [int(x) for x in s]
	if n == 1:
		print('0')
		print(s[0])
		continue
	elif n == 2 and s[0] == s[1]:
		print('0')
		print(s[0], s[1])
		continue
	elif n == 2:
		print('2')
		print(s[1], s[0])
		continue
	elif n == 3 and s[0] == s[1]:
		print('2')
		print(s[0], s[2], s[1])
		continue
	elif n == 3 and s[0] == s[2]:
		print('2')
		print(s[1], s[0], s[2])
		continue
	elif n == 3 and s[1] == s[2]:
		print('2')
		print(s[1], s[0], s[2])
		continue
	elif n == 3:
		print('3')
		print(s[1], s[2], s[0])
		continue
	j = 0
	count = 0
	temp = 0
	while j < n:
		if j + 1 < n:
			if s[j] != s[j + 1]:
				temp = s[j]
				s[j] = s[j + 1]
				s[j + 1] = temp
				count += 2
				j += 2
			elif j + 3 < n:
				temp = s[j]
				s[j] = s[j + 2]
				s[j + 2] = temp
				temp = s[j + 1]
				s[j + 1] = s[j + 3]
				s[j + 3] = temp
				count += 4
				j += 4
			elif j + 2 < n:
				temp = s[j]
				s[j] = s[j + 2]
				s[j + 2] = temp
				if s[j + 1] != s[0]:
					temp = s[j + 1]
					s[j + 1] = s[0]
					s[0] = temp
				else:
					temp = s[j + 1]
					s[j + 1] = s[1]
					s[1] = temp
				count += 3
				j += 3
			else:
				temp = s[j]
				s[j] = s[0]
				s[0] = temp
				temp = s[j + 1]
				s[j + 1] = s[1]
				s[1] = temp
				count += 2
				j += 2
		else:
			if s[j] != s[0]:
				temp = s[j]
				s[j] = s[0]
				s[0] = temp
			else:
				temp = s[j]
				s[j] = s[1]
				s[1] = temp
			count += 1
			j += 1
	print(count)
	for i in range(n):
		print(s[i], end='')
		print(' ', end='')
	print()


T = int(input())
for ai in range(T):
	N = int(input())
	inp = input()
	S = [int(m) for m in inp.split()]
	T = [int(m) for m in inp.split()]
	visit = []
	for i in range(N):
		if S[i] == T[i] and i < N - 1:
			try:
				if S[i] != S[i + 1] and S[i + 1] != T[i] and (S[i] != T[i + 1]):
					a = S[i]
					S[i] = S[i + 1]
					S[i + 1] = a
				elif S[i] != S[-1] and S[-1] != T[i] and (S[i] != T[-1]):
					a = S[i]
					S[i] = S[-1]
					S[-1] = a
				elif S[i] != S[i - 1] and S[i - 1] != T[i] and (S[i] != T[i - 1]):
					a = S[i]
					S[i] = S[i - 1]
					S[i - 1] = a
				elif S[i] != S[i - 2] and S[i - 2] != T[i] and (S[i] != T[i - 2]):
					a = S[i]
					S[i] = S[i - 2]
					S[i - 2] = a
			except IndexError:
				pass
		elif S[i] == T[i]:
			try:
				if S[i] != S[-2] and S[-2] != T[i] and (S[i] != T[-2]):
					a = S[i]
					S[i] = S[-2]
					S[-2] = a
				elif S[i] != S[-3] and S[-3] != T[i] and (S[i] != T[-3]):
					a = S[i]
					S[i] = S[-3]
					S[-3] = a
				elif S[i] != S[-4] and S[-4] != T[i] and (S[i] != T[-4]):
					a = S[i]
					S[i] = S[-4]
					S[-4] = a
			except IndexError:
				pass
	hd = 0
	for i in range(N):
		if S[i] != T[i]:
			hd += 1
	print(hd)
	print(' '.join(map(str, S)))