Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation.

Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. 

Notes: 

$\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10).
If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$.

Input Format

The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows:

The first line contains a single integer denoting the value of $\mbox{K}$.
Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$.

Constraints

$1\leq Q\leq5$
$0\leq K\leq5\times10^5$
$0<A,B,C<16^{5\times10^4}$

Output Format

Print two lines of output for each query:

The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$.
The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. 

If no valid answer exists, you must instead print one line of output with the integer $-1$.

Note: The letters in Hexadecimal numbers must be in uppercase.

Sample Input
3
8
2B
9F
58
5
B9
40
5A
2
91
BE
A8

Sample Output
8
58
18
42
-1

Explanation

Query 0: 

In this query, $K=8$. 

Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed.

Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed.

$A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$

Query 1: 

In this query, $K=5$. 

Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. 

Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed.

$A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$

Query 2: 

There is no valid answer, so we print $-1$.

Difficulty: MEDIUM
Tags: Bit manipulation
Skill types: Bit manipulation

Starter code:

Solutions:
Q = int(input())
for _ in range(Q):
	k = int(input())
	A = bin(int(input(), 16))[2:]
	B = bin(int(input(), 16))[2:]
	C = bin(int(input(), 16))[2:]
	l = max(len(A), len(B), len(C))
	A = list('0' * (l - len(A)) + A)
	B = list('0' * (l - len(B)) + B)
	C = list('0' * (l - len(C)) + C)
	for i in range(l):
		if A[i] == B[i] == '0' and C[i] == '1':
			B[i] = '1'
			k -= 1
		if A[i] > C[i] or B[i] > C[i]:
			if A[i] == '1':
				A[i] = '0'
				k -= 1
			if B[i] == '1':
				B[i] = '0'
				k -= 1
	if k < 0:
		print(-1)
		continue
	for i in range(l):
		if A[i] == '1' and B[i] == '1' and (k > 0):
			A[i] = '0'
			k -= 1
		if A[i] == '1' and B[i] == '0' and (k > 1):
			A[i] = '0'
			B[i] = '1'
			k -= 2
	print('%X' % int(''.join(A), 2))
	print('%X' % int(''.join(B), 2))


import binascii
bitMap = {}
revBitMap = {}
for b in '0123456789ABCDEF':
	x = bin(int(b, 16))[2:]
	while len(x) < 4:
		x = '0' + x
	bitMap[b] = x
	revBitMap[x] = b

def toBit(inp):
	s = ''
	for i in inp:
		s += bitMap[i]
	return list(s)

def toHex(inp):
	inp = ''.join(inp)
	s = ''
	for i in range(0, len(inp), 4):
		s += revBitMap[inp[i:i + 4]]
		if s == '0':
			s = ''
	if s == '':
		s = '0'
	return s

def solve():
	K = int(input())
	A = toBit(input())
	B = toBit(input())
	C = toBit(input())
	mxlen = max(len(A), len(B), len(C))
	if len(A) < mxlen:
		A = list('0' * (mxlen - len(A))) + A
	if len(B) < mxlen:
		B = list('0' * (mxlen - len(B))) + B
	if len(C) < mxlen:
		C = list('0' * (mxlen - len(C))) + C
	for i in range(len(C)):
		if C[i] == '0':
			if A[i] == '1':
				A[i] = '0'
				K -= 1
			if B[i] == '1':
				B[i] = '0'
				K -= 1
		elif A[i] == '0' and B[i] == '0':
			B[i] = '1'
			K -= 1
	if K < 0:
		print('-1')
		return
	for i in range(len(C)):
		if C[i] == '1':
			if A[i] == '1' and B[i] == '1' and (K >= 1):
				A[i] = '0'
				K -= 1
			elif A[i] == '1' and B[i] == '0' and (K >= 2):
				A[i] = '0'
				B[i] = '1'
				K -= 2
	print(toHex(A))
	print(toHex(B))
for aa in range(int(input())):
	solve()


class Solution:

	def __init__(self):
		self.K = int(input())
		self.A = int(input(), 16)
		self.B = int(input(), 16)
		self.C = int(input(), 16)

	def calculate(self):
		changes = bin((self.A | self.B) ^ self.C).count('1')
		if changes > self.K:
			print(-1)
			return
		max_len = max(self.A.bit_length(), self.B.bit_length(), self.C.bit_length())
		a_str = '0' * (max_len - self.A.bit_length()) + '{:b}'.format(self.A)
		b_str = '0' * (max_len - self.B.bit_length()) + '{:b}'.format(self.B)
		c_str = '0' * (max_len - self.C.bit_length()) + '{:b}'.format(self.C)
		a = [int(i) for i in a_str]
		b = [int(i) for i in b_str]
		c = [int(i) for i in c_str]
		remaining_swaps = self.K
		for i in range(max_len):
			if a[i] | b[i] != c[i]:
				if c[i] == 1:
					b[i] = 1
					remaining_swaps -= 1
				else:
					if b[i] == 1:
						b[i] = 0
						remaining_swaps -= 1
					if a[i] == 1:
						a[i] = 0
						remaining_swaps -= 1
		if remaining_swaps < 0:
			print(-1)
			return
		if remaining_swaps == 0:
			print_hex(a, b)
			return
		for i in range(max_len):
			if a[i] == 1:
				if b[i] == 1:
					a[i] = 0
					remaining_swaps -= 1
				elif remaining_swaps >= 2:
					b[i] = 1
					a[i] = 0
					remaining_swaps -= 2
			if remaining_swaps == 0:
				break
		print_hex(a, b)
		return

def print_hex(a, b):
	print('{0:X}\n{1:X}'.format(int(''.join([str(i) for i in a]), 2), int(''.join([str(i) for i in b]), 2)))

def main():
	cases = int(input())
	for i in range(cases):
		my_object = Solution()
		my_object.calculate()

def get_int_list(in_str):
	return [int(i) for i in in_str.strip().split()]
main()


def eat():
	ret = bin(int('A' + input(), 16))[2:]
	ret = [int(_) for _ in ret]
	return ret[4:]

def prt(a):
	b = ''.join([str(_) for _ in a])
	print('%X' % int(b, 2))

def app(a, n):
	if len(a) == n:
		return
	a.reverse()
	while len(a) < n:
		a.append(0)
	a.reverse()
q = int(input())
for cc in range(q):
	k = int(input())
	a = eat()
	b = eat()
	c = eat()
	n = max(len(a), len(b), len(c))
	app(a, n)
	app(b, n)
	app(c, n)
	for i in range(len(a)):
		if a[i] | b[i] == c[i]:
			continue
		if c[i]:
			b[i] = 1
			k -= 1
		else:
			if a[i]:
				a[i] = 0
				k -= 1
			if b[i]:
				b[i] = 0
				k -= 1
	if k < 0:
		print(-1)
		continue
	for i in range(len(a)):
		if k == 0:
			break
		if c[i]:
			if a[i] and b[i]:
				a[i] = 0
				k -= 1
			if a[i] and (not b[i]) and (k >= 2):
				a[i] = 0
				b[i] = 1
				k -= 2
	prt(a)
	prt(b)


for _ in range(int(input())):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	m = max(len(a), len(b), len(c))
	a = list(a.zfill(m))
	b = list(b.zfill(m))
	c = list(c.zfill(m))
	for i in range(len(a)):
		if c[i] == '0':
			if a[i] == '1':
				a[i] = '0'
				k -= 1
			if b[i] == '1':
				b[i] = '0'
				k -= 1
		elif a[i] == '0' == b[i]:
			b[i] = '1'
			k -= 1
	for i in range(len(a)):
		if k > 0 and a[i] == '1' == b[i]:
			a[i] = '0'
			k -= 1
		if k > 1 and a[i] == '1' and (b[i] == '0'):
			a[i] = '0'
			b[i] = '1'
			k -= 2
	if k < 0:
		print(-1)
	else:
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())


for _ in range(int(input().strip())):
	K = int(input().strip())
	A = int(input().strip(), 16)
	B = int(input().strip(), 16)
	C = int(input().strip(), 16)
	biggest = max(max(A, B), C)
	fill_len = len(bin(biggest)) - 2
	__A = list(bin(A)[2:].zfill(fill_len))
	__B = list(bin(B)[2:].zfill(fill_len))
	__C = list(bin(C)[2:].zfill(fill_len))
	num_changes = 0
	extra_A = []
	for i in range(fill_len):
		if __C[i] == '0':
			if __A[i] == '1':
				__A[i] = '0'
				num_changes += 1
			if __B[i] == '1':
				__B[i] = '0'
				num_changes += 1
		elif __B[i] == '0' and __A[i] == '0':
			__B[i] = '1'
			num_changes += 1
		elif __B[i] == '1' and __A[i] == '1':
			extra_A.append((i, False))
		elif __B[i] == '0' and __A[i] == '1':
			extra_A.append((i, True))
	if num_changes > K:
		print(-1)
	else:
		if num_changes != K:
			for j in range(len(extra_A)):
				if extra_A[j][1]:
					if num_changes + 2 <= K:
						__A[extra_A[j][0]] = '0'
						__B[extra_A[j][0]] = '1'
						num_changes += 2
				else:
					__A[extra_A[j][0]] = '0'
					num_changes += 1
				if num_changes == K:
					break
		print(hex(int(''.join(__A), 2))[2:].upper())
		print(hex(int(''.join(__B), 2))[2:].upper())


t = int(input())
for i in range(t):
	count = 0
	k = int(input())
	a = bin(int(input(), 16))
	b = bin(int(input(), 16))
	c = input()
	num = len(c) * 4
	c = bin(int(c, 16))[2:].zfill(num)
	a = list(a[2:].zfill(num))
	b = list(b[2:].zfill(num))
	for j in range(num):
		if c[j] == '0' and (a[j] == '1' and b[j] == '1'):
			count += 2
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '0' and (not (a[j] == '0' and b[j] == '0')):
			count += 1
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '1' and a[j] == '0' and (b[j] == '0'):
			count += 1
			b[j] = '1'
	if count == k:
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())
	elif count > k:
		print(-1)
	else:
		for j in range(num):
			if a[j] == '1' and b[j] == '1' and (count <= k - 1):
				count += 1
				a[j] = '0'
			elif a[j] == '1' and b[j] == '0' and (count <= k - 2):
				count += 2
				a[j] = '0'
				b[j] = '1'
			if count == k:
				break
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())


t = int(input())
for i in range(t):
	count = 0
	k = int(input())
	a = bin(int(input(), 16))
	b = bin(int(input(), 16))
	c = input()
	num = len(c) * 4
	c = bin(int(c, 16))[2:].zfill(num)
	a = list(a[2:].zfill(num))
	b = list(b[2:].zfill(num))
	for j in range(num):
		if c[j] == '0' and (a[j] == '1' and b[j] == '1'):
			count += 2
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '0' and (not (a[j] == '0' and b[j] == '0')):
			count += 1
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '1' and a[j] == '0' and (b[j] == '0'):
			count += 1
			b[j] = '1'
	if count == k:
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())
	elif count > k:
		print(-1)
	else:
		for j in range(num):
			if a[j] == '1' and b[j] == '1' and (count <= k - 1):
				count += 1
				a[j] = '0'
			elif a[j] == '1' and b[j] == '0' and (count <= k - 2):
				count += 2
				a[j] = '0'
				b[j] = '1'
			if count == k:
				break
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())


q = int(input())

def swap(x):
	x1 = x & 1
	x2 = (x & 2) >> 1
	x3 = (x & 4) >> 2
	x4 = (x & 8) >> 3
	return x1 << 3 | x2 << 2 | x3 << 1 | x4
for _ in range(q):
	k = int(input())
	a = input()
	b = input()
	c = input()
	max_len = max(len(a), len(b), len(c))
	if len(a) < max_len:
		x = '0' * (max_len - len(a))
		a = x + a
	if len(b) < max_len:
		x = '0' * (max_len - len(b))
		b = x + b
	if len(c) < max_len:
		x = '0' * (max_len - len(c))
		c = x + c
	a_ans = ''
	b_ans = ''
	for i in range(max_len):
		a_i = int(a[i], 16)
		b_i = int(b[i], 16)
		c_i = int(c[i], 16)
		o_a_i = a_i
		o_b_i = b_i
		o_c_i = c_i
		for j in range(3, -1, -1):
			a_x = a_i >> j & 1
			b_x = b_i >> j & 1
			c_x = c_i >> j & 1
			if a_x | b_x != c_x:
				if c_x == 0:
					if a_x == 1:
						o_a_i = o_a_i ^ 1 << j
						k -= 1
					if b_x == 1:
						o_b_i = o_b_i ^ 1 << j
						k -= 1
				else:
					o_b_i = o_b_i | 1 << j
					k -= 1
		a_ans += hex(o_a_i)[2:]
		b_ans += hex(o_b_i)[2:]
	if k > 0:
		a = a_ans[:]
		b = b_ans[:]
		a_ans = ''
		b_ans = ''
		for i in range(max_len):
			a_i = int(a[i], 16)
			b_i = int(b[i], 16)
			c_i = int(c[i], 16)
			o_a_i = a_i
			o_b_i = b_i
			o_c_i = c_i
			for j in range(3, -1, -1):
				a_x = a_i >> j & 1
				b_x = b_i >> j & 1
				c_x = c_i >> j & 1
				if c_x == 1:
					if a_x == 1 and b_x == 1:
						if k > 0:
							o_a_i = o_a_i ^ 1 << j
							k -= 1
					elif b_x == 0:
						if k > 1:
							o_a_i = o_a_i ^ 1 << j
							o_b_i = o_b_i | 1 << j
							k -= 2
			a_ans += hex(o_a_i)[2:]
			b_ans += hex(o_b_i)[2:]
	if k < 0:
		print(-1)
	else:
		a_ans = a_ans.lstrip('0')
		if a_ans == '':
			a_ans = '0'
		b_ans = b_ans.lstrip('0')
		if b_ans == '':
			b_ans = '0'
		print(a_ans.upper())
		print(b_ans.upper())


def one_count(s):
	return bin(s).count('1')
nq = int(input().strip())
for i in range(nq):
	k = int(input().strip())
	(a, b, c) = [int(input().strip(), 16) for i in range(3)]
	t1_mask = (a ^ b) & ~c
	t2_mask = a & b & ~c
	t3_mask = ~(a | b) & c
	c = [one_count(x) for x in [t1_mask, t2_mask, t3_mask]]
	c[1] *= 2
	nb = sum(c)
	if nb > k:
		print(-1)
		continue
	t1_mask |= t2_mask
	a &= ~t1_mask
	b &= ~t1_mask
	b |= t3_mask
	rb = k - nb
	al = list(bin(a))[2:]
	bl = list(bin(b))[2:]
	diff = len(al) - len(bl)
	if diff > 0:
		bl = ['0'] * diff + bl
	else:
		al = ['0'] * -diff + al
	for i in range(len(al)):
		if rb >= 1 and al[i] == '1' and (bl[i] == '1'):
			al[i] = '0'
			rb -= 1
		elif rb >= 2 and al[i] == '1' and (bl[i] == '0'):
			al[i] = '0'
			bl[i] = '1'
			rb -= 2
	print(hex(int(''.join(al), 2))[2:].upper())
	print(hex(int(''.join(bl), 2))[2:].upper())


def hex_to_bin(st):
	res = ''
	for i in st:
		if i == '0':
			res += '0000'
		elif i == '1':
			res += '0001'
		elif i == '2':
			res += '0010'
		elif i == '3':
			res += '0011'
		elif i == '4':
			res += '0100'
		elif i == '5':
			res += '0101'
		elif i == '6':
			res += '0110'
		elif i == '7':
			res += '0111'
		elif i == '8':
			res += '1000'
		elif i == '9':
			res += '1001'
		elif i == 'A':
			res += '1010'
		elif i == 'B':
			res += '1011'
		elif i == 'C':
			res += '1100'
		elif i == 'D':
			res += '1101'
		elif i == 'E':
			res += '1110'
		else:
			res += '1111'
	return res

def bin_to_hex(st):
	res = ''
	for i in range(0, len(st), 4):
		if st[i:i + 4] == '0000':
			res += '0'
		elif st[i:i + 4] == '0001':
			res += '1'
		elif st[i:i + 4] == '0010':
			res += '2'
		elif st[i:i + 4] == '0011':
			res += '3'
		elif st[i:i + 4] == '0100':
			res += '4'
		elif st[i:i + 4] == '0101':
			res += '5'
		elif st[i:i + 4] == '0110':
			res += '6'
		elif st[i:i + 4] == '0111':
			res += '7'
		elif st[i:i + 4] == '1000':
			res += '8'
		elif st[i:i + 4] == '1001':
			res += '9'
		elif st[i:i + 4] == '1010':
			res += 'A'
		elif st[i:i + 4] == '1011':
			res += 'B'
		elif st[i:i + 4] == '1100':
			res += 'C'
		elif st[i:i + 4] == '1101':
			res += 'D'
		elif st[i:i + 4] == '1110':
			res += 'E'
		else:
			res += 'F'
	i = 0
	while res[i] == '0':
		i += 1
		if i == len(res):
			return '0'
	return res[i:]

def fun(a, b, c, n, maximum):
	required = 0
	a_copy = [i for i in a]
	b_copy = [i for i in b]
	for i in range(n):
		if c[i] == '0' and a[i] == '1' and (b[i] == '1'):
			required += 2
			a_copy[i] = '0'
			b_copy[i] = '0'
		elif c[i] == '0' and (a[i] == '1' or b[i] == '1'):
			required += 1
			if a[i] == '1':
				a_copy[i] = '0'
			else:
				b_copy[i] = '0'
		elif c[i] == '1' and a[i] == '0' and (b[i] == '0'):
			required += 1
			b_copy[i] = '1'
		if required > maximum:
			break
	if required <= maximum:
		remaining = maximum - required
		for i in range(n):
			if remaining == 0:
				break
			if c[i] == '1' and a[i] == '1':
				if b[i] == '1':
					a_copy[i] = '0'
					remaining -= 1
				elif b[i] == '0' and remaining >= 2:
					a_copy[i] = '0'
					b_copy[i] = '1'
					remaining -= 2
		return [True, ''.join(map(str, a_copy)), ''.join(map(str, b_copy))]
	else:
		return [False]
q = int(input())
while q != 0:
	q -= 1
	k = int(input())
	a = input()
	b = input()
	c = input()
	a_bin = hex_to_bin(a)
	b_bin = hex_to_bin(b)
	c_bin = hex_to_bin(c)
	max_length = max([len(a_bin), len(b_bin), len(c_bin)])
	a_bin = '0' * (max_length - len(a_bin)) + a_bin
	b_bin = '0' * (max_length - len(b_bin)) + b_bin
	c_bin = '0' * (max_length - len(c_bin)) + c_bin
	res = fun(a_bin, b_bin, c_bin, max_length, k)
	if res[0] == False:
		print('-1')
	else:
		print(bin_to_hex(res[1]))
		print(bin_to_hex(res[2]))


queries = int(input())

def convert_hex_to_bin(hexstring):
	return bin(int(hexstring, 16))[2:]

def convert_bin_to_hex(binstring):
	return hex(int(binstring, 2))[2:]

def solve(k, a, b, c):
	edits = 0
	a = list(a)
	b = list(b)
	c = list(c)
	for (i, (ab, bb, cb)) in enumerate(zip(a, b, c)):
		if cb == '1':
			if ab == '0' and bb == '0':
				b[i] = '1'
				edits += 1
		else:
			if ab == '1':
				a[i] = '0'
				edits += 1
			if bb == '1':
				b[i] = '0'
				edits += 1
	possible = edits <= k
	if edits < k:
		for (i, (ab, bb, cb)) in enumerate(zip(a, b, c)):
			if cb == '1' and ab == '1' and (bb == '1'):
				a[i] = '0'
				edits += 1
			elif cb == '1' and ab == '1' and (bb == '0'):
				if k - edits >= 2:
					a[i] = '0'
					b[i] = '1'
					edits += 2
			if edits == k:
				break
	a = ''.join(a)
	b = ''.join(b)
	c = ''.join(c)
	return (possible, a, b)
for q in range(queries):
	k = int(input())
	ah = input()
	bh = input()
	ch = input()
	a = convert_hex_to_bin(ah)
	b = convert_hex_to_bin(bh)
	c = convert_hex_to_bin(ch)
	bits = max(len(a), len(b), len(c))
	a = a.zfill(bits)
	b = b.zfill(bits)
	c = c.zfill(bits)
	(possible, ap, bp) = solve(k, a, b, c)
	if not possible:
		print('-1')
	else:
		print(convert_bin_to_hex(ap).upper())
		print(convert_bin_to_hex(bp).upper())


