Path To C

0

0 votes
Easy
Problem

Today, Sherlock Holmes is playing with numbers. He has got 4 numbers A, B, C, and S from a crime scene. He is trying to find those 2 numbers which ORing gives C. If those numbers are M and N (M|N=C where | denotes bitwise OR), these must be found only to interchange at max S bits of A and B.  If there are multiple solutions, make M as small as possible; if there are still multiple solutions, make N as small as possible.

 Holmes need your help to find M and N. So write a program to help Holmes.

Note:

  • A, B, and C are given in Hexadecimal (base 16), and is S given in decimal (base 10).
  • If the number of bits changed in A is ka and the number of bits changed in B is kb, then (ka + kb ) must be <  S.

Input:

The first line of the input contains an integer T denoting the number of test cases. The description of each test case follows.

  • The first line contains a single integer denoting the value of S.
  • Next three lines contains a Hexadecimal (base 16) numbers describing the respective values of AB and C.

Output:

Print two lines of output for each test case:

  1. The first line should contain a Hexadecimal (base 16) number denoting the value of M.
  2. The second line must contain a Hexadecimal (base 16), number denoting the value of N.

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

Constraints:

1<T <6

0 < S< 5x105

0< A,B,C< 165x10000 

Sample Test Case:

Input:

3

8

2B

9F

58

5

B9

40

5A

2

91

BE

A8

Output:

8

58

18

42

-1

Explaination: 

Test case (0):

A = (2B)16 = (00101011)2 

After changing 3 bits

M = (00001000)2 = (8)16 
B = (9F)16 = (10011111)2 

After changing 5 bits

N = (01011000)2 = (58)16
M|N = (8)16 | (58)16 = (58)16 = C
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?