def solve(N, AS, BS):
from collections import Counter
a_count = Counter(AS)
b_count = Counter(BS)
max_occur = 0
when_max = None
for i in range(N + 10):
t = a_count[i] + b_count[i]
if t > N:
return
if t > max_occur:
max_occur = t
when_max = i
ret = []
del a_count[when_max]
del b_count[when_max]
a_keys = list(a_count.keys())
b_keys = list(b_count.keys())
a_pointer = 0
b_pointer = 0
a_len = len(a_keys)
b_len = len(b_keys)
for _i in range(N - max_occur):
a = a_keys[a_pointer]
if a == when_max:
a_pointer = (a_pointer + 1) % a_len
a = a_keys[a_pointer]
c = a_count[a]
if c == 0:
del a_count[a]
a_pointer = (a_pointer + 1) % a_len
a = a_keys[a_pointer]
b = b_keys[b_pointer]
while a == b or b == when_max or b_count[b] == 0:
b_pointer = (b_pointer + 1) % b_len
b = b_keys[b_pointer]
ret.append((a, b))
a_count[a] -= 1
b_count[b] -= 1
for b in b_count:
for _i in range(b_count[b]):
ret.append((when_max, b))
for a in a_count:
for _i in range(a_count[a]):
ret.append((a, when_max))
ret.sort()
ret = [b for a, b in ret]
return ret