n = int(input().strip())

def numbits(i):
	return bin(i).count('1')
for _ in range(n):
	k = int(input().strip())
	a = int(input().strip(), 16)
	b = int(input().strip(), 16)
	c = int(input().strip(), 16)
	wrongbits = c ^ (a | b)
	turnoff_a = wrongbits & a
	turnoff_b = wrongbits & b
	turnon = wrongbits & c
	k -= numbits(turnoff_a) + numbits(turnoff_b) + numbits(turnon)
	if k >= 0:
		a -= turnoff_a
		b -= turnoff_b
		b += turnon
		tmp = 0
		while k > 0 and a > 0:
			highest_a = 1 << a.bit_length() - 1
			if b & highest_a:
				a -= highest_a
				k -= 1
			elif k >= 2:
				a -= highest_a
				b += highest_a
				k -= 2
			else:
				a -= highest_a
				tmp += highest_a
		a += tmp
		print('%X' % a)
		print('%X' % b)
	else:
		print(-1)


for _ in range(int(input())):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	la = len(a)
	lb = len(b)
	lc = len(c)
	lm = max(la, lb, lc)
	a = '0' * (lm - la) + a
	b = '0' * (lm - lb) + b
	c = '0' * (lm - lc) + c
	a = [ord(a[i]) - ord('0') for i in range(lm)]
	b = [ord(b[i]) - ord('0') for i in range(lm)]
	c = [ord(c[i]) - ord('0') for i in range(lm)]
	o = 0
	for i in range(0, lm):
		if o > k:
			break
		if a[i] | b[i] != c[i]:
			if c[i] == 0:
				if a[i] == 1:
					a[i] = 0
					o += 1
				if b[i] == 1:
					b[i] = 0
					o += 1
			else:
				b[i] = 1
				o += 1
	if o > k:
		print(-1)
	else:
		for i in range(lm):
			if o >= k:
				break
			if a[i] == 1 and b[i] == 1:
				a[i] = 0
				o += 1
			elif a[i] == 1 and b[i] == 0 and (k - o >= 2):
				a[i] = 0
				b[i] = 1
				o += 2
		p = 0
		while p + 1 < lm and a[p] == 0:
			p += 1
		q = 0
		while q + 1 < lm and b[q] == 0:
			q += 1
		for i in range(lm):
			a[i] = chr(a[i] + 48)
			b[i] = chr(b[i] + 48)
		a = hex(int(''.join(a[p:]), 2))[2:]
		b = hex(int(''.join(b[q:]), 2))[2:]
		a = a.upper()
		b = b.upper()
		print(a)
		print(b)


Q = int(input())
for __ in range(Q):
	K = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	maxlength = max(len(a), len(b), len(c))
	a = list(map(int, a.rjust(maxlength, '0')))
	b = list(map(int, b.rjust(maxlength, '0')))
	c = list(map(int, c.rjust(maxlength, '0')))
	for i in range(maxlength):
		if c[i]:
			if a[i] or b[i]:
				pass
			else:
				b[i] = 1
				K -= 1
		else:
			if a[i]:
				a[i] = 0
				K -= 1
			if b[i]:
				b[i] = 0
				K -= 1
		if K < 0:
			break
	if K < 0:
		print(-1)
		continue
	if K == 0:
		print(hex(int(''.join([str(x) for x in a]), 2))[2:].upper())
		print(hex(int(''.join([str(x) for x in b]), 2))[2:].upper())
		continue
	for i in range(maxlength):
		if c[i]:
			if a[i] and b[i] and (K > 0):
				a[i] = 0
				K -= 1
			elif a[i] and K > 1:
				a[i] = 0
				b[i] = 1
				K -= 2
		if K == 0:
			break
	print(hex(int(''.join([str(x) for x in a]), 2))[2:].upper())
	print(hex(int(''.join([str(x) for x in b]), 2))[2:].upper())


c = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}
d = {}
for i in c:
	d[c[i]] = i
for i in range(int(input())):
	k = int(input())
	s1 = ''
	for j in input():
		s1 += c[j]
	s2 = ''
	for j in input():
		s2 += c[j]
	s3 = ''
	for j in input():
		s3 += c[j]
	m = max(len(s1), len(s2), len(s3))
	s1 = list('0' * (m - len(s1)) + s1)
	s2 = list('0' * (m - len(s2)) + s2)
	s3 = list('0' * (m - len(s3)) + s3)
	for i in range(m):
		if s3[i] == '0':
			if s1[i] == '1':
				k -= 1
				s1[i] = '0'
			if s2[i] == '1':
				k -= 1
				s2[i] = '0'
		elif s1[i] == '0' and s2[i] == '0':
			k -= 1
			s2[i] = '1'
	for i in range(m):
		if k == 0:
			break
		if s3[i] == '1' and s1[i] == '1' and (s2[i] == '1'):
			s1[i] = '0'
			k -= 1
		if s3[i] == '1' and s1[i] == '1' and (s2[i] == '0') and (k >= 2):
			k -= 2
			s1[i] = '0'
			s2[i] = '1'
	if k < 0:
		print(-1)
	else:
		s = ''
		k = -1
		for i in range(len(s1) // 4):
			s += d[''.join(s1[4 * i:4 * i + 4])]
		for i in range(len(s)):
			if s[i] != '0':
				k = 0
				print(s[i:])
				break
		if k == -1:
			print(0)
		k = -1
		s = ''
		for i in range(len(s2) // 4):
			s += d[''.join(s2[4 * i:4 * i + 4])]
		for i in range(len(s)):
			if s[i] != '0':
				k = 0
				print(s[i:])
				break
		if k == -1:
			print(0)


def leftpad(s, n):
	padlen = n - len(s)
	return '0' * padlen + s

def solve(k, a, b, c):
	changed = 0
	ap = ''
	bp = ''
	for (x, y, z) in zip(a, b, c):
		x = int(x, 16)
		y = int(y, 16)
		z = int(z, 16)
		p = 1
		while p < 16:
			if z & p == 0:
				if x & p:
					x ^= p
					changed += 1
				if y & p:
					y ^= p
					changed += 1
			elif x & p == 0 and y & p == 0:
				y ^= p
				changed += 1
			p <<= 1
		ap += hex(x)[2:]
		bp += hex(y)[2:]
	app = ''
	bpp = ''
	for (x, y, z) in zip(ap, bp, c):
		x = int(x, 16)
		y = int(y, 16)
		z = int(z, 16)
		p = 8
		while p > 0:
			if changed + 2 <= k and x & p and (not y & p) and z & p:
				x ^= p
				y ^= p
				changed += 2
			elif changed + 1 <= k and x & p and y & p and z & p:
				x ^= p
				changed += 1
			p >>= 1
		app += hex(x)[2:]
		bpp += hex(y)[2:]
	if changed <= k:
		app = app.lstrip('0')
		if not app:
			app = '0'
		bpp = bpp.lstrip('0')
		if not bpp:
			bpp = '0'
		print(app.upper())
		print(bpp.upper())
	else:
		print(-1)
q = int(input())
for _ in range(q):
	k = int(input())
	a = input()
	b = input()
	c = input()
	n = max(len(a), len(b), len(c))
	solve(k, leftpad(a, n), leftpad(b, n), leftpad(c, n))


from operator import add
q = int(input())
for i in range(q):
	k = int(input())
	a = int(input(), 16)
	a = [int(x) for x in bin(a)[2:]]
	b = int(input(), 16)
	b = [int(x) for x in bin(b)[2:]]
	c = int(input(), 16)
	c = [int(x) for x in bin(c)[2:]]
	mlen = max([len(a), len(b), len(c)])
	a = [0] * (mlen - len(a)) + a
	b = [0] * (mlen - len(b)) + b
	c = [0] * (mlen - len(c)) + c
	ab = list(map(add, a, b))
	maybe = []
	for j in range(len(ab)):
		if k < 0:
			k = -1
			break
		elif c[j] == 0 and ab[j] == 1:
			if a[j] == 1:
				a[j] = 0
			else:
				b[j] = 0
			k -= 1
		elif c[j] == 1 and ab[j] == 0:
			b[j] = 1
			k -= 1
		elif c[j] == 0 and ab[j] == 2:
			b[j] = 0
			a[j] = 0
			k -= 2
		elif c[j] == 1:
			if a[j] == 1 and b[j] == 1:
				maybe.append((j, 1))
			elif a[j] == 1:
				maybe.append((j, 2))
	if k == -1:
		print(k)
	elif k == 0:
		print(hex(int(''.join(list(map(str, a))), 2))[2:].upper())
		print(hex(int(''.join(list(map(str, b))), 2))[2:].upper())
	else:
		for (x, y) in maybe:
			if k < 1:
				break
			if k >= y:
				a[x] = 0
				k -= 1
				if y == 2:
					b[x] = 1
					k -= 1
		print(hex(int(''.join(list(map(str, a))), 2))[2:].upper())
		print(hex(int(''.join(list(map(str, b))), 2))[2:].upper())


from sys import stderr
Q = int(input())
for _ in range(Q):
	K = int(input())
	A = int(input(), 16)
	B = int(input(), 16)
	C = int(input(), 16)
	badbits = (A | B) ^ C
	badonesC = C & badbits
	badonesA = A & (badbits ^ badonesC)
	badonesB = B & (badbits ^ badonesC)
	if badonesC:
		n = bin(badonesC).count('1')
		K -= n
		B |= badonesC
	if badonesB:
		n = bin(badonesB).count('1')
		K -= n
		B ^= badonesB
	if badonesA:
		n = bin(badonesA).count('1')
		K -= n
		A ^= badonesA
	while K > 0:
		superfluous = (A & B).bit_length()
		swappable = (A & (A ^ B)).bit_length()
		if swappable > superfluous and K >= 2:
			bit = 1 << swappable >> 1
			A ^= bit
			B ^= bit
			K -= 2
		elif superfluous > 0:
			bit = 1 << superfluous >> 1
			A ^= bit
			K -= 1
		else:
			break
	print(-1 if K < 0 else '%X\n%X' % (A, B))


for _ in range(int(input().strip())):
	num_changes = 0
	(K, A, B, C) = (int(input().strip()), list('{0:b}'.format(int(input(), 16))), list('{0:b}'.format(int(input(), 16))), list('{0:b}'.format(int(input(), 16))))
	max_length = max(len(A), len(B), len(C))
	(A, B, C) = (['0'] * (max_length - len(A)) + A, ['0'] * (max_length - len(B)) + B, ['0'] * (max_length - len(C)) + C)
	for i in range(max_length):
		if C[i] == '1':
			if B[i] == '0' and A[i] == '0':
				if num_changes + 1 > K:
					num_changes += 1
					break
				B[i] = '1'
				num_changes += 1
		elif B[i] == '1' and A[i] == '1':
			if num_changes + 2 > K:
				num_changes += 2
				break
			A[i] = '0'
			B[i] = '0'
			num_changes += 2
		elif A[i] == '1':
			if num_changes + 1 > K:
				num_changes += 1
				break
			A[i] = '0'
			num_changes += 1
		elif B[i] == '1':
			if num_changes + 1 > K:
				num_changes += 1
				break
			B[i] = '0'
			num_changes += 1
	if num_changes > K:
		print(-1)
	else:
		for i in range(max_length):
			if C[i] == '1':
				if B[i] == '1' and A[i] == '1':
					if num_changes + 1 > K:
						break
					A[i] = '0'
					num_changes += 1
				elif A[i] == '1':
					if num_changes + 2 > K:
						continue
					A[i] = '0'
					B[i] = '1'
					num_changes += 2
		print('{0:X}'.format(int(''.join(A), 2)))
		print('{0:X}'.format(int(''.join(B), 2)))


q = input()
for i in range(int(q)):
	k = int(input())
	A = ''
	for e in input():
		A += bin(int(e, 16))[2:].zfill(4)
	B = ''
	for e in input():
		B += bin(int(e, 16))[2:].zfill(4)
	C = ''
	for e in input():
		C += bin(int(e, 16))[2:].zfill(4)
	biggest = max(len(A), len(B), len(C))
	A = list(A.zfill(biggest))
	B = list(B.zfill(biggest))
	C = list(C.zfill(biggest))
	assert len(A) == len(B) == len(C)
	for i in range(len(A)):
		if (A[i] == '1' and B[i] == '0') and C[i] == '0':
			A[i] = '0'
			k -= 1
		elif (A[i] == '0' and B[i] == '1') and C[i] == '0':
			B[i] = '0'
			k -= 1
		elif (A[i] == '1' and B[i] == '1') and C[i] == '0':
			A[i] = '0'
			B[i] = '0'
			k -= 2
		elif (A[i] == '0' and B[i] == '0') and C[i] == '1':
			B[i] = '1'
			k -= 1
	if k < 0:
		print(-1)
		continue
	if k > 0:
		for i in range(len(A)):
			if C[i] == '1' and B[i] == '1' and (A[i] == '1') and (k > 0):
				A[i] = '0'
				k -= 1
			elif C[i] == '1' and B[i] == '0' and (A[i] == '1') and (k > 1):
				B[i] = '1'
				A[i] = '0'
				k -= 2
			if k == 0:
				break
	at = int(''.join(A), 2)
	bt = int(''.join(B), 2)
	ct = int(''.join(B), 2)
	assert at or bt == ct
	print('%X' % int(''.join(A), 2))
	print('%X' % int(''.join(B), 2))


Q = int(input().strip())
for i in range(Q):
	K = int(input().strip())
	K_orig = K
	A_temp = input().strip()
	B_temp = input().strip()
	C_temp = input().strip()
	z = len(C_temp) * 4
	A = bin(int(A_temp, 16))[2:].zfill(z)
	B = bin(int(B_temp, 16))[2:].zfill(z)
	C = bin(int(C_temp, 16))[2:].zfill(z)
	a = list(A)
	b = list(B)
	c = list(C)
	n = len(c)
	p_1 = []
	p_2 = []
	for i in range(n):
		if c[i] == '0':
			if a[i] != '0':
				a[i] = '0'
				K -= 1
			if b[i] != '0':
				b[i] = '0'
				K -= 1
		elif a[i] == '0':
			if b[i] == '0':
				b[i] = '1'
				K -= 1
		elif b[i] == '1':
			p_1.append(i)
		else:
			p_2.append(i)
	if K < 0:
		print(-1)
	else:
		while K > 0 and (not (len(p_2) == 0 and len(p_1) == 0)):
			if len(p_2) != 0 and K >= 2 and (len(p_1) == 0 or (len(p_1) != 0 and p_2[0] < p_1[0])):
				a[p_2[0]] = '0'
				b[p_2[0]] = '1'
				K -= 2
				p_2.pop(0)
			elif len(p_1) != 0:
				a[p_1[0]] = '0'
				K -= 1
				p_1.pop(0)
			else:
				K = -1
		Ap = ''.join(a)
		Bp = ''.join(b)
		Ap = hex(int(Ap, 2))[2:].upper()
		Bp = hex(int(Bp, 2))[2:].upper()
		print(Ap)
		print(Bp)


q = int(input().strip())
for qq in range(q):
	k = int(input().strip())
	a = input().strip()
	b = input().strip()
	c = input().strip()
	max_len = max(len(a), max(len(b), len(c)))
	a_bin = ''
	b_bin = ''
	c_bin = ''
	for ch in a:
		a_bin += bin(int(ch, 16))[2:].zfill(4)
	for ch in b:
		b_bin += bin(int(ch, 16))[2:].zfill(4)
	for ch in c:
		c_bin += bin(int(ch, 16))[2:].zfill(4)
	a_bin = list(a_bin.zfill(max_len * 4))
	b_bin = list(b_bin.zfill(max_len * 4))
	c_bin = list(c_bin.zfill(max_len * 4))
	change = 0
	pos_changes = []
	for i in range(max_len * 4):
		if a_bin[i] == '0' and b_bin[i] == '0' and (c_bin[i] == '1'):
			change += 1
			b_bin[i] = '1'
		elif a_bin[i] == '0' and b_bin[i] == '1' and (c_bin[i] == '0'):
			change += 1
			b_bin[i] = '0'
		elif a_bin[i] == '1' and b_bin[i] == '0' and (c_bin[i] == '0'):
			change += 1
			a_bin[i] = '0'
		elif a_bin[i] == '1' and b_bin[i] == '0' and (c_bin[i] == '1'):
			pos_changes.append(i)
		elif a_bin[i] == '1' and b_bin[i] == '1' and (c_bin[i] == '0'):
			change += 2
			a_bin[i] = '0'
			b_bin[i] = '0'
		elif a_bin[i] == '1' and b_bin[i] == '1' and (c_bin[i] == '1'):
			pos_changes.append(i)
	if change > k:
		print(-1)
	else:
		i = 0
		while i < len(pos_changes) and change < k:
			ind = pos_changes[i]
			if b_bin[ind] == '0' and change + 2 <= k:
				a_bin[ind] = '0'
				b_bin[ind] = '1'
				change += 2
			elif b_bin[ind] == '1' and change + 1 <= k:
				a_bin[ind] = '0'
				change += 1
			i += 1
		a_bin = ''.join(a_bin)
		b_bin = ''.join(b_bin)
		a_hex = ''
		b_hex = ''
		for i in range(0, len(a_bin), 4):
			a_hex += hex(int(a_bin[i:i + 4], 2))[2:]
		for i in range(0, len(b_bin), 4):
			b_hex += hex(int(b_bin[i:i + 4], 2))[2:]
		a_hex = a_hex.lstrip('0')
		b_hex = b_hex.lstrip('0')
		a_hex = '0' if a_hex == '' else a_hex.upper()
		b_hex = '0' if b_hex == '' else b_hex.upper()
		print(a_hex)
		print(b_hex)


import sys
Q = int(sys.stdin.readline())
while Q:
	K = int(sys.stdin.readline())
	A = int(sys.stdin.readline(), 16)
	B = int(sys.stdin.readline(), 16)
	C = int(sys.stdin.readline(), 16)
	A2 = A & C
	B2 = B & C
	B3 = B2 | C & ~A2
	K -= bin(A ^ A2).count('1') + bin(B ^ B3).count('1')
	A3 = bin(A2)[2:][::-1]
	B4 = bin(B3)[2:][::-1]
	(As, Bs) = (0, 0)
	j = max(len(A3), len(B4))
	while j >= 0:
		if j < len(A3) and A3[j] == '1':
			if j < len(B4) and B4[j] == '1':
				if K > 0:
					Bs += 1 << j
					K -= 1
				else:
					As += 1 << j
					Bs += 1 << j
			elif K > 1:
				Bs += 1 << j
				K -= 2
			else:
				As += 1 << j
		elif j < len(B4) and B4[j] == '1':
			Bs += 1 << j
		j -= 1
	if K >= 0:
		print(hex(As)[2:].upper())
		print(hex(Bs)[2:].upper())
	else:
		print(-1)
	Q -= 1


testCases = int(input())
for i in range(testCases):
	maxChange = int(input())
	hexA = input()
	hexB = input()
	hexC = input()
	binA = ''
	binB = ''
	binC = ''
	binaryLength = len(hexA) * 4
	binA = '{0:0{width}b}'.format(int(hexA, 16), width=binaryLength)
	binA = list(binA)
	binB = '{0:0{width}b}'.format(int(hexB, 16), width=binaryLength)
	binB = list(binB)
	binC = '{0:0{width}b}'.format(int(hexC, 16), width=binaryLength)
	binC = list(binC)
	optionalBits = list()
	changes = 0
	for i in range(len(binC)):
		if binC[i] == '1':
			if binA[i] == '1':
				optionalBits.append(i)
			elif binB[i] == '0':
				binB[i] = '1'
				changes += 1
		if binC[i] == '0':
			if binA[i] == '1':
				binA[i] = '0'
				changes += 1
			if binB[i] == '1':
				binB[i] = '0'
				changes += 1
	if changes < maxChange:
		for i in optionalBits:
			if binB[i] == '0' and changes + 2 <= maxChange:
				binA[i] = '0'
				binB[i] = '1'
				changes += 2
			elif binB[i] == '1' and changes + 1 <= maxChange:
				binA[i] = '0'
				changes += 1
	if changes <= maxChange:
		print(format(int(''.join(binA), 2), 'x').upper())
		print(format(int(''.join(binB), 2), 'x').upper())
	else:
		print(-1)


def bin_to_hex(bin):
	return hex(int(bin, 2)).upper()[2:]

def determine_answer(k, bin_a, bin_b, bin_c):
	for i in range(len(bin_a)):
		if bin_a[i] == '0' and bin_b[i] == '0' and (bin_c[i] == '1'):
			bin_b[i] = '1'
			k -= 1
		elif bin_a[i] == '0' and bin_b[i] == '1' and (bin_c[i] == '0'):
			bin_b[i] = '0'
			k -= 1
		elif bin_a[i] == '1' and bin_b[i] == '0' and (bin_c[i] == '0'):
			bin_a[i] = '0'
			k -= 1
		elif bin_a[i] == '1' and bin_b[i] == '1' and (bin_c[i] == '0'):
			bin_a[i] = '0'
			bin_b[i] = '0'
			k -= 2
	if k < 0:
		return False
	for i in range(len(bin_a)):
		if k == 0:
			break
		if k > 1 and bin_a[i] == '1' and (bin_b[i] == '0') and (bin_c[i] == '1'):
			bin_a[i] = '0'
			bin_b[i] = '1'
			k -= 2
		elif bin_a[i] == '1' and bin_b[i] == '1' and (bin_c[i] == '1'):
			bin_a[i] = '0'
			k -= 1
	return True
test_cases = int(input())
num_of_bits = 400000
scale = 16
for i in range(test_cases):
	k = int(input())
	hex_a = input()
	hex_b = input()
	hex_c = input()
	binary_a = list(bin(int(hex_a, scale))[2:].zfill(num_of_bits))
	binary_b = list(bin(int(hex_b, scale))[2:].zfill(num_of_bits))
	binary_c = list(bin(int(hex_c, scale))[2:].zfill(num_of_bits))
	if determine_answer(k, binary_a, binary_b, binary_c):
		print(bin_to_hex(''.join(binary_a)))
		print(bin_to_hex(''.join(binary_b)))
	else:
		print(-1)


def hex_to_bin(hex_str):
	return bin(int(hex_str, 16))[2:]

def hex_to_bool_array(hex_str):
	bin_str = hex_to_bin(hex_str)
	return [c == '1' for c in bin_str]

def bool_array_to_hex(arr):
	arr = ''.join(['1' if b else '0' for b in arr])
	return hex(int(arr, 2)).upper()[2:]

def pad_bool_arrays(arrays):
	max_len = max(map(len, arrays))
	padded = []
	for (i, arr) in enumerate(arrays):
		padded.append([False] * (max_len - len(arr)) + arr)
	return padded

def read_input():
	K = int(input())
	hex_nums = [input() for i in range(3)]
	(A, B, C) = pad_bool_arrays([hex_to_bool_array(x) for x in hex_nums])
	return (K, A, B, C)

def first_pass(K, A, B, C):
	assert len(A) == len(B) == len(C)
	remaining = K
	for i in range(len(A)):
		if C[i]:
			if not (A[i] or B[i]):
				B[i] = True
				remaining -= 1
		else:
			if A[i]:
				A[i] = False
				remaining -= 1
			if B[i]:
				B[i] = False
				remaining -= 1
		if remaining < 0:
			return None
	return (remaining, A, B)

def solve(K, A, B, C):
	ans = first_pass(K, A, B, C)
	if ans is None:
		return None
	(remaining, A, B) = ans
	for i in range(len(A)):
		assert remaining >= 0
		if remaining == 0:
			break
		if C[i] and A[i]:
			if remaining >= 2 or B[i]:
				A[i] = False
				remaining -= 1
				if not B[i]:
					B[i] = True
					remaining -= 1
	return (A, B)

def main():
	for _ in range(int(input())):
		ans = solve(*read_input())
		if ans is None:
			print(-1)
		else:
			(Aprime, Bprime) = map(bool_array_to_hex, ans)
			print(Aprime)
			print(Bprime)
main()


import binascii
q = int(input().rstrip())
for i in range(0, q):
	K = int(input().rstrip())
	A = input().rstrip()
	B = input().rstrip()
	C = input().rstrip()
	binA = bin(int(A, 16))[2:]
	binB = bin(int(B, 16))[2:]
	binC = bin(int(C, 16))[2:]
	max_len = max([len(binA), len(binB), len(binC)])
	binA = [bit for bit in binA.zfill(max_len)]
	binB = [bit for bit in binB.zfill(max_len)]
	binC = [bit for bit in binC.zfill(max_len)]
	fail = False
	d = 0
	for (idx, digitC) in enumerate(binC):
		digitA = binA[idx]
		digitB = binB[idx]
		if digitC == '0':
			if digitA == '1':
				binA[idx] = '0'
				d += 1
			if digitB == '1':
				binB[idx] = '0'
				d += 1
		elif digitA == '1':
			pass
		elif digitB == '0':
			binB[idx] = '1'
			d += 1
		else:
			pass
		if d > K:
			print(-1)
			fail = True
			break
	if fail:
		continue
	for (idx, digitC) in enumerate(binC):
		digitA = binA[idx]
		digitB = binB[idx]
		if d < K:
			if digitC == '1' and digitA == '1' and (digitB == '1'):
				d += 1
				binA[idx] = '0'
		if d < K - 1:
			if digitC == '1' and digitA == '1' and (digitB == '0'):
				d += 2
				binA[idx] = '0'
				binB[idx] = '1'
	print(hex(int(''.join(binA), 2))[2:].upper())
	print(hex(int(''.join(binB), 2))[2:].upper())


bits = ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']

def hex2bits(h):
	s = ''
	for d in h:
		if d.isnumeric():
			s += bits[int(d)]
		else:
			s += bits[10 + ord(d) - 65]
	return s

def bits2hex(b):
	return hex(int(b, 2))[2:].upper()
for q in range(0, int(input().strip())):
	K = int(input().strip())
	A = hex2bits(input().strip())
	B = hex2bits(input().strip())
	C = hex2bits(input().strip())
	mx = max(len(A), len(B), len(C))
	A = A.rjust(mx, '0')
	B = B.rjust(mx, '0')
	C = C.rjust(mx, '0')
	noAnswer = False
	(D, E) = ('', '')
	for i in range(0, mx):
		(a, b, c) = (A[i], B[i], C[i])
		if (a, b, c) == ('0', '0', '1'):
			if K < 1:
				noAnswer = True
				break
			D += a
			E += '1'
			K -= 1
		elif (a, b, c) == ('0', '1', '0'):
			if K < 1:
				noAnswer = True
				break
			D += a
			E += '0'
			K -= 1
		elif (a, b, c) == ('1', '0', '0'):
			if K < 1:
				noAnswer = True
				break
			D += '0'
			E += b
			K -= 1
		elif (a, b, c) == ('1', '1', '0'):
			if K < 2:
				noAnswer = True
				break
			D += '0'
			E += '0'
			K -= 2
		else:
			D += a
			E += b
	if noAnswer:
		print(-1)
	else:
		for i in range(0, mx):
			if K < 1:
				break
			(a, b, c) = (A[i], B[i], C[i])
			if (a, b, c) == ('1', '0', '1'):
				if K >= 2:
					D = D[0:i] + '0' + D[i + 1:]
					E = E[0:i] + '1' + E[i + 1:]
					K -= 2
			elif (a, b, c) == ('1', '1', '1'):
				if K >= 1:
					D = D[0:i] + '0' + D[i + 1:]
					K -= 1
			else:
				continue
		print(bits2hex(D))
		print(bits2hex(E))


Q = int(input())
for a in range(Q):
	k = int(input())
	A = '{0:b}'.format(int(input(), 16))
	B = '{0:b}'.format(int(input(), 16))
	C = '{0:b}'.format(int(input(), 16))
	len_A = len(A)
	len_B = len(B)
	len_C = len(C)
	max_len = max(len_A, len_B, len_C)
	A = list('0' * (max_len - len_A) + A)
	B = list('0' * (max_len - len_B) + B)
	C = list('0' * (max_len - len_C) + C)
	found = True
	AChanged = [False] * max_len
	BChanged = [False] * max_len
	for ind in range(max_len):
		if C[ind] == '0':
			if A[ind] == '1':
				AChanged[ind] = True
				A[ind] = '0'
				k -= 1
			if B[ind] == '1':
				BChanged[ind] = True
				B[ind] = '0'
				k -= 1
		if C[ind] == '1':
			if A[ind] == '0' and B[ind] == '0':
				B[ind] = '1'
				BChanged[ind] = True
				k -= 1
		if k < 0:
			found = False
			break
	if k > 0:
		for ind in range(max_len):
			if A[ind] == '1' and B[ind] == '1':
				A[ind] = '0'
				if not AChanged[ind]:
					k -= 1
			if A[ind] == '1' and B[ind] == '0':
				if k > 1:
					A[ind] = '0'
					B[ind] = '1'
					if not AChanged[ind]:
						k -= 1
					if not BChanged[ind]:
						k -= 1
			if k < 1:
				break
	A = int(''.join(A), 2)
	B = int(''.join(B), 2)
	C = int(''.join(C), 2)
	if found:
		print(format(A, 'X'))
		print(format(B, 'X'))
	else:
		print('-1')


Q = int(input())
for _ in range(Q):
	K = int(input())
	A = bin(int(input(), 16))[2:]
	B = bin(int(input(), 16))[2:]
	C = bin(int(input(), 16))[2:]
	max_lth = max(len(A), len(B), len(C))
	max_lth += max_lth % 8
	A = list(A.rjust(max_lth, '0'))
	B = list(B.rjust(max_lth, '0'))
	C = list(C.rjust(max_lth, '0'))
	extras = []
	ops = 0
	for bit_pos in range(max_lth):
		if C[bit_pos] == '0':
			if A[bit_pos] == '1':
				ops += 1
				A[bit_pos] = '0'
			if B[bit_pos] == '1':
				ops += 1
				B[bit_pos] = '0'
		elif A[bit_pos] == '0' and B[bit_pos] == '0':
			ops += 1
			B[bit_pos] = '1'
		elif A[bit_pos] == '1':
			extras.append(bit_pos)
		if ops > K:
			ops = -1
			break
	if ops == -1:
		print(-1)
	else:
		for bit in extras:
			if ops < K:
				if B[bit] == '1':
					A[bit] = '0'
					ops += 1
				elif ops < K - 1:
					A[bit] = '0'
					B[bit] = '1'
					ops += 2
			else:
				break
		print(hex(int(''.join(A), 2))[2:].upper())
		print(hex(int(''.join(B), 2))[2:].upper())


for _ in range(int(input())):
	K = int(input())
	(A, B, C) = (input().strip(), input().strip(), input().strip())
	a = bin(int(A, 16))[2:]
	b = bin(int(B, 16))[2:]
	c = bin(int(C, 16))[2:]
	mx = max(len(a), len(b), len(c))
	k = 0
	a = list(map(int, list('0' * (mx - len(a)) + a)))
	b = list(map(int, list('0' * (mx - len(b)) + b)))
	c = list(map(int, list('0' * (mx - len(c)) + c)))
	for i in range(len(a)):
		if a[i] == 1:
			if b[i] == 1:
				if c[i] == 0:
					a[i] = 0
					b[i] = 0
					k += 2
			elif c[i] == 0:
				a[i] = 0
				k += 1
		elif b[i] == 1:
			if c[i] == 0:
				b[i] = 0
				k += 1
		elif c[i] == 1:
			b[i] = 1
			k += 1
		if k > K:
			print(-1)
			break
	else:
		for j in range(len(a)):
			if k == K:
				break
			if a[j] == 1 and b[j] == 1 and (c[j] == 1):
				a[j] = 0
				k += 1
				if k >= K:
					break
			if a[j] == 1 and b[j] == 0 and (c[j] == 1) and (k < K - 1):
				a[j] = 0
				b[j] = 1
				k += 2
				if k >= K:
					break
		print(str(hex(int(''.join(map(str, a)), 2))).upper()[2:])
		print(str(hex(int(''.join(map(str, b)), 2))).upper()[2:])


for _ in range(int(input())):
	k = int(input())
	(a, b, c) = (list(input()) for _ in range(3))
	max_len = max((len(num) for num in (a, b, c)))
	(a, b, c) = (['0'] * (max_len - len(num)) + num for num in (a, b, c))
	for i in range(max_len):
		(a_i, b_i, c_i) = (int(num, 16) for num in (a[i], b[i], c[i]))
		d_i = '{:04b}'.format((a_i | b_i) ^ c_i)
		(a_i, b_i, c_i) = (list('{:04b}'.format(num)) for num in (a_i, b_i, c_i))
		for j in range(4):
			if d_i[j] == '1':
				if c_i[j] == '1':
					b_i[j] = '1'
					k -= 1
				else:
					if a_i[j] == '1':
						a_i[j] = '0'
						k -= 1
					if b_i[j] == '1':
						b_i[j] = '0'
						k -= 1
				if k < 0:
					break
		if k < 0:
			break
		else:
			(a[i], b[i]) = ('{:X}'.format(int(''.join(num), 2)) for num in (a_i, b_i))
	if k < 0:
		print(-1)
	else:
		for i in range(max_len):
			(a_i, b_i) = (list('{:04b}'.format(int(num, 16))) for num in (a[i], b[i]))
			for j in range(4):
				if a_i[j] == b_i[j] == '1':
					if k >= 1:
						a_i[j] = '0'
						k -= 1
					else:
						break
				elif a_i[j] == '1' and b_i[j] == '0':
					if k >= 2:
						a_i[j] = '0'
						b_i[j] = '1'
						k -= 2
					else:
						continue
			(a[i], b[i]) = ('{:X}'.format(int(''.join(num), 2)) for num in (a_i, b_i))
			if k == 0:
				break
		string = ''.join(a).lstrip('0')
		print(string if string else '0')
		string = ''.join(b).lstrip('0')
		print(string if string else '0')


q = int(input())

class Enum:
	skip = 0
	flip_a = 1
	flip_b = 2
	flip_both = 3
	potential_flip_both = 4
	potential_flip_a = 5

def binaryToHex(x):
	return hex(int(x, 2))[2:].upper()

def hexToBinary(x, numBits):
	scale = 16
	return bin(int(x, scale))[2:].zfill(numBits)

def getAction(a, b, c):
	zeroesOrOnes = a == b == c
	eithers = (int(a) ^ int(b)) & int(c)
	if zeroesOrOnes or eithers:
		return Enum.skip
	if a == '0' and int(b) ^ int(c):
		return Enum.flip_b
	if a == '1' and b == '0' and (c == '0'):
		return Enum.flip_a
	else:
		return Enum.flip_both

def solve(k, a, b, c):
	numBits = 4 * max((len(x) for x in [a, b, c]))
	a = hexToBinary(a, numBits)
	b = hexToBinary(b, numBits)
	c = hexToBinary(c, numBits)
	flipsRemaining = k
	newA = []
	newB = []
	for i in range(len(a)):
		action = getAction(a[i], b[i], c[i])
		if action == Enum.flip_both:
			flipsRemaining -= 2
			a_char = '0'
			b_char = '0'
		elif action == Enum.flip_a:
			flipsRemaining -= 1
			a_char = '0'
			b_char = b[i]
		elif action == Enum.flip_b:
			flipsRemaining -= 1
			a_char = a[i]
			if b[i] == '0':
				b_char = '1'
			else:
				b_char = '0'
		elif action == Enum.skip:
			a_char = a[i]
			b_char = b[i]
		newA.append(a_char)
		newB.append(b_char)
		if flipsRemaining < 0:
			return '-1'
	a = ''.join(newA)
	b = ''.join(newB)
	newA = []
	newB = []
	for i in range(len(a)):
		if flipsRemaining == 0:
			newA.append(a[i:])
			newB.append(b[i:])
			break
		if a[i] == '1' and b[i] == '0' and (c[i] == '1'):
			if flipsRemaining > 1:
				flipsRemaining -= 2
				a_char = '0'
				b_char = '1'
			else:
				a_char = a[i]
				b_char = b[i]
		elif a[i] == b[i] == c[i] == '1':
			flipsRemaining -= 1
			a_char = '0'
			b_char = b[i]
		else:
			a_char = a[i]
			b_char = b[i]
		newA.append(a_char)
		newB.append(b_char)
	a = ''.join(newA)
	b = ''.join(newB)
	return '{}\n{}'.format(binaryToHex(a), binaryToHex(b))
for i in range(q):
	k = int(input())
	a = input()
	b = input()
	c = input()
	print(solve(k, a, b, c))


import sys

def parse_input(n):
	return list('{0:0b}'.format(int(n, 16)))

def lpad(x, size):
	return ['0'] * (size - len(x)) + x

def normalize(A, B, C):
	size = max(len(A), len(B), len(C))
	return (lpad(A, size), lpad(B, size), lpad(C, size))

def solve(A, B, C, K):
	changes = 0
	index = 0
	while index < len(C):
		if C[index] == '0':
			if A[index] == '1':
				A[index] = '0'
				changes = changes + 1
			if B[index] == '1':
				B[index] = '0'
				changes = changes + 1
		elif A[index] == '0' and B[index] == '0':
			B[index] = '1'
			changes = changes + 1
		index += 1
	index = 0
	while index < len(A) and changes < K:
		if A[index] == '1' and B[index] == '1':
			A[index] = '0'
			changes = changes + 1
		elif A[index] == '1' and B[index] == '0' and (changes <= K - 2):
			A[index] = '0'
			B[index] = '1'
			changes = changes + 2
		index += 1
	if changes <= K:
		print('{0:X}'.format(int(''.join(A), 2)))
		print('{0:X}'.format(int(''.join(B), 2)))
	else:
		print('-1')
cases = int(input())
for case in range(cases):
	K = int(input().strip())
	A = parse_input(input().strip())
	B = parse_input(input().strip())
	C = parse_input(input().strip())
	(A, B, C) = normalize(A, B, C)
	solve(A, B, C, K)


maxm = 10 ** 9

def distance(x, y, z, K):
	count = 0
	(x, y, z) = (list(x), list(y), list(z))
	for i in range(len(x)):
		(a, b, c) = (x[i], y[i], z[i])
		if c == '1':
			if a == '0' and b == '0':
				(count, y[i]) = (count + 1, '1')
		elif c == '0':
			if a == '1':
				(x[i], count) = ('0', count + 1)
			if b == '1':
				(y[i], count) = ('0', count + 1)
	if count > K:
		print(-1)
	else:
		count = K - count
		for i in range(len(x)):
			(a, b, c) = (x[i], y[i], z[i])
			if count < 1:
				break
			if c == '1':
				if a == '1' and b == '1':
					(count, x[i]) = (count - 1, '0')
				elif a == '1' and b == '0' and (count >= 2):
					(x[i], y[i], count) = ('0', '1', count - 2)
		nA = ''.join([str(ll) for ll in x])
		mA = ''.join([str(mm) for mm in y])
		nB = hex(int(nA, 2))[2:].upper()
		mB = hex(int(mA, 2))[2:].upper()
		print(nB)
		print(mB)
for _ in range(int(input())):
	K = int(input())
	A = '0x' + input().strip().upper()
	B = '0x' + input().strip().upper()
	C = '0x' + input().strip().upper()
	(x, y, z) = (bin(int(A, 16))[2:], bin(int(B, 16))[2:], bin(int(C, 16))[2:])
	maxPad = max(len(x), len(y), len(z))
	x = x.rjust(maxPad, '0')
	y = y.rjust(maxPad, '0')
	z = z.rjust(maxPad, '0')
	distance(x, y, z, K)


Q = int(input())
for _ in range(0, Q):
	num_swaps = int(input())
	a = int(input(), 16)
	b = int(input(), 16)
	c = int(input(), 16)
	a = [int(d) for d in bin(a)[2:]]
	b = [int(d) for d in bin(b)[2:]]
	c = [int(d) for d in bin(c)[2:]]
	ideal_length = max(map(len, (a, b, c)))
	a = [0] * (ideal_length - len(a)) + a
	b = [0] * (ideal_length - len(b)) + b
	c = [0] * (ideal_length - len(c)) + c
	for i in range(0, ideal_length):
		if c[i] == 0:
			if a[i] == 1:
				a[i] = 0
				num_swaps -= 1
			if b[i] == 1:
				b[i] = 0
				num_swaps -= 1
		elif c[i] == 1:
			if a[i] == 0 and b[i] == 0:
				b[i] = 1
				num_swaps -= 1
		else:
			print(c[i])
			raise Exception()
	for i in range(0, ideal_length):
		if c[i] == 0:
			if a[i] == 1 or b[i] == 1:
				raise Exception()
		if c[i] == 1:
			if a[i] == 0 and b[i] == 0:
				raise Exception()
	if num_swaps < 0:
		print(-1)
		continue
	for i in range(0, ideal_length):
		if num_swaps <= 0:
			break
		if a[i] == 1:
			if b[i] == 1:
				a[i] = 0
				num_swaps -= 1
			elif num_swaps >= 2:
				a[i] = 0
				b[i] = 1
				num_swaps -= 2
	a_int = int(''.join(('01'[i] for i in a)), 2)
	b_int = int(''.join(('01'[i] for i in b)), 2)
	print('{:X}'.format(a_int))
	print('{:X}'.format(b_int))


import sys
Q = int(input().strip())

def boolify(s, length, mlength):
	res = [False] * (4 * mlength)
	k = mlength - length
	for i in range(0, length):
		x = int(s[i], 16)
		res[4 * k + 0] = x & 8 != 0
		res[4 * k + 1] = x & 4 != 0
		res[4 * k + 2] = x & 2 != 0
		res[4 * k + 3] = x & 1 != 0
		k += 1
	return res

def minimize(x, y, k, l):
	i = 0
	while k and i < l:
		if x[i] and (not y[i]) and (k >= 2):
			x[i] = False
			y[i] = True
			k -= 2
		if x[i] and y[i]:
			x[i] = False
			k -= 1
		i += 1
	return k

def print_res(x, l):
	hx = '0123456789ABCDEF'
	i = 0
	while i < l - 4 and x[i] == x[i + 1] == x[i + 2] == x[i + 3] == 0:
		i += 4
	while i < l:
		sys.stdout.write(hx[(x[i] << 3) + (x[i + 1] << 2) + (x[i + 2] << 1) + x[i + 3]])
		i += 4
	sys.stdout.write('\n')

def query():
	K = int(input().strip())
	sA = input().strip()
	sB = input().strip()
	sC = input().strip()
	(lA, lB, lC) = (len(sA), len(sB), len(sC))
	length = max(lA, lB, lC)
	A = boolify(sA, lA, length)
	B = boolify(sB, lB, length)
	C = boolify(sC, lC, length)
	i = 4 * length
	while i:
		i -= 1
		(a, b, c) = (A[i], B[i], C[i])
		if (a or b) != c:
			if c:
				B[i] = True
				K -= 1
			else:
				if a:
					A[i] = False
					K -= 1
				if b:
					B[i] = False
					K -= 1
		if K < 0:
			print(-1)
			return
	minimize(A, B, K, 4 * length)
	print_res(A, 4 * length)
	print_res(B, 4 * length)
while Q:
	query()
	Q -= 1


def oor(a, b):
	if str(a) == '1' or str(b) == '1':
		return 1
	else:
		return 0
for _ in range(int(input())):
	k = int(input())
	a = str(bin(int(input(), 16)))[2:]
	b = str(bin(int(input(), 16)))[2:]
	c = str(bin(int(input(), 16)))[2:]
	le = max([len(i) for i in [a, b, c]])
	a = list(''.join(['0'] * abs(le - len(a))) + a)
	b = list(''.join(['0'] * abs(le - len(b))) + b)
	c = list(''.join(['0'] * abs(le - len(c))) + c)
	no = True
	for i in range(le):
		if k < 0:
			print('-1')
			no = False
			break
		if not oor(a[i], b[i]) == int(c[i]):
			if c[i] == '0':
				if a[i] == '0':
					b[i] = '0'
					k -= 1
				elif b[i] == '0':
					a[i] = '0'
					k -= 1
				else:
					if k < 2:
						no = False
						print('-1')
						break
					a[i] = '0'
					b[i] = '0'
					k -= 2
			else:
				b[i] = '1'
				k -= 1
	if k > 0:
		for i in range(le):
			if k <= 0:
				break
			if a[i] == '1' and b[i] == '0':
				if k >= 2:
					a[i] = '0'
					b[i] = '1'
					k -= 2
			if a[i] == '1' and a[i] == b[i]:
				a[i] = '0'
				k -= 1
	if no:
		print(str(hex(int(''.join(a), 2)))[2:].upper())
		print(str(hex(int(''.join(b), 2)))[2:].upper())


Q = int(input().strip())
for q in range(Q):
	K = int(input().strip())
	A = int(input().strip(), base=16)
	B = int(input().strip(), base=16)
	C = int(input().strip(), base=16)
	bina = [int(s) for s in bin(A).split('b')[1]]
	binb = [int(s) for s in bin(B).split('b')[1]]
	binc = [int(s) for s in bin(C).split('b')[1]]
	lena = len(bina)
	lenb = len(binb)
	lenc = len(binc)
	maxl = max(lena, lenb, lenc)
	if lena < maxl:
		for index in range(lena, maxl):
			bina.insert(0, 0)
	if lenb < maxl:
		for index in range(lenb, maxl):
			binb.insert(0, 0)
	if lenc < maxl:
		for index in range(lenc, maxl):
			binc.insert(0, 0)
	count = 0
	for index in range(maxl):
		if binc[index] == 0:
			if bina[index] == 1:
				bina[index] = 0
				count += 1
			if binb[index] == 1:
				binb[index] = 0
				count += 1
		elif bina[index] == 0 and binb[index] == 0:
			binb[index] = 1
			count += 1
	if count > K:
		failed = True
	else:
		failed = False
	index = 0
	while count < K and index < maxl:
		if bina[index] == 1:
			if binb[index] == 1:
				bina[index] = 0
				count += 1
			elif K - count > 1:
				bina[index] = 0
				binb[index] = 1
				count += 2
		index += 1
	A_prime = int(''.join((str(b) for b in bina)), base=2)
	B_prime = int(''.join((str(b) for b in binb)), base=2)
	if failed:
		print(-1)
	else:
		print(hex(A_prime).upper().split('X')[1])
		print(hex(B_prime).upper().split('X')[1])


q = int(input())
for repetition in range(q):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	l = max(map(len, (a, b, c)))
	a = list('0' * (l - len(a)) + a)
	b = list('0' * (l - len(b)) + b)
	c = list('0' * (l - len(c)) + c)
	i = 0
	while i < l:
		if c[i] == '0':
			if a[i] == '1':
				k -= 1
				a[i] = '0'
			if b[i] == '1':
				k -= 1
				b[i] = '0'
		elif a[i] == '0' and b[i] == '0':
			b[i] = '1'
			k -= 1
		if k < 0:
			print(-1)
			break
		i += 1
	else:
		i = 0
		while i < l and k > 0:
			if a[i] == '1':
				if b[i] == '1':
					k -= 1
					a[i] = '0'
				elif k > 1:
					k -= 2
					a[i] = '0'
					b[i] = '1'
			i += 1
		print(format(int(''.join(a), 2), 'X'))
		print(format(int(''.join(b), 2), 'X'))


q = int(input())
while q > 0:
	k = int(input())
	a = input()
	b = input()
	c = input()
	h_size = max(len(a) * 4, max(len(b) * 4, len(c) * 4))
	bin_a = bin(int(a, 16))[2:].zfill(h_size)
	bin_b = bin(int(b, 16))[2:].zfill(h_size)
	bin_c = bin(int(c, 16))[2:].zfill(h_size)
	ans_a = list(bin_a)
	ans_b = list(bin_b)
	count = 0
	for i in range(0, len(bin_c)):
		if bin_c[i] == '0':
			if bin_a[i] != '0':
				ans_a[i] = '0'
				count += 1
			if bin_b[i] != '0':
				ans_b[i] = '0'
				count += 1
		elif bin_a[i] == '1':
			continue
		elif bin_b[i] != '1':
			ans_b[i] = '1'
			count += 1
	if count <= k:
		if count < k:
			k = k - count
			for i in range(0, len(bin_a)):
				if ans_a[i] == '1':
					if ans_b[i] == '1':
						if k >= 1:
							ans_a[i] = '0'
							k -= 1
					elif k >= 2:
						ans_a[i] = '0'
						ans_b[i] = '1'
						k -= 2
		ans_a = ''.join(ans_a)
		ans_b = ''.join(ans_b)
		ans1 = '%*X' % ((len(ans_a) + 3) // 4, int(ans_a, 2))
		ans2 = '%*X' % ((len(ans_b) + 3) // 4, int(ans_b, 2))
		print(str(ans1).upper().strip())
		print(str(ans2).upper().strip())
	else:
		print(-1)
	q -= 1


q = int(input())
while q > 0:
	k = int(input())
	a = input()
	b = input()
	c = input()
	h_size = max(len(a) * 4, max(len(b) * 4, len(c) * 4))
	bin_a = bin(int(a, 16))[2:].zfill(h_size)
	bin_b = bin(int(b, 16))[2:].zfill(h_size)
	bin_c = bin(int(c, 16))[2:].zfill(h_size)
	ans_a = list(bin_a)
	ans_b = list(bin_b)
	count = 0
	for i in range(0, len(bin_c)):
		if bin_c[i] == '0':
			if bin_a[i] != '0':
				ans_a[i] = '0'
				count += 1
			if bin_b[i] != '0':
				ans_b[i] = '0'
				count += 1
		elif bin_a[i] == '1':
			continue
		elif bin_b[i] != '1':
			ans_b[i] = '1'
			count += 1
	if count <= k:
		if count < k:
			k = k - count
			for i in range(0, len(bin_a)):
				if ans_a[i] == '1':
					if ans_b[i] == '1':
						if k >= 1:
							ans_a[i] = '0'
							k -= 1
					elif k >= 2:
						ans_a[i] = '0'
						ans_b[i] = '1'
						k -= 2
		ans_a = ''.join(ans_a)
		ans_b = ''.join(ans_b)
		ans1 = '%*X' % ((len(ans_a) + 3) // 4, int(ans_a, 2))
		ans2 = '%*X' % ((len(ans_b) + 3) // 4, int(ans_b, 2))
		print(str(ans1).upper().strip())
		print(str(ans2).upper().strip())
	else:
		print(-1)
	q -= 1


import sys
Q = int(input().strip())

def printHex(A, B):
	print(hex(int(''.join(A), 2)).upper()[2:])
	print(hex(int(''.join(B), 2)).upper()[2:])
for t in range(Q):
	k = int(input().strip())
	A = list(bin(int(input().strip(), 16))[2:])
	B = list(bin(int(input().strip(), 16))[2:])
	C = list(bin(int(input().strip(), 16))[2:])
	longitud = max(len(A), len(B), len(C))
	A = ['0' for i in range(longitud - len(A))] + A
	B = ['0' for i in range(longitud - len(B))] + B
	C = ['0' for i in range(longitud - len(C))] + C
	for i in range(longitud):
		if C[i] == '0':
			if A[i] != '0':
				A[i] = '0'
				k -= 1
			if B[i] != '0':
				B[i] = '0'
				k -= 1
		elif A[i] != '1' and B[i] != '1':
			B[i] = '1'
			k -= 1
	if k < 0:
		print(-1)
	elif k == 0:
		printHex(A, B)
	else:
		for i in range(longitud):
			if C[i] == '1':
				if B[i] == '1' and A[i] == '1' and (k >= 1):
					A[i] = '0'
					k -= 1
				elif B[i] == '0' and A[i] == '1' and (k >= 2):
					A[i] = '0'
					B[i] = '1'
					k -= 2
		printHex(A, B)


for T in range(int(input())):
	k = int(input())
	a = input()
	b = input()
	c = input()
	A = [x for x in bin(int(a, 16))[2:]]
	B = [x for x in bin(int(b, 16))[2:]]
	C = [x for x in bin(int(c, 16))[2:]]
	length = [len(A), len(B), len(C)]
	m = max(length)
	A = ['0' for i in range(m - length[0])] + A
	B = ['0' for i in range(m - length[1])] + B
	C = ['0' for i in range(m - length[2])] + C
	ch = 0
	bah = 0
	d = {}
	n = 0
	for i in range(m):
		if C[i] == '0':
			if A[i] != '0':
				if ch + 1 <= k:
					ch += 1
					A[i] = '0'
				else:
					bah = 1
					break
			if B[i] != '0':
				if ch + 1 <= k:
					ch += 1
					B[i] = '0'
				else:
					bah = 1
					break
		if C[i] == '1':
			if A[i] == '0' and B[i] == '0':
				B[i] = '1'
				ch += 1
		if C[i] == '1' and A[i] == '1' and (B[i] == '1'):
			n += 1
			d[i] = 1
		elif C[i] == '1' and A[i] == '1' and (B[i] == '0'):
			n += 1
			d[i] = 2
	if bah == 1:
		print(-1)
		continue
	i = 0
	l = sorted(d)
	while k - ch > 0 and i < n:
		if k - ch >= 2 and d[l[i]] == 2:
			A[l[i]] = '0'
			B[l[i]] = '1'
			ch += 2
		elif k - ch >= 1 and d[l[i]] == 1:
			A[l[i]] = '0'
			ch += 1
		i += 1
	a = ''
	for i in A:
		a += i
	print(hex(int(a, 2))[2:].upper())
	b = ''
	for i in B:
		b += i
	print(hex(int(b, 2))[2:].upper())


bn = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}
hx = {'0000': '0', '0001': '1', '0010': '2', '0011': '3', '0100': '4', '0101': '5', '0110': '6', '0111': '7', '1000': '8', '1001': '9', '1010': 'A', '1011': 'B', '1100': 'C', '1101': 'D', '1110': 'E', '1111': 'F'}
for each in range(int(input().strip())):
	k = int(input().strip())
	a = list(input().strip())
	b = list(input().strip())
	c = list(input().strip())
	ab = ''
	bb = ''
	cb = ''
	for i in range(max(len(c), len(b), len(a)) - 1, -1, -1):
		if len(a) > 0:
			A = a.pop()
		else:
			A = '0'
		if len(b) > 0:
			B = b.pop()
		else:
			B = '0'
		if len(c) > 0:
			C = c.pop()
		else:
			C = '0'
		ab = bn[A] + ab
		bb = bn[B] + bb
		cb = bn[C] + cb
	ab = list(ab)
	bb = list(bb)
	cb = list(cb)
	t = 0
	i = len(cb) - 1
	while i >= 0 and t <= k:
		if max(ab[i], bb[i]) != cb[i]:
			if cb[i] == '0':
				if ab[i] == '1':
					ab[i] = '0'
					t = t + 1
				if bb[i] == '1':
					bb[i] = '0'
					t = t + 1
			else:
				bb[i] = '1'
				t = t + 1
		i = i - 1
	if t > k:
		print(-1)
		continue
	l = k - t
	if l > 0:
		i = 0
		while l > 0 and i < len(cb):
			if cb[i] == '1':
				if bb[i] == '1':
					if ab[i] == '1':
						ab[i] = '0'
						l = l - 1
				elif l > 1:
					ab[i] = '0'
					bb[i] = '1'
					l = l - 2
			i = i + 1
	ax = ''
	bx = ''
	for i in range(len(ab) // 4):
		A = ''.join(ab[i * 4:(i + 1) * 4])
		B = ''.join(bb[i * 4:(i + 1) * 4])
		if hx[A] != '0' or len(ax) != 0:
			ax = ax + hx[A]
		if hx[B] != '0' or len(bx) != 0:
			bx = bx + hx[B]
	if len(ax) == 0:
		ax = '0'
	if len(bx) == 0:
		bx = '0'
	print(ax)
	print(bx)


q = int(input().strip())
for q0 in range(q):
	k = int(input().strip())
	a = int(input().strip(), 16)
	b = int(input().strip(), 16)
	c = int(input().strip(), 16)
	a1 = c & a
	b1 = c & b | c & ~a
	diff = bin(a1 ^ a).count('1') + bin(b1 ^ b).count('1')
	if diff > k:
		print(-1)
	else:
		flips_left = k - diff
		bina1 = bin(a1)[2:]
		binb1 = bin(b1)[2:]
		maxlen = max(len(bina1), len(binb1))
		bina1 = bina1.zfill(maxlen)
		binb1 = binb1.zfill(maxlen)
		bina1 = [1 if digit == '1' else 0 for digit in bina1]
		binb1 = [1 if digit == '1' else 0 for digit in binb1]
		for i in range(0, maxlen):
			if flips_left == 0:
				break
			if bina1[i] == 1 and (flips_left > 1 or (flips_left == 1 and binb1[i] == 1)):
				bina1[i] = 0
				flips_left -= 1
				if binb1[i] == 0:
					binb1[i] = 1
					flips_left -= 1
		a2 = int(''.join((str(d) for d in bina1)), 2)
		b2 = int(''.join((str(d) for d in binb1)), 2)
		print('{:X}'.format(a2))
		print('{:X}'.format(b2))


Q = int(input().strip())

def bump_len(s, l):
	return '0' * (l - len(s)) + s
count = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4]

def bits_for_digit(a, b, c):
	ab = a | b
	drop_a = a & ~c
	drop_b = b & ~c
	needed = ~ab & c
	return count[needed] + count[drop_a] + count[drop_b]

def doit(a, b, c, extra):
	a &= c
	b &= c
	b |= c & ~a
	for i in [8, 4, 2, 1]:
		if extra == 0:
			break
		if a & i != 0:
			if b & i != 0:
				a ^= i
				extra -= 1
			elif extra > 1:
				a ^= i
				b ^= i
				extra -= 2
	return (a, b, extra)

def hexzip(ABC):
	for abc in zip(*ABC):
		yield [int(x, 16) for x in abc]
for _ in range(Q):
	K = int(input().strip())
	ABC = [input().strip() for _ in range(3)]
	length = max((len(s) for s in ABC))
	ABC = [bump_len(s, length) for s in ABC]
	need = 0
	for (a, b, c) in hexzip(ABC):
		need += bits_for_digit(a, b, c)
	if need > K:
		print(-1)
		continue
	extra = K - need
	out_a = out_b = ''
	na = nb = 0
	for (a, b, c) in hexzip(ABC):
		(oa, ob, extra) = doit(a, b, c, extra)
		na += count[a ^ oa]
		nb += count[b ^ ob]
		if out_a == '0':
			out_a = ''
		if out_b == '0':
			out_b = ''
		out_a += '%X' % (oa,)
		out_b += '%X' % (ob,)
	assert na + nb <= K
	print(out_a)
	print(out_b)


for _ in range(int(input())):
	K = int(input())
	A = bin(int(input(), 16))[2:]
	B = bin(int(input(), 16))[2:]
	C = bin(int(input(), 16))[2:]
	A1 = ''
	B1 = ''
	k = 0
	l = max(len(A), len(B), len(C))
	A = '0' * (l - len(A)) + A
	B = '0' * (l - len(B)) + B
	C = '0' * (l - len(C)) + C
	flag = [0] * l
	for i in range(l):
		if C[i] == '0':
			if A[i] == '1':
				A1 += '0'
				k += 1
			else:
				A1 += '0'
			if B[i] == '1':
				B1 += '0'
				k += 1
			else:
				B1 += '0'
		elif A[i] == '0':
			A1 += '0'
			if B[i] == '1':
				B1 += '1'
			else:
				B1 += '1'
				k += 1
		else:
			A1 += '1'
			B1 += B[i]
			if B[i] == '1':
				flag[i] = 1
			else:
				flag[i] = 2
	if k > K:
		print(-1)
	elif k == K:
		print(hex(int(A1, 2))[2:].upper())
		print(hex(int(B1, 2))[2:].upper())
	else:
		A2 = ''
		B2 = ''
		d = K - k
		for i in range(l):
			if flag[i]:
				if int(d / 2) > 0:
					A2 += '0'
					d -= 1
					if B[i] == '0':
						B2 += '1'
						d -= 1
					else:
						B2 += '1'
				elif B1[i] == '1':
					A2 += '0'
					d -= 1
					B2 += '1'
				else:
					A2 += A1[i]
					B2 += B1[i]
				if d <= 0:
					A2 += A1[i + 1:]
					B2 += B1[i + 1:]
					break
			else:
				A2 += A1[i]
				B2 += B1[i]
		print(hex(int(A2, 2))[2:].upper())
		print(hex(int(B2, 2))[2:].upper())


def fun(K, ABC):
	abc = []
	l = max([len(i) for i in ABC])
	for i in ABC:
		abc.append(list(bin(int(i, 16))[2:].zfill(4 * l)))
	for i in range(4 * l):
		if abc[0][i] == '0' and abc[1][i] == '0' and (abc[2][i] == '1'):
			abc[1][i] = '1'
			K -= 1
		if abc[0][i] == '0' and abc[1][i] == '1' and (abc[2][i] == '0'):
			abc[1][i] = '0'
			K -= 1
		if abc[0][i] == '1' and abc[1][i] == '0' and (abc[2][i] == '0'):
			abc[0][i] = '0'
			K -= 1
		if abc[0][i] == '1' and abc[1][i] == '1' and (abc[2][i] == '0'):
			abc[0][i] = '0'
			abc[1][i] = '0'
			K -= 2
		if K < 0:
			return -1
	if K >= 1:
		for i in range(4 * l):
			if abc[0][i] == '1' and abc[1][i] == '1' and (abc[2][i] == '1') and (K >= 1):
				abc[0][i] = '0'
				K -= 1
			if abc[0][i] == '1' and abc[1][i] == '0' and (abc[2][i] == '1') and (K >= 2):
				abc[0][i] = '0'
				abc[1][i] = '1'
				K -= 2
			if K < 1:
				break
	return [hex(int(''.join(abc[0]), 2))[2:].upper(), hex(int(''.join(abc[1]), 2))[2:].upper()]
for _ in range(int(input())):
	K = int(input())
	A = input()
	B = input()
	C = input()
	res = fun(K, [A, B, C])
	if res == -1:
		print(-1)
	else:
		for i in res:
			print(i)


total = 0
Q = int(input())
for iets in range(Q):
	K = int(input())
	A = list(format(int(input(), 16), '0>200006b'))
	B = list(format(int(input(), 16), '0>200006b'))
	C = format(int(input(), 16), '0>200006b')
	for index in range(200006):
		if C[index] == '0':
			if A[index] == '1':
				A[index] = '0'
				K -= 1
			if B[index] == '1':
				B[index] = '0'
				K -= 1
		elif A[index] == '0' and B[index] == '0':
			B[index] = '1'
			K -= 1
	if K < 0:
		print(-1)
	else:
		for index in range(200006):
			if K == 0:
				break
			if C[index] == '1':
				if A[index] == '1' and B[index] == '1':
					A[index] = '0'
					K -= 1
				elif A[index] == '1' and B[index] == '0' and (K != 1):
					A[index] = '0'
					B[index] = '1'
					K -= 2
		print(str(hex(int(''.join(A), 2))[2:].upper()))
		print(str(hex(int(''.join(B), 2))[2:].upper()))


import math
q = int(input().strip())
k = []
a = []
b = []
c = []
debug = 0
for i in range(q):
	k.append(int(input().strip()))
	a.append(bin(int(input().strip(), base=16))[2:])
	b.append(bin(int(input().strip(), base=16))[2:])
	c.append(bin(int(input().strip(), base=16))[2:])
if debug == 1:
	print('q: {}'.format(q))
	print('k: {}'.format(k))
	print('a: {}'.format(a))
	print('b: {}'.format(b))
	print('c: {}'.format(c))
for i in range(q):
	m = max(len(a[i]), len(b[i]), len(c[i]))
	a[i] = list(a[i].zfill(m))
	b[i] = list(b[i].zfill(m))
	c[i] = list(c[i].zfill(m))
if debug == 1:
	print('a: {}'.format(a))
	print('b: {}'.format(b))
	print('c: {}'.format(c))
for i in range(q):
	valid = True
	j = 0
	while j < len(a[i]):
		if a[i][j] == '1' and b[i][j] == '1' and (c[i][j] == '0'):
			if k[i] == 0 or k[i] == 1:
				valid = False
				break
			a[i][j] = '0'
			b[i][j] = '0'
			k[i] -= 2
		elif a[i][j] == '0' and b[i][j] == '1' and (c[i][j] == '0'):
			if k[i] == 0:
				valid = False
				break
			b[i][j] = '0'
			k[i] -= 1
		elif a[i][j] == '1' and b[i][j] == '0' and (c[i][j] == '0'):
			if k[i] == 0:
				valid = False
				break
			a[i][j] = '0'
			k[i] -= 1
		elif a[i][j] == '0' and b[i][j] == '0' and (c[i][j] == '1'):
			if k[i] == 0:
				valid = False
				break
			b[i][j] = '1'
			k[i] -= 1
		j += 1
	if valid is False:
		print(-1)
	else:
		j = 0
		while j < len(a[i]):
			if a[i][j] == '1' and b[i][j] == '1' and (c[i][j] == '1'):
				if k[i] == 0:
					break
				a[i][j] = '0'
				k[i] -= 1
			elif a[i][j] == '1' and b[i][j] == '0' and (c[i][j] == '1'):
				if k[i] == 0:
					break
				elif k[i] > 1:
					a[i][j] = '0'
					b[i][j] = '1'
					k[i] -= 2
			j += 1
		a[i] = '0b' + ''.join(a[i])
		b[i] = '0b' + ''.join(b[i])
		c[i] = '0b' + ''.join(c[i])
		print(str(hex(int(a[i], base=2)))[2:].upper())
		print(str(hex(int(b[i], base=2)))[2:].upper())
exit(0)


def HEX(A):
	A = ''.join(list(map(str, A)))
	A = int(A, 2)
	A = hex(A).replace('0x', '')
	return A
q = int(input())
for qq in range(q):
	k = int(input())
	a = int(input(), 16)
	b = int(input(), 16)
	c = int(input(), 16)
	A = list(bin(a).replace('0b', ''))
	B = list(bin(b).replace('0b', ''))
	C = list(bin(c).replace('0b', ''))
	m = max(len(A), len(B), len(C))
	A = list('0' * (m - len(A))) + A
	B = list('0' * (m - len(B))) + B
	C = list('0' * (m - len(C))) + C
	for i in range(m):
		A[i] = int(A[i])
		B[i] = int(B[i])
		C[i] = int(C[i])
	for i in range(m):
		s = '{}{}{}'.format(A[i], B[i], C[i])
		if s == '000':
			continue
		if s == '001':
			B[i] = 1
			k -= 1
		if s == '010':
			B[i] = 0
			k -= 1
		if s == '011':
			continue
		if s == '100':
			A[i] = 0
			k -= 1
		if s == '101':
			continue
		if s == '110':
			A[i] = 0
			B[i] = 0
			k -= 2
		if s == '111':
			continue
	if k < 0:
		print('-1')
	if k == 0:
		print(HEX(A).upper())
		print(HEX(B).upper())
	if k > 0:
		for i in range(m):
			s = '{}{}{}'.format(A[i], B[i], C[i])
			if s == '000':
				continue
			if s == '011':
				continue
			if s == '101':
				if k > 1:
					A[i] = 0
					B[i] = 1
					k -= 2
			if s == '111':
				if k > 0:
					A[i] = 0
					k -= 1
		print(HEX(A).upper())
		print(HEX(B).upper())


Q = int(input())
for _ in range(Q):
	K = int(input())
	A = int(input(), 16)
	B = int(input(), 16)
	C = int(input(), 16)
	if K == 0:
		if A | B == C:
			print('{:X}'.format(A))
			print('{:X}'.format(B))
		else:
			print(-1)
		continue
	M = max(A, B, C)
	L = len('{0:b}'.format(M))
	a = '{:0{}b}'.format(A, L)
	b = '{:0{}b}'.format(B, L)
	c = '{:0{}b}'.format(C, L)
	a = list(a)
	b = list(b)
	b2 = list(b)
	for (i, v) in enumerate(c):
		if v == '0':
			if a[i] == '1':
				a[i] = '0'
				K -= 1
			if b[i] == '1':
				b[i] = '0'
				K -= 1
		elif a[i] == '0' and b[i] == '0':
			b[i] = '1'
			K -= 1
	if K < 0:
		print(-1)
		continue
	for (i, v) in enumerate(c):
		if v == '1':
			if a[i] == '1':
				if b[i] == '1':
					a[i] = '0'
					K -= 1
				elif K > 1:
					a[i] = '0'
					b[i] = '1'
					K -= 2
					assert b2[i] == '0'
		if K == 0:
			break
	print('{:X}'.format(int(''.join(a), 2)))
	print('{:X}'.format(int(''.join(b), 2)))


Q = int(input())
for i in range(0, Q):
	K = int(input())
	A = int(input(), 16)
	B = int(input(), 16)
	C = int(input(), 16)
	length = max(len(bin(A)), len(bin(B)), len(bin(C))) - 2
	diff = C ^ (A | B)
	basic = bin(diff).count('1') + bin(A & B & ~C).count('1')
	if basic > K:
		print(-1)
	else:
		nB = B - (B & diff) + (~A & ~B & C)
		nA = A - (A & diff)
		e = K - basic
		inds = [i for (i, x) in enumerate(bin(nA & C)[2:].zfill(length)) if x == '1']
		bA = bin(nA)[2:].zfill(length)
		bB = bin(nB)[2:].zfill(length)
		for i in inds:
			if bB[i] == '0':
				if e >= 2:
					bA = bA[:i] + '0' + bA[i + 1:]
					bB = bB[:i] + '1' + bB[i + 1:]
					e = e - 2
			elif e >= 1:
				bA = bA[:i] + '0' + bA[i + 1:]
				e = e - 1
		nA = int(bA, 2)
		nB = int(bB, 2)
		print(hex(nA)[2:].upper())
		print(hex(nB)[2:].upper())


dtoh = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}
T = int(input())
for t in range(T):
	K = int(input())
	A = list(input())
	B = list(input())
	C = list(input())
	lenA = len(A)
	lenB = len(B)
	lenC = len(C)
	length = max(lenA, lenB, lenC)
	A = ['0'] * (length - lenA) + A
	B = ['0'] * (length - lenB) + B
	C = ['0'] * (length - lenC) + C
	count = 0
	bits = [8, 4, 2, 1]
	for i in range(length):
		a = int(A[i], 16)
		b = int(B[i], 16)
		c = int(C[i], 16)
		an = 0
		bn = 0
		for j in range(4):
			aj = 1 if a & bits[j] else 0
			bj = 1 if b & bits[j] else 0
			cj = 1 if c & bits[j] else 0
			if cj == 0:
				if aj == 1:
					aj = 0
					K -= 1
				if bj == 1:
					bj = 0
					K -= 1
			if cj == 1:
				if aj == 0 and bj == 0:
					bj = 1
					K -= 1
			an |= bits[j] if aj else 0
			bn |= bits[j] if bj else 0
		A[i] = dtoh[an]
		B[i] = dtoh[bn]
	for i in range(lenC):
		a = int(A[i], 16)
		b = int(B[i], 16)
		c = int(C[i], 16)
		an = 0
		bn = 0
		for j in range(4):
			aj = 1 if a & bits[j] else 0
			bj = 1 if b & bits[j] else 0
			cj = 1 if c & bits[j] else 0
			if cj == 1:
				if aj == 1 and bj == 1 and (K > 0):
					aj = 0
					K -= 1
				elif aj == 1 and bj == 0 and (K > 1):
					aj = 0
					bj = 1
					K -= 2
			an |= bits[j] if aj else 0
			bn |= bits[j] if bj else 0
		A[i] = dtoh[an]
		B[i] = dtoh[bn]
	if K < 0:
		print('-1')
	else:
		i = 0
		while i < len(A) - 1 and A[0] == '0':
			del A[i]
		while i < len(B) - 1 and B[0] == '0':
			del B[i]
		print(''.join(A))
		print(''.join(B))


Q = int(input())
d = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}
d2 = {'0000': '0', '0001': '1', '0010': '2', '0011': '3', '0100': '4', '0101': '5', '0110': '6', '0111': '7', '1000': '8', '1001': '9', '1010': 'A', '1011': 'B', '1100': 'C', '1101': 'D', '1110': 'E', '1111': 'F'}
while Q > 0:
	Q -= 1
	k = int(input())
	A = input()
	B = input()
	C = input()
	mx = max([len(A), len(B), len(C)])
	A = '0' * (mx - len(A)) + A
	B = '0' * (mx - len(B)) + B
	C = '0' * (mx - len(C)) + C
	aa = ''
	bb = ''
	cc = ''
	for a in A:
		aa += d[a]
	for b in B:
		bb += d[b]
	for c in C:
		cc += d[c]
	A = list(map(int, aa))
	B = list(map(int, bb))
	C = list(map(int, cc))
	mx = len(A)
	ans = 0
	for i in range(mx):
		if A[i] | B[i] == C[i]:
			continue
		if C[i] == 0:
			if A[i]:
				A[i] = 0
				k -= 1
			if B[i]:
				B[i] = 0
				k -= 1
		else:
			B[i] = 1
			k -= 1
	if k < 0:
		print(-1)
		continue
	for i in range(mx):
		if k <= 0:
			break
		if A[i] == 1 and B[i] == 1:
			A[i] = 0
			k -= 1
		if k >= 2:
			if A[i] == 1 and B[i] == 0:
				A[i] = 0
				B[i] = 1
				k -= 2
	AA = ''
	BB = ''
	for i in range(0, mx, 4):
		a = ''
		b = ''
		for j in range(4):
			a = a + str(A[i + j])
			b = b + str(B[i + j])
		a = d2[a]
		b = d2[b]
		if a == '0' and AA == '':
			a = ''
		if b == '0' and BB == '':
			b = ''
		AA += a
		BB += b
	if AA == '':
		AA = '0'
	if BB == '':
		BB = '0'
	print(AA)
	print(BB)


def to_bits(S, bits):
	A = [0] * bits
	i = bits - 4 * len(S)
	for c in S:
		j = int(c, 16)
		if j & 8:
			A[i] = 1
		if j & 4:
			A[i + 1] = 1
		if j & 2:
			A[i + 2] = 1
		if j & 1:
			A[i + 3] = 1
		i += 4
	return A

def print_hex(A):
	S = [''] * (len(A) // 4)
	S[-1] = '0'
	nonzero = 0
	for i in range(0, len(A), 4):
		d = A[i] << 3 | A[i + 1] << 2 | A[i + 2] << 1 | A[i + 3]
		if d or nonzero:
			S[i >> 2] = '%X' % d
			nonzero = 1
	print(''.join(S))
Q = int(input())
for q in range(Q):
	K = int(input())
	A = input().strip()
	B = input().strip()
	C = input().strip()
	bits = 4 * max(len(A), len(B), len(C))
	A = to_bits(A, bits)
	B = to_bits(B, bits)
	C = to_bits(C, bits)
	for i in range(bits):
		if C[i]:
			if A[i] == 0 and B[i] == 0:
				B[i] = 1
				K -= 1
		else:
			if A[i]:
				A[i] = 0
				K -= 1
			if B[i]:
				B[i] = 0
				K -= 1
	if K < 0:
		print(-1)
	else:
		for i in range(bits):
			if A[i]:
				if B[i]:
					if K >= 1:
						A[i] = 0
						K -= 1
				elif K >= 2:
					A[i] = 0
					B[i] = 1
					K -= 2
		print_hex(A)
		print_hex(B)


t = int(input())
for i in range(t):
	count = 0
	k = int(input())
	a = bin(int(input(), 16))
	b = bin(int(input(), 16))
	c = input()
	num = len(c) * 4
	c = bin(int(c, 16))[2:].zfill(num)
	a = list(a[2:].zfill(num))
	b = list(b[2:].zfill(num))
	for j in range(num):
		if c[j] == '0' and (a[j] == '1' and b[j] == '1'):
			count += 2
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '0' and (not (a[j] == '0' and b[j] == '0')):
			count += 1
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '1' and a[j] == '0' and (b[j] == '0'):
			count += 1
			b[j] = '1'
	if count == k:
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())
	elif count > k:
		print(-1)
	else:
		for j in range(num):
			if a[j] == '1' and b[j] == '1' and (count <= k - 1):
				count += 1
				a[j] = '0'
			elif a[j] == '1' and b[j] == '0' and (count <= k - 2):
				count += 2
				a[j] = '0'
				b[j] = '1'
			if count == k:
				break
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())


def fun(K, ABC):
	abc = []
	l = max([len(i) for i in ABC])
	for i in ABC:
		abc.append(list(bin(int(i, 16))[2:].zfill(4 * l)))
	for i in range(4 * l):
		if abc[0][i] == '0' and abc[1][i] == '0' and (abc[2][i] == '1'):
			abc[1][i] = '1'
			K -= 1
		if abc[0][i] == '0' and abc[1][i] == '1' and (abc[2][i] == '0'):
			abc[1][i] = '0'
			K -= 1
		if abc[0][i] == '1' and abc[1][i] == '0' and (abc[2][i] == '0'):
			abc[0][i] = '0'
			K -= 1
		if abc[0][i] == '1' and abc[1][i] == '1' and (abc[2][i] == '0'):
			abc[0][i] = '0'
			abc[1][i] = '0'
			K -= 2
		if K < 0:
			return -1
	if K >= 1:
		for i in range(4 * l):
			if abc[0][i] == '1' and abc[1][i] == '1' and (abc[2][i] == '1') and (K >= 1):
				abc[0][i] = '0'
				K -= 1
			if abc[0][i] == '1' and abc[1][i] == '0' and (abc[2][i] == '1') and (K >= 2):
				abc[0][i] = '0'
				abc[1][i] = '1'
				K -= 2
			if K < 1:
				break
	return [hex(int(''.join(abc[0]), 2))[2:].upper(), hex(int(''.join(abc[1]), 2))[2:].upper()]
for _ in range(int(input())):
	K = int(input())
	A = input()
	B = input()
	C = input()
	res = fun(K, [A, B, C])
	if res == -1:
		print(-1)
	else:
		for i in res:
			print(i)


t = int(input())
for i in range(t):
	count = 0
	k = int(input())
	a = bin(int(input(), 16))
	b = bin(int(input(), 16))
	c = input()
	num = len(c) * 4
	c = bin(int(c, 16))[2:].zfill(num)
	a = list(a[2:].zfill(num))
	b = list(b[2:].zfill(num))
	for j in range(num):
		if c[j] == '0' and (a[j] == '1' and b[j] == '1'):
			count += 2
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '0' and (not (a[j] == '0' and b[j] == '0')):
			count += 1
			a[j] = '0'
			b[j] = '0'
		elif c[j] == '1' and a[j] == '0' and (b[j] == '0'):
			count += 1
			b[j] = '1'
	if count == k:
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())
	elif count > k:
		print(-1)
	else:
		for j in range(num):
			if a[j] == '1' and b[j] == '1' and (count <= k - 1):
				count += 1
				a[j] = '0'
			elif a[j] == '1' and b[j] == '0' and (count <= k - 2):
				count += 2
				a[j] = '0'
				b[j] = '1'
			if count == k:
				break
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())


BITS = {'0': [0, 0, 0, 0], '1': [1, 0, 0, 0], '2': [0, 1, 0, 0], '3': [1, 1, 0, 0], '4': [0, 0, 1, 0], '5': [1, 0, 1, 0], '6': [0, 1, 1, 0], '7': [1, 1, 1, 0], '8': [0, 0, 0, 1], '9': [1, 0, 0, 1], 'A': [0, 1, 0, 1], 'B': [1, 1, 0, 1], 'C': [0, 0, 1, 1], 'D': [1, 0, 1, 1], 'E': [0, 1, 1, 1], 'F': [1, 1, 1, 1]}

def bitsof(x):
	B = []
	for c in x:
		B.append(BITS[c])
	C = []
	for D in reversed(B):
		C.extend(D)
	return C

def frombits(B):
	letters = []
	for i in range(len(B) // 4):
		letters.append('{:x}'.format(B[4 * i] + 2 * (B[4 * i + 1] + 2 * (B[4 * i + 2] + 2 * B[4 * i + 3]))))
	res = ''.join(reversed(letters))
	if res.count('0') != len(res):
		return res.upper().lstrip('0')
	else:
		return '0'

def atbit(s, *n):
	return [i[s] for i in n]

def force(A, B, C, s):
	(a, b, c) = atbit(s, A, B, C)
	if c == 0:
		A[s] = 0
		B[s] = 0
		return a + b
	elif c == 1 and a == b == 0:
		B[s] = 1
		return 1
	else:
		return 0

def aelim(A, B, C, s, l):
	(a, b, c) = atbit(s, A, B, C)
	if a == b == c == 1:
		if l >= 1:
			A[s] = 0
			return 1
		else:
			return 0
	elif a == c == 1:
		if l >= 2:
			B[s] = 1
			A[s] = 0
			return 2
		else:
			return 0
	else:
		return 0

def tosize(A, bits):
	while len(A) < bits:
		A.append(0)
Q = int(input())
for _ in range(Q):
	K = int(input())
	A = bitsof(input())
	B = bitsof(input())
	C = bitsof(input())
	bits = max(len(A), len(B), len(C))
	tosize(A, bits)
	tosize(B, bits)
	tosize(C, bits)
	r = 0
	for b in range(bits):
		r += force(A, B, C, b)
	if r > K:
		print(-1)
		continue
	left = K - r
	for b in range(bits - 1, -1, -1):
		left -= aelim(A, B, C, b, left)
	print(frombits(A))
	print(frombits(B))


def string_to_bits(s, total_bits):
	for _ in range(total_bits - 4 * len(s)):
		yield 0
	for c in s:
		bits = []
		number = int(c, base=16)
		for _ in range(4):
			bits.append(number % 2)
			number //= 2
		for bit in reversed(bits):
			yield bit

def group(n, seq):
	items = []
	for el in seq:
		items.append(el)
		if len(items) == n:
			yield tuple(items)
			items.clear()
	if len(items) > 0:
		yield items

def bits_to_chars(bits):
	still_zero = True
	for bit_group in group(4, bits):
		n = 0
		for bit in bit_group:
			n = n * 2 + bit
		if still_zero and n == 0:
			continue
		still_zero = False
		yield ('%X' % n)
	if still_zero:
		yield '0'

class NotSolvableError(Exception):
	pass

def process(a_bits, b_bits, c_bits, max_corrections, operations):
	corrections_left = max_corrections
	for operation in operations:
		for i in range(len(a_bits)):
			(corrections_left, a_bits[i], b_bits[i]) = operation(a_bits[i], b_bits[i], c_bits[i], corrections_left)

def strict_operation(a, b, c, corrections_left):

	def use_correction():
		nonlocal corrections_left
		if corrections_left == 0:
			raise NotSolvableError
		corrections_left -= 1
	if c == 0:
		if a == 1:
			a = 0
			use_correction()
		if b == 1:
			b = 0
			use_correction()
	elif (a, b, c) == (0, 0, 1):
		b = 1
		use_correction()
	return (corrections_left, a, b)

def minimize_operation(a, b, c, corrections_left):
	if (a, b, c) == (1, 0, 1) and corrections_left >= 2:
		(a, b) = (0, 1)
		corrections_left -= 2
	elif (a, b, c) == (1, 1, 1) and corrections_left >= 1:
		a = 0
		corrections_left -= 1
	return (corrections_left, a, b)

def main():
	Q = int(input().strip())
	for _ in range(Q):
		K = int(input().strip())
		strings = [input().strip() for _ in range(3)]
		max_bits = max(map(len, strings)) * 4
		(a_bits, b_bits, c_bits) = [list(string_to_bits(s, max_bits)) for s in strings]
		try:
			process(a_bits, b_bits, c_bits, K, [strict_operation, minimize_operation])
		except NotSolvableError:
			print('-1')
		else:
			print(''.join(bits_to_chars(a_bits)))
			print(''.join(bits_to_chars(b_bits)))
main()


def transform(a, b, c):
	changes = 0
	if c == '0':
		if a == '1':
			changes += 1
		if b == '1':
			changes += 1
		return ('0', '0', changes)
	if a == '1' or b == '1':
		return (a, b, 0)
	return ('0', '1', 1)
for _ in range(int(input())):
	k = int(input())
	(A, B, C) = [bin(int(input(), 16))[2:] for _ in range(3)]
	n = max(len(A), len(B), len(C))
	(A, B, C) = (A.zfill(n), B.zfill(n), C.zfill(n))
	(P, Q) = ([], [])
	for i in range(n):
		(p, q, x) = transform(A[i], B[i], C[i])
		P.append(p)
		Q.append(q)
		k -= x
		if k < 0:
			break
	if k < 0:
		print(-1)
		continue
	i = 0
	while k > 0 and i < n:
		if P[i] == '1' and Q[i] == '1':
			P[i] = '0'
			k -= 1
		elif P[i] == '1' and Q[i] == '0' and (k > 1):
			(P[i], Q[i]) = ('0', '1')
			k -= 2
		i += 1
	print(hex(int(''.join(P), 2))[2:].upper())
	print(hex(int(''.join(Q), 2))[2:].upper())


import math
Q = int(input())
for qi in range(Q):
	K = int(input())
	A = int(input(), 16)
	B = int(input(), 16)
	C = int(input(), 16)
	n = int(math.log(max([A, B, C]), 2)) + 1
	m = pow(2, n)
	notA = m - A - 1
	notB = m - B - 1
	notC = m - C - 1
	newA = A & C
	K -= bin(A - newA).count('1')
	A = newA
	newB = B & C
	K -= bin(B - newB).count('1')
	B = newB
	newB = B | C - A
	K -= bin(newB - B).count('1')
	B = newB
	if K < 0:
		print(-1)
		continue
	mask = m
	while K > 0 and mask > 1:
		mask >>= 1
		if mask & A & B:
			A ^= mask
			K -= 1
		elif mask & A and K > 1:
			A ^= mask
			B |= mask
			K -= 2
	print(hex(A)[2:].upper())
	print(hex(B)[2:].upper())


import sys

def solve(a, b, c, k, debug=0):
	if debug > 0:
		print('a=%s b=%s c=%s k=%d' % (a, b, c, k), file=sys.stderr)
	mx = max(len(a), len(b), len(c))
	a = '0' * (mx - len(a)) + a
	b = '0' * (mx - len(b)) + b
	c = '0' * (mx - len(c)) + c
	mp = {('0', '0', '0'): (0, '0', '0'), ('0', '0', '1'): (1, '0', '1'), ('0', '1', '0'): (1, '0', '0'), ('0', '1', '1'): (0, '0', '1'), ('1', '0', '0'): (1, '0', '0'), ('1', '0', '1'): (0, '1', '0'), ('1', '1', '0'): (2, '0', '0'), ('1', '1', '1'): (0, '1', '1')}
	cnt = 0
	(AA, BB, C) = ([], [], [])
	for (v1, v2, v3) in zip(a, b, c):
		(cc, ba, bb) = mp[v1, v2, v3]
		cnt += cc
		AA.append(ba)
		BB.append(bb)
		C.append(v3)
		if debug > 0:
			print('v1=%s v2=%s v3=%s cc=%d ba=%s bb=%s' % (v1, v2, v3, cc, ba, bb), file=sys.stderr)
		if cnt > k:
			return None
	mp = {('0', '0', '0'): (0, '0', '0'), ('0', '1', '1'): (0, '0', '1'), ('1', '0', '1'): (2, '0', '1'), ('1', '1', '1'): (1, '0', '1')}
	for (i, t) in enumerate(zip(AA, BB, C)):
		if cnt == k:
			break
		(cc, nba, nbb) = mp[t]
		if cnt + cc <= k:
			cnt += cc
			(AA[i], BB[i]) = (nba, nbb)
	(AA, BB) = (int(''.join(AA), 2), int(''.join(BB), 2))
	return (hex(AA)[2:], hex(BB)[2:])
t = int(input())

def hextobin(s):
	mp = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}
	t = []
	for c in s:
		t.append(mp[c])
	return ''.join(t)
for _ in range(t):
	k = int(input())
	a = hextobin(input())
	b = hextobin(input())
	c = hextobin(input())
	t = solve(a, b, c, k, 0)
	if t is None:
		print(-1)
	else:
		(A, B) = t
		print(A.upper())
		print(B.upper())


def read_int():
	return int(input().strip())

def read_string():
	return str(input().strip())
HEX_BIN = {'0': (0, 0, 0, 0), '1': (0, 0, 0, 1), '2': (0, 0, 1, 0), '3': (0, 0, 1, 1), '4': (0, 1, 0, 0), '5': (0, 1, 0, 1), '6': (0, 1, 1, 0), '7': (0, 1, 1, 1), '8': (1, 0, 0, 0), '9': (1, 0, 0, 1), 'A': (1, 0, 1, 0), 'B': (1, 0, 1, 1), 'C': (1, 1, 0, 0), 'D': (1, 1, 0, 1), 'E': (1, 1, 1, 0), 'F': (1, 1, 1, 1)}
BIN_HEX = {v: k for (k, v) in HEX_BIN.items()}

def get_binary_list(x):
	return list(HEX_BIN[x])

def compute_single_change(a, b, c):
	ab = get_binary_list(a)
	bb = get_binary_list(b)
	cb = get_binary_list(c)
	k = 0
	for i in range(4):
		if cb[i] == 0:
			if ab[i] == 1:
				ab[i] = 0
				k += 1
			if bb[i] == 1:
				bb[i] = 0
				k += 1
		if cb[i] == 1:
			if ab[i] == bb[i] == 0:
				bb[i] = 1
				k += 1
	ap = BIN_HEX[tuple(ab)]
	bp = BIN_HEX[tuple(bb)]
	return (k, ap, bp)

def compute_minimization(K, m, y, c):
	mb = get_binary_list(m)
	bb = get_binary_list(y)
	cb = get_binary_list(c)
	for i in range(4):
		if K == 0:
			break
		if cb[i] == 1:
			if mb[i] == bb[i] == 1:
				mb[i] = 0
				K -= 1
			if mb[i] == 1 and K >= 2:
				(mb[i], bb[i]) = (0, 1)
				K -= 2
	ap = BIN_HEX[tuple(mb)]
	bp = BIN_HEX[tuple(bb)]
	return (K, ap, bp)

def minimize_values(K, A, B, C):
	minimized = ''
	other = ''
	for (m, y, c) in zip(A, B, C):
		if K > 0:
			(K, mp, bp) = compute_minimization(K, m, y, c)
			minimized += mp
			other += bp
		else:
			minimized += m
			other += y
	return (minimized, other)

def compute_solution(K, A, B, C):
	Ap = Bp = ''
	for (a, b, c) in zip(A, B, C):
		(k, ap, bp) = compute_single_change(a, b, c)
		K -= k
		if K < 0:
			return [-1]
		Ap += ap
		Bp += bp
	if K > 0:
		(Ap, Bp) = minimize_values(K, Ap, Bp, C)
	return (Ap, Bp)

def normalize_lengths(S1, S2, S3):

	def pad(m, s):
		return '0' * (m - len(s)) + s
	ML = max(len(S1), len(S2), len(S3))
	return (pad(ML, S1), pad(ML, S2), pad(ML, S3))

def remove_trailing_zeroes(n):
	m = n.lstrip('0')
	if m == '':
		return '0'
	else:
		return m
Q = read_int()
for q in range(Q):
	K = read_int()
	A = read_string()
	B = read_string()
	C = read_string()
	(A, B, C) = normalize_lengths(A, B, C)
	sol = compute_solution(K, A, B, C)
	if len(sol) == 1:
		print(-1)
	else:
		(Ap, Bp) = sol
		Ap = remove_trailing_zeroes(Ap)
		Bp = remove_trailing_zeroes(Bp)
		print(Ap)
		print(Bp)


bn = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}
hx = {'0000': '0', '0001': '1', '0010': '2', '0011': '3', '0100': '4', '0101': '5', '0110': '6', '0111': '7', '1000': '8', '1001': '9', '1010': 'A', '1011': 'B', '1100': 'C', '1101': 'D', '1110': 'E', '1111': 'F'}
for each in range(int(input().strip())):
	k = int(input().strip())
	a = list(input().strip())
	b = list(input().strip())
	c = list(input().strip())
	ab = ''
	bb = ''
	cb = ''
	for i in range(max(len(c), len(b), len(a)) - 1, -1, -1):
		if len(a) > 0:
			A = a.pop()
		else:
			A = '0'
		if len(b) > 0:
			B = b.pop()
		else:
			B = '0'
		if len(c) > 0:
			C = c.pop()
		else:
			C = '0'
		ab = bn[A] + ab
		bb = bn[B] + bb
		cb = bn[C] + cb
	ab = list(ab)
	bb = list(bb)
	cb = list(cb)
	t = 0
	i = len(cb) - 1
	while i >= 0 and t <= k:
		if max(ab[i], bb[i]) != cb[i]:
			if cb[i] == '0':
				if ab[i] == '1':
					ab[i] = '0'
					t = t + 1
				if bb[i] == '1':
					bb[i] = '0'
					t = t + 1
			else:
				bb[i] = '1'
				t = t + 1
		i = i - 1
	if t > k:
		print(-1)
		continue
	l = k - t
	if l > 0:
		i = 0
		while l > 0 and i < len(cb):
			if cb[i] == '1':
				if bb[i] == '1':
					if ab[i] == '1':
						ab[i] = '0'
						l = l - 1
				elif l > 1:
					ab[i] = '0'
					bb[i] = '1'
					l = l - 2
			i = i + 1
	ax = ''
	bx = ''
	for i in range(len(ab) // 4):
		A = ''.join(ab[i * 4:(i + 1) * 4])
		B = ''.join(bb[i * 4:(i + 1) * 4])
		if hx[A] != '0' or len(ax) != 0:
			ax = ax + hx[A]
		if hx[B] != '0' or len(bx) != 0:
			bx = bx + hx[B]
	if len(ax) == 0:
		ax = '0'
	if len(bx) == 0:
		bx = '0'
	print(ax)
	print(bx)


import sys

def debug(*msg):
	print(*msg, file=sys.stderr)
for each_test in range(int(input())):
	k = int(input())
	a = list(input())
	b = list(input())
	c = list(input())
	for i in range(len(c)):
		ai = int(a[i], base=16)
		bi = int(b[i], base=16)
		ci = int(c[i], base=16)
		oi = ai | bi
		for j in range(4):
			mask = 2 ** j
			if oi & mask > ci & mask:
				if ai & mask > 0:
					ai -= mask
					k -= 1
				if bi & mask > 0:
					bi -= mask
					k -= 1
			elif oi & mask < ci & mask:
				bi += mask
				k -= 1
			if k < 0:
				break
		if k < 0:
			break
		a[i] = hex(ai).upper()[2:]
		b[i] = hex(bi).upper()[2:]
	for i in range(len(c)):
		if k <= 0:
			break
		ai = int(a[i], base=16)
		bi = int(b[i], base=16)
		ci = int(c[i], base=16)
		for j in reversed(range(4)):
			if k == 0:
				break
			mask = 2 ** j
			if ai & mask == bi & mask and ai & mask > 0:
				ai -= mask
				k -= 1
			elif ai & mask == ci & mask and ai & mask > 0 and (k > 1):
				ai -= mask
				bi += mask
				k -= 2
		a[i] = hex(ai).upper()[2:]
		b[i] = hex(bi).upper()[2:]
	if k < 0:
		print(-1)
	else:
		print(hex(int(''.join(a), base=16)).upper()[2:])
		print(hex(int(''.join(b), base=16)).upper()[2:])


Q = int(input().strip())
for a0 in range(Q):
	k = int(input())
	(a, b, c) = [input() for _ in range(3)]
	m = max((len(x) for x in (a, b, c)))
	a = '0' * (m - len(a)) + a
	b = '0' * (m - len(b)) + b
	c = '0' * (m - len(c)) + c
	ra = ''
	rb = ''
	for (da, db, dc) in zip(a, b, c):
		da = int(da, 16)
		db = int(db, 16)
		dc = int(dc, 16)
		diff = (da | db) ^ dc
		(d1, d2, d3) = (diff & da, diff & db, (diff ^ db) & diff & ~da)
		k -= bin(d1).count('1')
		k -= bin(d2).count('1')
		k -= bin(d3).count('1')
		if k < 0:
			print(-1)
			break
		ra = ra + '%X' % (da ^ d1)
		rb = rb + '%X' % (db ^ d2 ^ d3)
	else:
		ra2 = rb2 = ''
		for (da, db) in zip(ra, rb):
			p = 8
			da = int(da, 16)
			db = int(db, 16)
			while k and p:
				if da & p:
					if db & p:
						da -= p
						k -= 1
					elif k >= 2:
						da -= p
						db += p
						k -= 2
				p >>= 1
			if ra2 or da:
				ra2 = ra2 + '%X' % da
			if rb2 or db:
				rb2 = rb2 + '%X' % db
		print(ra2 or '0')
		print(rb2 or '0')


for t in range(int(input())):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	if len(a) > len(b):
		b = '0' * (len(a) - len(b)) + b
	else:
		a = '0' * (len(b) - len(a)) + a
	if len(a) > len(c):
		c = '0' * (len(a) - len(c)) + c
	else:
		a = '0' * (len(c) - len(a)) + a
		b = '0' * (len(c) - len(b)) + b
	a = list(a)
	b = list(b)
	c = list(c)
	cantBeDone = False
	i = 0
	while k > 0 and i < len(c):
		if c[i] == '0':
			if a[i] == '1' and b[i] == '1':
				if k - 2 >= 0:
					a[i] = b[i] = '0'
					k -= 2
				else:
					cantBeDone = True
					break
			elif not (a[i] == b[i] and a[i] == '0'):
				a[i] = b[i] = '0'
				k -= 1
		elif a[i] == '0' and b[i] == '0':
			b[i] = '1'
			k -= 1
		i += 1
	i = 0
	while k > 0 and i < len(a):
		if c[i] == '1':
			if a[i] == '1' and b[i] == '1':
				a[i] = '0'
				k -= 1
			elif a[i] == '1' and b[i] == '0':
				if k - 2 >= 0:
					a[i] = '0'
					b[i] = '1'
					k -= 2
		i += 1
	if int(''.join(a), 2) | int(''.join(b), 2) != int(''.join(c), 2):
		print('-1')
	else:
		print(hex(int(''.join(a), 2))[2:].upper())
		print(hex(int(''.join(b), 2))[2:].upper())


def solve(K, A, B, C):
	a = list(bin(A)[2:])
	b = list(bin(B)[2:])
	c = list(bin(C)[2:])
	l = max(map(len, (a, b, c)))
	a = ['0'] * (l - len(a)) + a
	b = ['0'] * (l - len(b)) + b
	c = ['0'] * (l - len(c)) + c
	for i in range(len(c)):
		if c[i] == '0':
			if a[i] != '0':
				K -= 1
				a[i] = '0'
			if b[i] != '0':
				K -= 1
				b[i] = '0'
		elif a[i] == '0' and b[i] == '0':
			K -= 1
			b[i] = '1'
	if K < 0:
		return (-1,)
	i = 0
	while K > 0 and i < len(a):
		if a[i] == '1':
			if b[i] == '0':
				if K >= 2:
					a[i] = '0'
					b[i] = '1'
					K -= 2
			else:
				a[i] = '0'
				K -= 1
		i += 1
	return (hex(int(''.join(a), 2))[2:].upper(), hex(int(''.join(b), 2))[2:].upper())
Q = int(input())
for _ in range(Q):
	K = int(input())
	A = int(input(), 16)
	B = int(input(), 16)
	C = int(input(), 16)
	print('\n'.join(map(str, solve(K, A, B, C))))


import math

def count1bits(num):
	count = 0
	while num > 0:
		maxBitPos = int(math.log2(num))
		mask = 1 << maxBitPos
		num = num ^ mask
		count += 1
	return count
t = int(input())
for i in range(t):
	k = int(input())
	a = int(input(), 16)
	b = int(input(), 16)
	c = int(input(), 16)
	unsetA = ~c & a
	unsetB = ~c & b
	setAorB = ~a & ~b & c
	optionalUnsetA = a & c
	stepCount = count1bits(unsetA) + count1bits(unsetB) + count1bits(setAorB)
	if stepCount > k:
		print(-1)
	else:
		aa = a ^ unsetA
		bb = b ^ unsetB | setAorB
		while stepCount < k and optionalUnsetA > 0:
			maxBitPos = int(math.log2(optionalUnsetA))
			mask = 1 << maxBitPos
			tmpIsAbitSet = aa & mask != 0
			tmpIsBbitSet = bb & mask != 0
			if tmpIsAbitSet and tmpIsBbitSet and (stepCount + 1 <= k):
				aa = aa ^ mask
				stepCount += 1
			elif tmpIsAbitSet and (not tmpIsBbitSet) and (stepCount + 2 <= k):
				aa = aa ^ mask
				bb = bb | mask
				stepCount += 2
			optionalUnsetA = optionalUnsetA ^ mask
		print(hex(aa)[2:].upper())
		print(hex(bb)[2:].upper())


def calculate(A, B, C, k):
	n = max(len(A), len(B), len(C))
	(A, B, C) = ([0] * (n - len(x)) + x for x in (A, B, C))
	for (i, a, b, c) in zip(range(n), A, B, C):
		if a | b < c:
			B[i] = 1
			k -= 1
		elif a | b > c:
			k -= a + b
			A[i] = B[i] = 0
		if k < 0:
			return [-1]
	for (i, a, b) in zip(range(n), A, B):
		if a > b and k >= 2:
			(A[i], B[i]) = (0, 1)
			k -= 2
		elif a >= b >= 1 and k:
			A[i] = 0
			k -= 1
	(A, B) = (hex(int(''.join(map(str, x)), 2))[2:].upper() for x in (A, B))
	return (A, B)
q = int(input())
for i in range(q):
	k = int(input())
	(a, b, c) = (list(map(int, bin(int(input(), 16))[2:])) for i in range(3))
	print(*calculate(a, b, c, k), sep='\n')


for _ in range(int(input())):
	K = int(input())
	A = int(input(), 16)
	B = int(input(), 16)
	C = int(input(), 16)
	liczba = bin((A | B) ^ C).count('1') + bin(A & B).count('1') - bin(A & B & C).count('1')
	if liczba > K:
		print(-1)
	else:
		X = (A | B) ^ C
		Y = X & C
		dodaj = Y
		usun = X ^ Y
		Bn = B ^ dodaj
		ldodaj = bin(dodaj).count('1')
		lusun = 0
		usunA = A & usun
		An = A ^ usunA
		lusun += bin(usunA).count('1')
		usunB = B & usun
		Bn = Bn ^ usunB
		lusun += bin(usunB).count('1')
		zmiany = ldodaj + lusun
		if zmiany < K:
			x = K - zmiany
			An = list(bin(An))
			An.reverse()
			Bn = list(bin(Bn))
			Bn.reverse()
			An.pop(-1)
			An.pop(-1)
			Bn.pop(-1)
			Bn.pop(-1)
			if len(An) < len(Bn):
				An.extend(['0' for x in range(len(Bn) - len(An))])
			elif len(Bn) < len(An):
				Bn.extend(['0' for x in range(len(An) - len(Bn))])
			An.reverse()
			Bn.reverse()
			while x:
				for i in range(len(An)):
					if An[i] == Bn[i] and An[i] == '1':
						An[i] = '0'
						x -= 1
						if x == 0:
							break
					elif An[i] == '1' and Bn[i] == '0' and (x >= 2):
						An[i] = '0'
						Bn[i] = '1'
						x -= 2
						if x == 0:
							break
				else:
					x = 0
			An = int(''.join(An), 2)
			Bn = int(''.join(Bn), 2)
		print('{0:x}'.format(An).upper())
		print('{0:x}'.format(Bn).upper())


import sys
input = sys.stdin.readline
from itertools import chain

def fit(l):
	return lambda x: x + ['0'] * (l - len(x))

def hex_to_list(x):
	return list(chain.from_iterable(([bool(n & 1 << i) for i in range(4)] for n in [int(xx, 16) for xx in reversed(x)])))

def list_to_hex(l):
	return ''.join((hex(sum((1 << i for (i, b) in enumerate(l[i:i + 4]) if b)))[2:].upper() for i in range(0, len(l), 4)))[::-1].lstrip('0') or '0'

def main(k, a, b, c):
	(a, b, c) = map(hex_to_list, (a, b, c))
	n = max(len(a), len(b), len(c))
	(a, b, c) = map(fit(n), (a, b, c))
	for i in range(n):
		if c[i]:
			if not a[i] and (not b[i]):
				k -= 1
				b[i] = True
		else:
			if a[i]:
				a[i] = False
				k -= 1
			if b[i]:
				b[i] = False
				k -= 1
	if k < 0:
		return (-1,)
	for i in reversed(range(n)):
		if not k:
			break
		if a[i]:
			if b[i]:
				a[i] = False
				k -= 1
			elif k >= 2:
				a[i] = False
				b[i] = True
				k -= 2
	return map(list_to_hex, (a, b))
for _ in range(int(input())):
	print(*main(*[int(input())] + [input().strip() for __ in range(3)]), sep='\n')


for _ in range(int(input())):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	nbits = max(len(a), len(b), len(c))
	a = '0' * (nbits - len(a)) + a
	b = '0' * (nbits - len(b)) + b
	c = '0' * (nbits - len(c)) + c
	a = [ord(x) - 48 for x in a]
	b = [ord(x) - 48 for x in b]
	c = [ord(x) - 48 for x in c]
	req = 0
	for i in range(nbits):
		if a[i] == 1 and c[i] == 0:
			req += 1
			a[i] = 0
		if b[i] == 1 and c[i] == 0:
			req += 1
			b[i] = 0
		if a[i] == 0 and b[i] == 0 and (c[i] == 1):
			req += 1
			b[i] = 1
	if req <= k:
		rem = k - req
		i = 0
		p = 0
		while i < nbits and p < rem:
			if a[i] == 1 and b[i] == 1:
				a[i] = 0
				p += 1
			elif a[i] == 1 and b[i] == 0 and (p + 2 <= rem):
				a[i] = 0
				b[i] = 1
				p += 2
			i += 1
		s1 = ''.join([str(x) for x in a])
		s2 = ''.join([str(x) for x in b])
		s1 = hex(int(s1, 2))[2:].upper()
		s2 = hex(int(s2, 2))[2:].upper()
		print(s1)
		print(s2)
	else:
		print(-1)


import re

def binary_to_hex(bin_arr):
	hex_val = {i: '{:X}'.format(i) for i in range(16)}
	val = 0
	hex_arr = []
	stripped = re.sub('^(0000)*', '', ''.join(bin_arr))
	for (i, c) in enumerate(stripped):
		if c == '1':
			val += 2 ** (3 - i % 4)
		if i % 4 == 3:
			hex_arr.append(hex_val[val])
			val = 0
	return ''.join(hex_arr) or '0'

def hex_to_binary(hex_str, required_size):
	hex_val = {'{:X}'.format(i): list(bin(i)[2:].zfill(4)) for i in range(16)}
	bin_arr = ['0'] * (required_size - len(hex_str) * 4)
	for c in hex_str:
		bin_arr.extend(hex_val[c])
	return bin_arr

def solve(a, b, c, k):
	most_bits = len(max([a, b, c], key=len)) * 4
	(a_str, b_str, c_str) = [hex_to_binary(x, most_bits) for x in [a, b, c]]
	changes_required = 0
	for i in range(most_bits):
		if c_str[i] == '0':
			if a_str[i] == '1':
				a_str[i] = '0'
				changes_required += 1
			if b_str[i] == '1':
				b_str[i] = '0'
				changes_required += 1
		elif c_str[i] == '1':
			if a_str[i] == '0' and b_str[i] == '0':
				b_str[i] = '1'
				changes_required += 1
	if changes_required > k:
		print(-1)
		return
	for i in range(most_bits):
		if changes_required == k:
			break
		if c_str[i] == '1' and a_str[i] == '1':
			if changes_required + 2 <= k and b_str[i] == '0':
				a_str[i] = '0'
				b_str[i] = '1'
				changes_required += 2
			elif b_str[i] == '1':
				a_str[i] = '0'
				changes_required += 1
	print(binary_to_hex(a_str))
	print(binary_to_hex(b_str))
q = int(input())
for _ in range(q):
	k = int(input())
	(a, b, c) = [input().strip() for _ in range(3)]
	solve(a, b, c, k)


q = int(input())
for qq in range(q):
	k = int(input())
	a = list(bin(int(input(), 16))[2:].zfill(16 ** 5))
	b = list(bin(int(input(), 16))[2:].zfill(16 ** 5))
	c = list(bin(int(input(), 16))[2:].zfill(16 ** 5))
	notPossible = False
	for i in range(len(a)):
		if a[i] == '1' and c[i] == '0':
			if k > 0:
				a[i] = '0'
				k -= 1
			else:
				notPossible = True
				break
	if notPossible:
		print(-1)
		continue
	for i in range(len(b)):
		if int(a[i]) | int(b[i]) != int(c[i]):
			if k > 0:
				b[i] = str(int(b[i]) ^ 1)
				k -= 1
			else:
				notPossible = True
	if notPossible:
		print(-1)
		continue
	for i in range(len(a)):
		if k > 0:
			if int(a[i]) & int(b[i]) == 1:
				a[i] = '0'
				k -= 1
			if k > 1 and (a[i] == '1' and b[i] == '0'):
				a[i] = str(int(a[i]) ^ 1)
				b[i] = str(int(b[i]) ^ 1)
				k -= 2
		else:
			break
	print(hex(int(''.join(a), 2))[2:].upper())
	print(hex(int(''.join(b), 2))[2:].upper())


for x in range(int(input())):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	mv = max(len(a), len(b), len(c))
	while len(a) < mv:
		a = '0' + a
	while len(b) < mv:
		b = '0' + b
	while len(c) < mv:
		c = '0' + c
	a = list(a)
	b = list(b)
	c = list(c)
	for x in range(mv):
		if c[x] == '0':
			if a[x] != '0':
				a[x] = '0'
				k -= 1
			if b[x] != '0':
				b[x] = '0'
				k -= 1
		if c[x] == '1' and a[x] == '0' and (b[x] == '0'):
			b[x] = '1'
			k -= 1
	if k < 0:
		print(-1)
	else:
		for x in range(mv):
			if a[x] == '1' and b[x] == '1' and (k > 0):
				a[x] = '0'
				k -= 1
			if a[x] == '1' and b[x] == '0' and (k > 1):
				a[x] = '0'
				b[x] = '1'
				k -= 2
		print(hex(int(''.join(a), 2)).upper()[2:])
		print(hex(int(''.join(b), 2)).upper()[2:])


def reduceab(k, a, b, c):
	bitca = bitc(a)
	reserved = 0
	while bitca > 0 and k > 0:
		mod = 1 << a.bit_length() - 1
		a ^= mod
		k -= 1
		bitca -= 1
		if mod & b == 0:
			if k > 0:
				b |= mod
				k -= 1
			else:
				reserved |= mod
				k += 1
	a |= reserved
	return (a, b)

def do_the_shit(k, a, b, c):
	killa = ~c & a
	killb = ~c & b
	addb = c & ~(a | b)
	k -= sum(map(bitc, (killa, killb, addb)))
	a ^= killa
	b ^= killb
	b |= addb
	if k < 0:
		return (0, 0)
	else:
		(a, b) = reduceab(k, a, b, c)
		return (a, b)

def func():
	return input()

def q(x):
	return '{0:X}'.format(x)

def bitc(x):
	return bin(x)[2:].count('1')
tc = range(int(func().strip()))
for _ in tc:
	kk = int(func().strip())
	aao = int(func().strip(), 16)
	bbo = int(func().strip(), 16)
	cc = int(func().strip(), 16)
	(aa, bb) = do_the_shit(kk, aao, bbo, cc)
	if aa or bb:
		print(q(aa))
		print(q(bb))
	else:
		print(-1)


SOLUTION_PER_BIT = [[[[True, 0, [0, 0]], [True, 1, [0, 1]]], [[True, 1, [0, 0]], [True, 0, [0, 1]]]], [[[True, 1, [0, 0]], [False, 2, [0, 1], [1, 0]]], [[True, 2, [0, 0]], [False, 1, [0, 1], [1, 1]]]]]

def solve(K, A, B, C):
	tentative_sol = []
	tentative_K = 0
	mandatory_K = 0
	for (na, nb, nc) in zip(A, B, C):
		for mask in [8, 4, 2, 1]:
			ba = int(not not int(na, 16) & mask)
			bb = int(not not int(nb, 16) & mask)
			bc = int(not not int(nc, 16) & mask)
			s = SOLUTION_PER_BIT[ba][bb][bc]
			tentative_sol.append(s)
			tentative_K += s[1]
			if s[0]:
				mandatory_K += s[1]
	if mandatory_K > K:
		print(-1)
		return
	i = len(tentative_sol) - 1
	while i >= 0 and tentative_K > K:
		s = tentative_sol[i]
		if not s[0]:
			tentative_K -= s[1]
			s = list(s)
			del s[2]
			tentative_sol[i] = s
		i -= 1
	if tentative_K < K:
		i += 2
		for x in tentative_sol[i:]:
			if x[2] == [1, 1]:
				x[2] = [0, 1]
				break
	Ap = 0
	Bp = 0
	for x in tentative_sol:
		Ap = Ap << 1 | x[2][0]
		Bp = Bp << 1 | x[2][1]
	print('{0:X}\n{1:X}'.format(Ap, Bp))
nTestCases = int(input().strip())
for _ in range(nTestCases):
	K = int(input().strip())
	(A, B, C) = (input() for i in [1, 2, 3])
	if len(A) != len(B) or len(B) != len(C):
		raise ValueError('A, B, C must have the same length')
	solve(K, A, B, C)


import sys

def first_pass(a, b, c, k):
	i = 0
	while i < len(a):
		if a[i] is '0' and b[i] is '1' and (c[i] is '0'):
			k -= 1
			if k < 0:
				return -1
			b[i] = '0'
		elif a[i] is '1' and b[i] is '0' and (c[i] is '0'):
			k -= 1
			if k < 0:
				return -1
			a[i] = '0'
		elif a[i] is '1' and b[i] is '1' and (c[i] is '0'):
			k -= 2
			if k < 0:
				return -1
			a[i] = '0'
			b[i] = '0'
		elif a[i] is '0' and b[i] is '0' and (c[i] is '1'):
			k -= 1
			if k < 0:
				return -1
			b[i] = '1'
		i += 1
	return k

def second_pass(a, b, c, k):
	i = 0
	while i < len(a) and k > 0:
		if a[i] is '1' and b[i] is '0' and (c[i] is '1'):
			if k >= 2:
				k -= 2
				a[i] = '0'
				b[i] = '1'
		elif a[i] is '1' and b[i] is '1' and (c[i] is '1'):
			k -= 1
			a[i] = '0'
		i += 1
	return k
lines = []
for line in sys.stdin:
	lines.append(line)
numberOfCase = int(lines.pop(0))
while numberOfCase > 0:
	numberOfCase -= 1
	K = int(lines.pop(0))
	A = bin(int(lines.pop(0), 16))[2:]
	B = bin(int(lines.pop(0), 16))[2:]
	C = bin(int(lines.pop(0), 16))[2:]
	max_len = max([len(A), len(B), len(C)])
	A = A.zfill(max_len)
	B = B.zfill(max_len)
	C = C.zfill(max_len)
	A = list(A)
	B = list(B)
	C = list(C)
	K = first_pass(A, B, C, K)
	if K is -1:
		print('-1')
		continue
	K = second_pass(A, B, C, K)
	print(hex(int(''.join(A), 2))[2:].upper())
	print(hex(int(''.join(B), 2))[2:].upper())


def first_pass(A, B, C, k):
	Ad = ''
	Bd = ''
	cnt = 0
	for (i, (a, b, c)) in enumerate(zip(A, B, C)):
		if int(a) | int(b) != int(c):
			if c == '0':
				Ad += '0'
				Bd += '0'
				if a == b == '1':
					cnt += 2
				else:
					cnt += 1
			else:
				Ad += '0'
				Bd += '1'
				cnt += 1
			if cnt > k:
				return (-1, -1, -1)
		else:
			Ad += a
			Bd += b
	return (Ad, Bd, cnt)

def second_pass(A, B, C, cnt):
	(Ad, Bd) = (list(A), list(B))
	for (i, (a, b, c)) in enumerate(zip(A, B, C)):
		if c == '1':
			if a == b == '1':
				Ad[i] = '0'
				cnt += -1
			elif a == '1' and b == '0' and (cnt > 1):
				Ad[i] = '0'
				Bd[i] = '1'
				cnt += -2
		if cnt == 0:
			break
		elif cnt < 0:
			print('Ahhh Poop')
	return (''.join(Ad), ''.join(Bd))
for _ in range(int(input())):
	k = int(input())
	A = bin(int(input(), 16))[2:]
	B = bin(int(input(), 16))[2:]
	C = bin(int(input(), 16))[2:]
	maxl = max(len(A), len(B), len(C))
	A = A.rjust(maxl, '0')
	B = B.rjust(maxl, '0')
	C = C.rjust(maxl, '0')
	cnt = 0
	(Ad, Bd, cnt) = first_pass(A, B, C, k)
	if Ad == Bd == -1:
		print(-1)
	else:
		cnt = k - cnt
		if cnt:
			(Ad, Bd) = second_pass(Ad, Bd, C, cnt)
		print(hex(int(Ad, 2))[2:].upper())
		print(hex(int(Bd, 2))[2:].upper())


def change_the_bits_and_print(A, B, C, k):
	L = max(A, B, C).bit_length()
	one = '1'
	zero = '0'
	bA = '0' * (L - A.bit_length()) + bin(A)[2:]
	bB = '0' * (L - B.bit_length()) + bin(B)[2:]
	bC = '0' * (L - C.bit_length()) + bin(C)[2:]
	A_p = []
	B_p = []
	bits_changed_successfully = True
	for i in range(len(bC)):
		if bC[i] == one:
			if bA[i] == one or bB[i] == one:
				A_p.append(bA[i])
				B_p.append(bB[i])
			else:
				B_p.append(one)
				A_p.append(zero)
				k -= 1
		else:
			A_p.append(zero)
			k -= bA[i] != zero
			B_p.append(zero)
			k -= bB[i] != zero
		if k < 0:
			bits_changed_successfully = False
			break
	if bits_changed_successfully:
		if k > 0:
			for i in range(len(bC)):
				if bC[i] == one:
					if A_p[i] == B_p[i]:
						A_p[i] = zero
						k -= 1
					elif A_p[i] > B_p[i] and k > 1:
						(A_p[i], B_p[i]) = (B_p[i], A_p[i])
						k -= 2
				if k == 0:
					break
		print(hex(int(''.join(A_p), 2))[2:].upper())
		print(hex(int(''.join(B_p), 2))[2:].upper())
	else:
		print(-1)
queries = int(input().strip())
for _i in range(queries):
	k = int(input().strip())
	A = int(input().strip(), 16)
	B = int(input().strip(), 16)
	C = int(input().strip(), 16)
	change_the_bits_and_print(A, B, C, k)


def solve(a, b, c, k):
	a = '{0:b}'.format(int(a, 16))
	b = '{0:b}'.format(int(b, 16))
	c = '{0:b}'.format(int(c, 16))
	l = max(len(a), len(b), len(c))
	a = '0' * (l - len(a)) + a
	b = '0' * (l - len(b)) + b
	c = '0' * (l - len(c)) + c
	a = [int(x) for x in a]
	b = [int(x) for x in b]
	c = [int(x) for x in c]
	for i in range(l - 1, -1, -1):
		if a[i] | b[i] != c[i]:
			if k <= 0:
				print(-1)
				return
			if c[i] == 1:
				b[i] = 1
				k -= 1
				continue
			else:
				if a[i] == 1:
					k -= 1
					a[i] = 0
				if b[i] == 1:
					k -= 1
					b[i] = 0
	for i in range(l):
		if (b[i] == 0) & (a[i] == c[i] == 1):
			if k >= 2:
				b[i] = 1
				a[i] = 0
				k -= 2
		if a[i] & b[i] == c[i] == 1:
			if k >= 1:
				a[i] = 0
				k -= 1
		if k <= 0:
			break
	a = int(''.join(map(str, a)), 2)
	b = int(''.join(map(str, b)), 2)
	a = hex(a)
	b = hex(b)
	print(a[2:].upper())
	print(b[2:].upper())
T = int(input())
for t0 in range(T):
	k = int(input())
	a = input()
	b = input()
	c = input()
	solve(a, b, c, k)


def c2i(c):
	n = ord(c) - 48
	return (n, n - 7)[n > 9]

def i2c(n):
	code = (n + 55, n + 48)[n < 10]
	return chr(code)

def getbit(s, ibyte, ibit):
	return ibyte < len(s) and s[ibyte] & ibit or 0
Q = int(input())
for _ in range(Q):
	K = int(input())
	sa = input()
	sb = input()
	sc = input()
	slen = max(len(sa), len(sb), len(sc))
	A = list(map(c2i, sa.zfill(slen)))
	B = list(map(c2i, sb.zfill(slen)))
	C = list(map(c2i, sc.zfill(slen)))
	mlen = max(len(A), len(B), len(C))
	(ibyte, ibit) = (mlen - 1, 1)
	for b in range(mlen * 4):
		bA = getbit(A, ibyte, ibit)
		bB = getbit(B, ibyte, ibit)
		bC = getbit(C, ibyte, ibit)
		if not bC:
			if bA:
				A[ibyte] &= ~ibit
				K -= 1
			if bB:
				B[ibyte] &= ~ibit
				K -= 1
		elif not bA and (not bB):
			B[ibyte] |= ibit
			K -= 1
		if K < 0:
			break
		ibit <<= 1
		if ibit > 8:
			ibyte -= 1
			ibit = 1
	(ibyte, ibit) = (0, 8)
	while K > 0 and ibit and (ibyte < mlen):
		bA = getbit(A, ibyte, ibit)
		bB = getbit(B, ibyte, ibit)
		bC = getbit(C, ibyte, ibit)
		if bC:
			if bA and bB:
				A[ibyte] &= ~ibit
				K -= 1
			elif bA and (not bB) and (K > 1):
				A[ibyte] &= ~ibit
				B[ibyte] |= ibit
				K -= 2
		ibit >>= 1
		if not ibit:
			ibyte += 1
			ibit = 8
	if K < 0:
		print(-1)
	else:
		sa = ''.join(map(i2c, A)).lstrip('0')
		sb = ''.join(map(i2c, B)).lstrip('0')
		if len(sa) == 0:
			sa = '0'
		if len(sb) == 0:
			sb = '0'
		print(sa)
		print(sb)


q = int(input())
for _ in range(q):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	maxlen = max(map(len, (a, b, c)))
	a = a.zfill(maxlen)
	b = b.zfill(maxlen)
	c = c.zfill(maxlen)
	best_a = ''
	best_b = ''
	changes = 0
	for (a_bit, b_bit, c_bit) in zip(a, b, c):
		if c_bit == '0':
			best_a += '0'
			best_b += '0'
			changes += 1 if a_bit != '0' else 0
			changes += 1 if b_bit != '0' else 0
		elif a_bit == '1' or b_bit == '1':
			best_a += a_bit
			best_b += b_bit
		else:
			best_a += '0'
			best_b += '1'
			changes += 1
	if changes > k:
		print(-1)
	else:
		for (i, (a_bit, b_bit, c_bit)) in enumerate(zip(a, b, c)):
			if c_bit == '1':
				if changes < k and a_bit == '1' and (b_bit == '1'):
					best_a = best_a[:i] + '0' + best_a[i + 1:]
					best_b = best_b[:i] + '1' + best_b[i + 1:]
					changes += 1
				elif changes < k - 1 and a_bit == '1' and (b_bit == '0'):
					best_a = best_a[:i] + '0' + best_a[i + 1:]
					best_b = best_b[:i] + '1' + best_b[i + 1:]
					changes += 2
		real_changes = sum((1 if a != b else 0 for (a, b) in zip(a, best_a)))
		real_changes += sum((1 if a != b else 0 for (a, b) in zip(b, best_b)))
		assert changes == real_changes
		print(hex(int(best_a, 2))[2:].upper())
		print(hex(int(best_b, 2))[2:].upper())


def adjust(a, b, c, k):
	for i in range(4):
		if c & 1 << i and (not (a | b) & 1 << i):
			b |= 1 << i
			k -= 1
		elif not c & 1 << i:
			if a & 1 << i:
				a ^= 1 << i
				k -= 1
			if b & 1 << i:
				b ^= 1 << i
				k -= 1
	return (a, b, k) if k >= 0 else None

def make_small(a, b, k):
	for i in range(3, -1, -1):
		if (a | b) & 1 << i:
			if k >= 1 and a & b & 1 << i:
				a ^= 1 << i
				k -= 1
			elif k >= 2 and a & 1 << i:
				a ^= 1 << i
				b ^= 1 << i
				k -= 2
	return (a, b, k)

def fh(c):
	return int(c, 16)

def th(v):
	if 0 <= v <= 9:
		return chr(v + ord('0'))
	if v == 10:
		return 'A'
	if v == 11:
		return 'B'
	if v == 12:
		return 'C'
	if v == 13:
		return 'D'
	if v == 14:
		return 'E'
	if v == 15:
		return 'F'
q = int(input().strip())
for _ in range(q):
	k = int(input().strip())
	a = input().strip()
	b = input().strip()
	c = input().strip()
	max_len = max(len(a), len(b), len(c))
	a = '0' * (max_len - len(a)) + a
	b = '0' * (max_len - len(b)) + b
	c = '0' * (max_len - len(c)) + c
	new_a = [0 for _ in range(max_len)]
	new_b = [0 for _ in range(max_len)]
	for i in range(max_len):
		ret = adjust(fh(a[i]), fh(b[i]), fh(c[i]), k)
		if ret is None:
			print(-1)
			break
		(new_a[i], new_b[i], k) = ret
	else:
		i = 0
		while k and i < max_len:
			(new_a[i], new_b[i], k) = make_small(new_a[i], new_b[i], k)
			i += 1
		print(''.join(map(th, new_a)).lstrip('0') or '0')
		print(''.join(map(th, new_b)).lstrip('0') or '0')


q = int(input())
for t in range(q):
	k = int(input())
	a = bin(int(input(), 16))[2:]
	b = bin(int(input(), 16))[2:]
	c = bin(int(input(), 16))[2:]
	l = max(len(a), len(b), len(c))
	a = (l - len(a)) * '0' + a
	b = (l - len(b)) * '0' + b
	c = (l - len(c)) * '0' + c
	a_new = ''
	b_new = ''
	for i in range(l):
		if c[i] == '0':
			if a[i] == '1':
				if k >= 1:
					k -= 1
				else:
					print(-1)
					break
			a_new += '0'
			if b[i] == '1':
				if k >= 1:
					k -= 1
				else:
					print(-1)
					break
			b_new += '0'
		elif a[i] == '0' and b[i] == '0':
			if k >= 1:
				k -= 1
			else:
				print(-1)
				break
			a_new += '0'
			b_new += '1'
		else:
			a_new += a[i]
			b_new += b[i]
	else:
		for i in range(l):
			if k == 0:
				break
			if (a[i], b[i], c[i]) == ('1', '0', '1'):
				if k >= 2:
					k -= 2
					if i != l - 1:
						a_new = a_new[:i] + '0' + a_new[i + 1:]
						b_new = b_new[:i] + '1' + b_new[i + 1:]
					else:
						a_new = a_new[:i] + '0'
						b_new = b_new[:i] + '1'
			elif (a[i], b[i], c[i]) == ('1', '1', '1'):
				if k >= 1:
					k -= 1
					if i != l - 1:
						a_new = a_new[:i] + '0' + a_new[i + 1:]
					else:
						a_new = a_new[:i] + '0'
		print(hex(int(a_new, 2))[2:].upper())
		print(hex(int(b_new, 2))[2:].upper())


def hex_to_bin(hex_str):
	hi = int(hex_str, 16)
	return map(int, bin(hi)[2:].zfill(4))

def bin_to_hex(bin_):
	bi = int(''.join(map(str, bin_)), 2)
	return hex(bi)[2:].upper()

def edit_distance(A, B, C):
	edit_scheme = {(0, 0, 0): 0, (0, 0, 1): 1, (0, 1, 0): 1, (0, 1, 1): 0, (1, 0, 0): 1, (1, 0, 1): 0, (1, 1, 0): 2, (1, 1, 1): 0}
	edits = 0
	for hex_words in zip(A, B, C):
		for (a, b, c) in zip(*map(hex_to_bin, hex_words)):
			edits += edit_scheme[a, b, c]
	return edits

def aorbify(A, B, C, K):
	A = A.zfill(len(C))
	B = B.zfill(len(C))
	opt_edit_scheme = {(1, 1, 1): 1, (1, 0, 1): 2}
	opt_edits = K - edit_distance(A, B, C)
	if opt_edits < 0:
		print(-1)
		return
	(edA, edB) = ([], [])
	for (i, hex_chars) in enumerate(zip(A, B, C)):
		(binA, binB) = ([], [])
		for (a, b, c) in zip(*map(hex_to_bin, hex_chars)):
			opt_cost = opt_edit_scheme.get((a, b, c), 0)
			edit_flag = opt_edits >= opt_cost > 0
			opt_edits -= edit_flag and opt_cost
			binA.append((edit_flag ^ 1) & a & c)
			binB.append(c if edit_flag else (a ^ 1) & c | a & b & c)
		edA.append(bin_to_hex(binA))
		edB.append(bin_to_hex(binB))
	outA = ''.join(edA).lstrip('0')
	outB = ''.join(edB).lstrip('0')
	print(outA if outA else 0)
	print(outB if outB else 0)
test_cases = int(input())
for i in range(test_cases):
	(K, A, B, C) = (int(input()), input(), input(), input())
	aorbify(A, B, C, K)


for _ in range(int(input())):
	k = int(input())
	A = '{0:b}'.format(int(input(), 16))
	B = '{0:b}'.format(int(input(), 16))
	C = '{0:b}'.format(int(input(), 16))
	l = max([len(A), len(B), len(C)])
	A = '0' * (l - len(A)) + A
	B = '0' * (l - len(B)) + B
	C = '0' * (l - len(C)) + C
	a = []
	b = []
	c = 0
	for i in range(l):
		if C[i] == '0':
			a.append('0')
			b.append('0')
			c += A[i] == '1'
			c += B[i] == '1'
		elif A[i] == '0':
			a.append('0')
			b.append('1')
			c += B[i] == '0'
		else:
			a.append('1')
			b.append(B[i])
	if c > k:
		print(-1)
	else:
		c = k - c
		for i in range(l):
			if c <= 0:
				break
			if a[i] == '1':
				if b[i] == '1':
					a[i] = '0'
					c -= 1
				elif c > 1:
					c -= 2
					a[i] = '0'
					b[i] = '1'
		a = ''.join(a)
		b = ''.join(b)
		print(hex(int(a, 2))[2:].upper())
		print(hex(int(b, 2))[2:].upper())


q = int(input())
for _ in range(q):
	k = int(input())
	a = list(bin(int(input(), 16))[2:])
	b = list(bin(int(input(), 16))[2:])
	c = list(bin(int(input(), 16))[2:])
	max_length = max(len(a), len(b), len(c))
	a = ['0'] * (max_length - len(a)) + a
	b = ['0'] * (max_length - len(b)) + b
	c = ['0'] * (max_length - len(c)) + c
	count = 0
	for (i, (a0, b0, c0)) in enumerate(zip(a, b, c)):
		if c0 == '0':
			if a0 == '1':
				a[i] = '0'
				count += 1
			if b0 == '1':
				b[i] = '0'
				count += 1
		elif a0 == '0' and b0 == '0':
			b[i] = '1'
			count += 1
	if count > k:
		print(-1)
		continue
	for (i, (a0, b0, c0)) in enumerate(zip(a, b, c)):
		if count == k:
			break
		if a0 == '0':
			continue
		if b0 == '1' and count < k:
			a[i] = '0'
			count += 1
		elif count + 1 < k:
			a[i] = '0'
			b[i] = '1'
			count += 2
	for (i, (a0, b0, c0)) in enumerate(zip(a, b, c)):
		if count == k:
			break
		if a0 == '1' and b0 == '1':
			b[i] = '0'
			count += 1
	print(hex(int(''.join(a), 2))[2:].upper())
	print(hex(int(''.join(b), 2))[2:].upper())


bin_hex = {'0000': '0', '0001': '1', '0010': '2', '0011': '3', '0100': '4', '0101': '5', '0110': '6', '0111': '7', '1000': '8', '1001': '9', '1010': 'A', '1011': 'B', '1100': 'C', '1101': 'D', '1110': 'E', '1111': 'F'}
hex_bin = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}
q = int(input())
for qi in range(q):
	K = int(input())
	A = input()
	B = input()
	C = input()
	na = len(A)
	nb = len(B)
	nc = len(C)
	n = max(na, nb, nc)
	A = list('0' * (n - na) + A)
	B = list('0' * (n - nb) + B)
	C = list('0' * (n - nc) + C)
	k = 0
	for i in range(n):
		a = list(hex_bin[A[i]])
		b = list(hex_bin[B[i]])
		c = list(hex_bin[C[i]])
		for j in range(4):
			if c[j] == '1' and b[j] == '0' and (a[j] == '0'):
				b[j] = '1'
				k += 1
			elif c[j] == '0' and b[j] == '1' and (a[j] == '0'):
				b[j] = '0'
				k += 1
			elif c[j] == '0' and b[j] == '0' and (a[j] == '1'):
				a[j] = '0'
				k += 1
			elif c[j] == '0' and b[j] == '1' and (a[j] == '1'):
				a[j] = '0'
				b[j] = '0'
				k += 2
		A[i] = bin_hex[''.join(a)]
		B[i] = bin_hex[''.join(b)]
	if k > K:
		print(-1)
		continue
	for i in range(n):
		if k == K:
			break
		a = list(hex_bin[A[i]])
		b = list(hex_bin[B[i]])
		c = list(hex_bin[C[i]])
		for j in range(4):
			if c[j] == '1' and b[j] == '1' and (a[j] == '1') and (K > k):
				a[j] = '0'
				k += 1
			elif c[j] == '1' and b[j] == '0' and (a[j] == '1') and (K > k + 1):
				b[j] = '1'
				a[j] = '0'
				k += 2
		A[i] = bin_hex[''.join(a)]
		B[i] = bin_hex[''.join(b)]
	all_0 = True
	for i in range(n):
		if A[i] != '0':
			A = A[i:]
			all_0 = False
			break
	if all_0:
		A = ['0']
	print(''.join(A))
	all_0 = True
	for i in range(n):
		if B[i] != '0':
			B = B[i:]
			all_0 = False
			break
	if all_0:
		B = ['0']
	print(''.join(B))


import itertools
import functools

@functools.lru_cache(maxsize=16)
def HexToBinDigit(d):
	result = []
	x = int(d, base=16)
	for bit in range(4):
		result.append(x & 1)
		x >>= 1
	return result
BIN_DIGITS = '0123456789ABCDEF'

def BinToHexIter(b_list):
	non_zero = False
	for pos in range(0, len(b_list), 4):
		val = (b_list[pos] << 3) + (b_list[pos + 1] << 2) + (b_list[pos + 2] << 1) + b_list[pos + 3]
		d = BIN_DIGITS[val]
		if non_zero or d != '0':
			yield d
			non_zero = True
	if not non_zero:
		yield '0'

def BinToHex(b_list):
	return ''.join(BinToHexIter(b_list))

@functools.lru_cache(maxsize=4096)
def FirstPassSolveForSingleDigit(a, b, c):
	a_bits = HexToBinDigit(a)
	b_bits = HexToBinDigit(b)
	c_bits = HexToBinDigit(c)
	cost = 0
	a_result = [0, 0, 0, 0]
	b_result = [0, 0, 0, 0]
	for (pos, (a0, b0, c0)) in enumerate(zip(a_bits, b_bits, c_bits)):
		if c0:
			if a0 or b0:
				a_result[pos] = a0
				b_result[pos] = b0
			else:
				cost += 1
				b_result[pos] = 1
		else:
			cost += a0 + b0
	return (a_result, b_result, cost)

def AddPaddingReverseIter(bin_str, desired_len):
	for x in reversed(bin_str):
		yield x
	for x in range(desired_len - len(bin_str)):
		yield '0'

def FindABC(A, B, C, k):
	desired_len = max(len(A), len(B), len(C))
	A = AddPaddingReverseIter(A, desired_len)
	B = AddPaddingReverseIter(B, desired_len)
	C = AddPaddingReverseIter(C, desired_len)
	A0 = []
	B0 = []
	for (a, b, c) in zip(A, B, C):
		(a0, b0, cost) = FirstPassSolveForSingleDigit(a, b, c)
		A0 += a0
		B0 += b0
		k -= cost
		if k < 0:
			return None
	A0.reverse()
	B0.reverse()
	pos = 0
	while k > 0 and pos < len(A0):
		if A0[pos]:
			if B0[pos]:
				k -= 1
				A0[pos] = 0
			elif k > 1:
				k -= 2
				A0[pos] = 0
				B0[pos] = 1
		pos += 1
	return (A0, B0)
for iter in range(int(input())):
	k = int(input())
	A = input()
	B = input()
	C = input()
	result = FindABC(A, B, C, k)
	if result is None:
		print(-1)
	else:
		print(BinToHex(result[0]))
		print(BinToHex(result[1]))


def char_to_binary(ch):
	val = ord(ch)
	if val <= ord('9'):
		val -= ord('0')
	else:
		val = val - ord('A') + 10
	ret = [0] * 4
	for i in range(3, -1, -1):
		ret[i] = val & 1
		val >>= 1
	return ret

def binary_to_char(ar):
	first_not_found = True
	ll = len(ar)
	ll //= 4
	u = 0
	for i in range(ll):
		val = 0
		for j in range(4):
			val = val * 2 + ar[u]
			u += 1
		if val != 0 or first_not_found == False:
			first_not_found = False
			print(chr(val + ord('0')) if val < 10 else chr(val + ord('A') - 10), end='')
	print(0 if first_not_found else '')

def str_to_binary(a, l):
	ret = [0 for i in range(4 * l)]
	for ch in a:
		l -= 1
		tmp = char_to_binary(a[l])
		for i in range(4):
			ret[4 * l + i] = tmp[i]
	return ret
q = int(input())
while q > 0:
	q -= 1
	k = int(input())
	a = input()
	b = input()
	c = input()
	l = max([len(a), len(b), len(c)])
	x = str_to_binary(a, l)
	y = str_to_binary(b, l)
	z = str_to_binary(c, l)
	ans_x = [0 for i in range(len(x))]
	ans_y = [0 for i in range(len(x))]
	for i in range(4 * l):
		if z[i] == 0:
			ans_x[i] = 0
			ans_y[i] = 0
			if x[i] == 1:
				k -= 1
			if y[i] == 1:
				k -= 1
		else:
			if x[i] == 0 and y[i] == 0:
				ans_y[i] = 1
				k -= 1
			else:
				ans_y[i] = y[i]
			ans_x[i] = x[i]
		if k < 0:
			break
	i = 0
	while i < 4 * l and k > 0:
		if ans_x[i] == 1:
			if ans_y[i] == 1:
				ans_x[i] = 0
				k -= 1
			elif k >= 2:
				ans_x[i] = 0
				ans_y[i] = 1
				k -= 2
		i += 1
	if k < 0:
		print(-1)
	else:
		binary_to_char(ans_x)
		binary_to_char(ans_y)


q = int(input())
for _ in range(q):
	k = int(input())
	a = input()
	b = input()
	c = input()
	m = max(len(a), len(b), len(c))
	a = list(a.zfill(m))
	b = list(b.zfill(m))
	oa = a.copy()
	ob = b.copy()
	c = list(c.zfill(m))
	runningTotalChanges = 0
	changesA = [0] * m
	changesB = [0] * m
	for i in range(m):
		partA = int(a[i], 16)
		partB = int(b[i], 16)
		partC = int(c[i], 16)
		newPartA = (partA ^ partC) & partA
		changesA[i] = bin(newPartA).count('1')
		newPartA ^= partA
		a[i] = hex(newPartA)[2:]
		newPartB = (newPartA | partB) ^ partC ^ partB
		changesB[i] = bin(partB ^ newPartB).count('1')
		b[i] = hex(newPartB)[2:]
	runningTotalChanges = sum(changesB) + sum(changesA)
	i = 0
	while runningTotalChanges < k and i < m:
		ca = bin(int(oa[i], 16)).count('1')
		cb = bin(int(ob[i], 16) ^ int(c[i], 16)).count('1')
		if runningTotalChanges + ca + cb - changesA[i] - changesB[i] <= k:
			a[i] = '0'
			b[i] = c[i]
			runningTotalChanges += ca + cb - changesA[i] - changesB[i]
		else:
			_a = int(a[i], 16)
			_b = int(b[i], 16)
			_c = int(c[i], 16)
			for j in range(4, -1, -1):
				if runningTotalChanges == k:
					break
				if _a >> j & 1 == 1:
					if _b >> j & 1 == 1:
						if _c >> j & 1 == 1:
							_a = _a ^ 1 << j
							runningTotalChanges += 1
						elif runningTotalChanges + 2 <= k:
							_a = _a ^ 1 << j
							_b = _b ^ 1 << j
							runningTotalChanges += 2
					elif _c >> j & 1 == 1:
						if runningTotalChanges + 2 <= k:
							_a = _a ^ 1 << j
							_b = _b ^ 1 << j
							runningTotalChanges += 2
					else:
						_a = _a ^ 1 << j
						runningTotalChanges += 1
				elif _b >> j & 1 == 1:
					if _c >> j & 1 == 0:
						_b = _b ^ 1 << j
						runningTotalChanges += 1
				elif _c >> j & 1 == 1:
					_b = _b ^ 1 << j
					runningTotalChanges += 1
			a[i] = hex(_a)[2:]
			b[i] = hex(_b)[2:]
		i += 1
	if runningTotalChanges > k:
		print('-1')
	else:
		print(''.join(a).lstrip('0').upper() or '0')
		print(''.join(b).lstrip('0').upper())