# -----------------------------------------------------------------------------------------
# Oak Valley Coding Club 2019-2020
# Solution for 2020 ACSL Programming Problem #3 – Junior Division
# Author :  Jared Baza
# Language: Python 3.x
# -----------------------------------------------------------------------------------------


import ovcc_grader_strings


def read_2digit_hex(A):
    Y = [int(i,16) for i in str(A)]#Change Hex String into list of Hex numbers 
    Y1 = bin(Y[0]) #Create first Hex digit into binary string
    Y2 = bin(Y[1]) #Create second Hex digit into binary string
    listY1 = []      # Create binary list from binary string, including leading "0b" binary ID
    for s in str(Y1):
        listY1.append(s)
 
        listY2 = []      # Create binary list from binary string, including leading "0b" binary ID
    for s in str(Y2):
        listY2.append(s)

        list1 = listY1[2:] # Remove python's binary ID
        list2 = listY2[2:]
    
    while len(list1) < 4:#Add leading zeros back on to binary list
        list1 = ['0'] + list1

    while len(list2) < 4:
        list2 = ['0'] + list2

    T = [list(list1),  #Create 2D Array from binary list1 and list2
        list(list2)]
    
    rows = len(T)
    cols = len(T[0])
    count = 0
    for r in range(0,rows):
        for c in range(0,cols):
            lr1 = T[0]# List of strings in first row
            lr2 = T[1]# List of string in second row
            Ele = T[r][c] #Count ocurrance of 1s
            if T[r][c] == '1':
                count+=1

    if count == 0 or count==8:
        ans = "Non Existent"

    
        
    if count == 1:
        if int(lr1[0])==1:
            ans = str("AB~C")
        else:
            if int(lr1[1])==1:
                ans = str("ABC")
            else:
                if int(lr1[2])==1:
                    ans = str("~ABC")
                else:
                    if int(lr1[3])==1:
                        ans = str("~AB~C")
        if int(lr2[0])==1:
            ans = str("A~B~C")
        else:
            if int(lr2[1])==1:
                ans = str("A~BC")
            else:
                if int(lr2[2])==1:
                    ans = str("~A~BC")
                else:
                    if int(lr2[3])==1:
                        ans = str("~A~B~C")

    if count == 2:
        for i in range(0, len(lr1)-1):
            for i in range (0, len(lr2)-1):
                if int(lr1[0])*int(lr1[1])== 1:
                    ans = str('AB')
                else:
                    if int(lr1[2])*int(lr1[3])== 1:
                        ans = str('~AB')
                    else:
                        if int(lr2[0])*int(lr2[1])== 1:
                            ans = str('A~B')
                        else:
                            if int(lr2[2])*int(lr2[3])== 1:
                                ans = str('~A~B')
                            else:
                                if int(lr1[0])*int(lr2[0]) == 1:
                                    ans = str('A~C')
                                else:
                                    if int(lr1[1])*int(lr2[1]) == 1:
                                        ans = str('AC')
                                    else:
                                        if int(lr1[2])*int(lr2[2]) == 1:
                                            ans = ('~AC')
                                        else:
                                            if int(lr1[3])*int(lr2[3]) == 1:
                                                ans = str('~A~C')
                                            else:
                                                if int(lr1[0])*int(lr1[3])== 1:
                                                    ans = str('B~C')
                                                else: 
                                                    if int(lr2[1])*int(lr2[3])== 1:
                                                        ans = str('A~BC + ~A~B~C')
                                                    else:
                                                        if int(lr2[1])*int(lr2[2])== 1:
                                                            ans = str('~BC')
                                                        else:
                                                            if int(lr2[0])*int(lr2[3])== 1:
                                                                ans = str('~B~C')
                                                            else:
                                                                if int(lr2[0])*int(lr2[2])== 1:
                                                                    ans = str('A~B~C + ~A~BC')
                                                                else:
                                                                    if int(lr1[3])*int(lr2[2])== 1:
                                                                        ans = str('~AB~C + ~A~BC')
                                                                    else:
                                                                        if int(lr1[3])*int(lr2[1])== 1:
                                                                            ans = str('~AB~C + A~BC')
                                                                        else:
                                                                            if int(lr1[3])*int(lr2[0])== 1:
                                                                                ans = str('~AB~C + A~B~C')
                                                                            else:
                                                                                if int(lr1[2])*int(lr2[3])== 1:
                                                                                    ans = str('~ABC + ~A~B~C')
                                                                                else:
                                                                                    if int(lr1[2])*int(lr2[1])== 1:
                                                                                        ans = str('~ABC + A~BC')
                                                                                    else:
                                                                                        if int(lr1[2])*int(lr2[0])== 1:
                                                                                            ans = str('~ABC + A~B~C')
                                                                                        else:
                                                                                            if int(lr1[1])*int(lr2[3])== 1:
                                                                                                ans = str('ABC + ~A~B~C')
                                                                                            else:
                                                                                                if int(lr1[1])*int(lr2[2])== 1:
                                                                                                    ans = str('ABC + ~A~BC')
                                                                                                else:
                                                                                                    if int(lr1[1])*int(lr2[2])== 1:
                                                                                                        ans = str('ABC + ~A~BC')
                                                                                                    else:
                                                                                                        if int(lr1[1])*int(lr2[0])== 1:
                                                                                                            ans = str('ABC + A~B~C')
                                                                                                        else:
                                                                                                            if int(lr1[1])*int(lr1[3])== 1:
                                                                                                                ans = str('ABC + ~AB~C')
                                                                                                            else:
                                                                                                                if int(lr1[1])*int(lr1[3])== 1:
                                                                                                                    ans = str('ABC + ~AB~C')
                                                                                                                elif int(lr1[1])*int(lr1[2])== 1:
                                                                                                                        ans = str('BC')
                                                                                                                elif int(lr1[0])*int(lr2[3])== 1:
                                                                                                                         ans = str('AB~C + ~A~B~C')
                                                                                                                elif int(lr1[0])*int(lr2[2])== 1:
                                                                                                                          ans = str('AB~C + ~A~BC')
                                                                                                                elif int(lr1[0])*int(lr2[1])== 1:
                                                                                                                          ans = str('AB~C + A~BC')
                                                                                                                elif int(lr1[0])*int(lr1[2])== 1:
                                                                                                                          ans = str('AB~C + ~ABC')
                                                              

        
    if count == 3:
        for i in range(0, len(lr1)-1):
            for i in range (0, len(lr2)-1):
                if int(lr1[0])*int(lr1[3])*int(lr2[1])== 1:
                    ans = str('B~C + A~BC')
                elif int(lr1[0])*int(lr1[3])*int(lr2[2])== 1:
                    ans = str('B~C + ~A~BC')
                elif int(lr2[0])*int(lr2[3])*int(lr1[1])== 1:
                    ans = str('~B~C + ABC')
                elif int(lr2[0])*int(lr2[3])*int(lr1[2])== 1:
                    ans = str('~ABC + ~B~C')
                elif int(lr1[0])*int(lr1[1])*int(lr1[2])== 1:
                    ans = str('AB + ~ABC')
                elif int(lr1[0])*int(lr1[1])*int(lr1[3])== 1:
                    ans = str('AB + ~AB~C')
                elif int(lr1[0])*int(lr1[1])*int(lr2[0])== 1:
                    ans = str('AB + A~B~C')
                elif int(lr1[0])*int(lr1[1])*int(lr2[1])== 1:
                    ans = str('AB + A~BC')
                elif int(lr1[0])*int(lr1[1])*int(lr2[2])== 1:
                    ans = str('AB + ~A~BC')
                elif int(lr1[0])*int(lr1[1])*int(lr2[3])== 1:
                    ans = str('AB + ~A~B~C')
                elif int(lr1[0])*int(lr2[2])*int(lr2[3])== 1:
                    ans = str('~A~B + AB~C')
                elif int(lr1[0])*int(lr2[1])*int(lr2[3])== 1:
                    ans = str('AB~C + ABC + ~A~B~C')
                elif int(lr1[0])*int(lr2[1])*int(lr2[2])== 1:
                    ans = str('AB~C + ~BC')
                elif int(lr1[0])*int(lr2[0])*int(lr2[3])== 1:
                    ans = str('AB~C + ~B~C')
                elif int(lr1[0])*int(lr2[0])*int(lr2[2])== 1:
                    ans = str('A~C + ~A~BC')
                elif int(lr1[0])*int(lr2[0])*int(lr2[1])== 1:
                    ans = str('A~B + AB~C')
                elif int(lr1[0])*int(lr1[3])*int(lr2[3])== 1:
                    ans = str('~A~C + AB~C')
                elif int(lr1[0])*int(lr1[3])*int(lr2[0])== 1:
                    ans = str('A~C + ~AB~C')
                elif int(lr1[0])*int(lr1[2])*int(lr2[3])== 1:
                    ans = str('AB~C + ~ABC + ~A~B~C')
                elif int(lr1[0])*int(lr1[2])*int(lr2[2])== 1:
                    ans = str('~AC + AB~C')
                elif int(lr1[0])*int(lr1[2])*int(lr2[1])== 1:
                    ans = str('AB~C + ~ABC + A~BC')
                elif int(lr1[0])*int(lr1[2])*int(lr2[0])== 1:
                    ans = str('A~C + ~ABC')
                elif int(lr1[0])*int(lr1[2])*int(lr1[3])== 1:
                    ans = str('~AB + AB~C')
                elif int(lr2[0])*int(lr2[1])*int(lr2[3])== 1:
                    ans = str('A~B + ~A~B~C')
                elif int(lr2[0])*int(lr2[1])*int(lr2[2])== 1:
                    ans = str('A~B + ~A~BC')
                elif int(lr1[3])*int(lr2[0])*int(lr2[1])== 1:
                    ans = str('A~B + ~AB~C')
                elif int(lr1[2])*int(lr2[0])*int(lr2[1])== 1:
                    ans = str('A~B + ~ABC')
                elif int(lr1[1])*int(lr2[0])*int(lr2[1])== 1:
                    ans = str('A~B + ABC')
                elif int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                    ans = str('~BC + ~A~B~C')
                elif int(lr1[3])*int(lr2[1])*int(lr2[3])== 1:
                    ans = str('~A~C + A~BC')
                elif int(lr1[2])*int(lr2[1])*int(lr2[3])== 1:
                    ans = str('~ABC + A~BC + ~A~B~C')
                elif int(lr1[1])*int(lr2[1])*int(lr2[3])== 1:
                    ans = str('AC + ~A~B~C')
                elif int(lr1[3])*int(lr2[1])*int(lr2[2])== 1:
                    ans = str('~BC + ~AB~C')
                elif int(lr1[2])*int(lr2[1])*int(lr2[2])== 1:
                    ans = str('~BC + ~ABC')
                elif int(lr1[1])*int(lr2[1])*int(lr2[2])== 1:
                    ans = str('~BC + ABC')
                elif int(lr1[2])*int(lr1[3])*int(lr2[1])== 1:
                    ans = str('~AB + A~BC')
                elif int(lr1[1])*int(lr1[3])*int(lr2[1])== 1:
                    ans = str('AC + ~AB~C')
                elif int(lr1[1])*int(lr1[2])*int(lr2[1])== 1:
                    ans = str('BC + A~BC')
                elif int(lr1[2])*int(lr2[2])*int(lr2[3])== 1:
                    ans = str('~A~B + ~ABC')
                elif int(lr1[2])*int(lr2[2])*int(lr2[0])== 1:
                    ans = str('~AC + A~B~C')
                elif int(lr1[2])*int(lr1[3])*int(lr2[3])== 1:
                    ans = str('~AB + ~A~B~C')
                elif int(lr1[2])*int(lr1[3])*int(lr2[2])== 1:
                    ans = str('~AB + ~A~BC')
                elif int(lr1[2])*int(lr1[3])*int(lr2[0])== 1:
                    ans = str('~AB + A~B~C')
                elif int(lr1[1])*int(lr1[2])*int(lr2[3])== 1:
                    ans = str('BC + ~A~B~C')
                elif int(lr1[1])*int(lr1[2])*int(lr2[2])== 1:
                    ans = str('BC + ~A~BC')
                elif int(lr1[1])*int(lr1[2])*int(lr2[0])== 1:
                    ans = str('BC + A~B~C')
                elif int(lr1[1])*int(lr1[2])*int(lr1[3])== 1:
                    ans = str('BC + ~AB~C')
                elif int(lr2[0])*int(lr2[2])*int(lr2[3])== 1:
                    ans = str('~A~B + A~B~C')
                elif int(lr1[3])*int(lr2[2])*int(lr2[3])== 1:
                    ans = str('~A~B + ~AB~C')
                elif int(lr1[3])*int(lr2[0])*int(lr2[3])== 1:
                    ans = str('~A~C + A~B~C')
                elif int(lr1[3])*int(lr2[0])*int(lr2[2])== 1:
                    ans = str('~AB~C + A~B~C + ~A~BC')
                elif int(lr1[1])*int(lr2[2])*int(lr2[3])== 1:
                    ans = str('~A~B + ABC')
                elif int(lr1[1])*int(lr2[0])*int(lr2[2])== 1:
                    ans = str('ABC + A~B~C + ~A~BC')
                elif int(lr1[1])*int(lr1[3])*int(lr2[3])== 1:
                    ans = str('~A~C + ABC')
                elif int(lr1[1])*int(lr1[3])*int(lr2[2])== 1:
                    ans = str('ABC + ~AB~C + ~A~BC')
                elif int(lr1[1])*int(lr1[3])*int(lr2[0])== 1:
                    ans = str('ABC + ~AB~C + A~B~C')
                                    

    if count == 4: #Finding 4 Adjacent Xs
        if int(lr1[0])*int(lr2[0])*int(lr1[1])*int(lr2[1])== 1:
            ans = str('A')
        else:
            if int(lr1[2])*int(lr2[2])*int(lr1[3])*int(lr2[3])== 1:
                ans = str('~A')
            elif int(lr1[1])*int(lr2[1])*int(lr1[2])*int(lr2[2])== 1:
                ans = str('C')
            elif int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])== 1:
                ans = str('B')
            elif int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~B')
            elif int(lr1[0])*int(lr2[0])*int(lr1[3])*int(lr2[3])== 1:
                ans = str('~C')
            elif int(lr1[0])*int(lr1[1])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('AB + ~A~B')
            elif int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])== 1:
                ans = str('~AB + A~B')
            elif int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[3])== 1:
                ans = str('~AB + ~B~C')
            elif int(lr1[0])*int(lr1[3])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('~A~C + AB~C + A~BC')
            elif int(lr1[0])*int(lr1[1])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('AB + A~BC + ~A~B~C')
            elif int(lr1[0])*int(lr1[1])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('AB + ~BC')
            elif int(lr1[0])*int(lr1[1])*int(lr2[0])*int(lr2[3])== 1:
                ans = str('AB + ~B~C')
            elif int(lr1[0])*int(lr1[1])*int(lr2[0])*int(lr2[2])== 1:
                ans = str('AB + A~B~C + ~A~BC')
            elif int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[3])== 1:
                ans = str('AB + ~A~C')
            elif int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[2])== 1:
                ans = str('AB + ~AB~C + ~A~BC')
            elif int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[1])== 1:
                ans = str('AB + ~AB~C + A~BC')
            elif int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[0])== 1:
                ans = str('AB + ~AB~C + A~B~C')
            elif int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[3])== 1:
                ans = str('AB + ~ABC + ~A~B~C')
            elif int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[2])== 1:
                ans = str('AB + ~AC')
            elif int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[1])== 1:
                ans = str('AB + ~ABC + A~BC')
            elif int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[0])== 1:
                ans = str('AB + ~ABC + A~B~C')
            elif int(lr1[2])*int(lr1[3])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('~AB + A~BC + ~A~B~C')
            elif int(lr1[2])*int(lr1[3])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('~AB + ~BC')
            elif int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[2])== 1:
                ans = str('~AB + A~B~C + ~A~BC')
            elif int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[3])== 1:
                ans = str('BC + ~A~C')
            elif int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[2])== 1:
                ans = str('BC + ~AB~C + ~A~BC')
            elif int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[1])== 1:
                ans = str('BC + ~AB~C + A~BC')
            elif int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])== 1:
                ans = str('BC + ~AB~C + A~B~C')
            elif int(lr1[0])*int(lr1[2])*int(lr1[3])*int(lr2[3])== 1:
                ans = str('~AB + AB~C + ~A~B~C')
            elif int(lr1[0])*int(lr1[2])*int(lr1[3])*int(lr2[1])== 1:
                ans = str('~AB + AB~C + A~BC')
            elif int(lr1[0])*int(lr1[2])*int(lr1[3])*int(lr2[0])== 1:
                ans = str('~AB + A~C')
            elif int(lr1[3])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~BC + ~A~C')
            elif int(lr1[3])*int(lr2[0])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~A~B + ~AB~C + A~B~C')
            elif int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('A~B + ~A~C')
            elif int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('A~B + ~AB~C + ~A~BC')
            elif int(lr1[2])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~BC + ~ABC + ~A~B~C')
            elif int(lr1[2])*int(lr2[0])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~A~B + ~ABC + A~B~C')
            elif int(lr1[2])*int(lr2[0])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('A~B + ~ABC + ~A~B~C')
            elif int(lr1[2])*int(lr2[0])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('A~B + ~AC')
            elif int(lr1[1])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~BC + ABC + ~A~B~C')
            elif int(lr1[1])*int(lr2[0])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~A~B + ABC + A~B~C')
            elif int(lr1[1])*int(lr2[0])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('A~B + ABC + ~A~B~C')
            elif int(lr1[1])*int(lr2[0])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('A~B + ABC + ~A~BC')
            elif int(lr1[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~BC + AB~C + ~A~B~C')
            elif int(lr1[0])*int(lr2[0])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~A~B + A~C')
            elif int(lr1[0])*int(lr2[0])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('A~B + AB~C + ~A~B~C')
            elif int(lr1[0])*int(lr2[0])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('A~B + AB~C + ~A~BC')
            elif int(lr1[1])*int(lr1[3])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~A~B + ABC + ~AB~C')
            elif int(lr1[1])*int(lr1[3])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('AC + ~A~C')
            elif int(lr1[1])*int(lr1[3])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('~BC + ABC + ~AB~C')
            elif int(lr1[1])*int(lr1[3])*int(lr2[0])*int(lr2[3])== 1:
                ans = str('~A~C + ABC + A~B~C')
            elif int(lr1[1])*int(lr1[3])*int(lr2[0])*int(lr2[2])== 1:
                ans = str('ABC + ~AB~C + A~B~C + ~A~BC')
            elif int(lr1[1])*int(lr1[3])*int(lr2[0])*int(lr2[1])== 1:
                ans = str('A~B + ABC + ~AB~C')
            elif int(lr1[1])*int(lr1[2])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('BC + ~A~B')
            elif int(lr1[1])*int(lr1[2])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('BC + A~BC + ~A~B~C')
            elif int(lr1[1])*int(lr1[2])*int(lr2[0])*int(lr2[3])== 1:
                ans = str('BC + A~B~C + ~A~B~C')
            elif int(lr1[1])*int(lr1[2])*int(lr2[0])*int(lr2[2])== 1:
                ans = str('BC + A~B~C + ~A~BC')
            elif int(lr1[1])*int(lr1[2])*int(lr2[0])*int(lr2[1])== 1:
                ans = str('BC + A~B')
            elif int(lr1[0])*int(lr1[3])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('B~C + ~A~B')
            elif int(lr1[0])*int(lr1[3])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('~BC + B~C')
            elif int(lr1[0])*int(lr1[3])*int(lr2[0])*int(lr2[2])== 1:
                ans = str('A~C + ~AB~C + ~A~BC')
            elif int(lr1[0])*int(lr1[3])*int(lr2[0])*int(lr2[1])== 1:
                ans = str('A~B + B~C')
            elif int(lr1[0])*int(lr1[2])*int(lr2[2])*int(lr2[3])== 1:
                ans = str('~A~B + AB~C + ~ABC')
            elif int(lr1[0])*int(lr1[2])*int(lr2[1])*int(lr2[3])== 1:
                ans = str('AB~C + ~ABC + A~BC + ~A~B~C')
            elif int(lr1[0])*int(lr1[2])*int(lr2[1])*int(lr2[2])== 1:
                ans = str('~BC + AB~C + ~ABC')
            elif int(lr1[0])*int(lr1[2])*int(lr2[0])*int(lr2[3])== 1:
                ans = str('A~C + ~ABC + ~A~B~C')
            elif int(lr1[0])*int(lr1[2])*int(lr2[0])*int(lr2[2])== 1:
                ans = str('A~C + ~AC')
            elif int(lr1[0])*int(lr1[2])*int(lr2[0])*int(lr2[1])== 1:
                ans = str('A~B + AB~C + ~ABC') 
            


    if count ==5:
        nlr1 = [int(i,2) for i in lr1]
        nlr2 = [int(i,2) for i in lr2]
        for i in range(0, len(nlr1)-1):
            for i in range(0, len(nlr2)-1):
                if nlr1[0]*nlr1[1]*nlr1[2]*nlr1[3]*nlr2[0]== 1:
                    ans = str('B + A~B~C')
                elif sum(nlr1)==4 and nlr2[1]== 1:
                    ans = str('B + A~BC')
                elif sum(nlr1)==4 and nlr2[2]== 1:
                    ans = str('B + ~A~BC')
                elif sum(nlr1)==4 and nlr2[3]== 1:
                    ans = str('B + ~A~B~C')
                elif sum(nlr2)==4 and nlr1[3]== 1:
                    ans = str('~B + ~AB~C')
                elif sum(nlr2)==4 and nlr1[2]== 1:
                    ans = str('~B + ~ABC')
                elif sum(nlr2)==4 and nlr1[1]== 1:
                    ans = str('~B + ABC')
                elif sum(nlr2)==4 and nlr1[0]== 1:
                    ans = str('~B + AB~C')
                elif nlr1[2]*nlr1[3]*nlr2[1]*nlr2[2]*nlr2[3]== 1:
                    ans = str('~A + A~BC')
                elif nlr1[2]*nlr1[3]*nlr2[0]*nlr2[2]*nlr2[3]== 1:
                    ans = str('~A + A~B~C')
                elif nlr1[2]*nlr1[3]*nlr2[0]*nlr2[1]*nlr2[3]== 1:
                    ans = str('~AB + A~B + ~A~B~C')
                elif nlr1[2]*nlr1[3]*nlr2[0]*nlr2[1]*nlr2[2]== 1:
                    ans = str('~AB + A~B + ~A~BC')
                elif nlr1[1]*nlr1[3]*nlr2[3]*nlr2[1]*nlr2[2]== 1:
                    ans = str('~BC + ~A~C + ABC')
                elif nlr1[1]*nlr1[3]*nlr2[3]*nlr2[0]*nlr2[2]== 1:
                    ans = str('~A~B + ABC + ~AB~C + A~B~C')
                elif nlr1[1]*nlr1[3]*nlr2[3]*nlr2[0]*nlr2[1]== 1:
                    ans = str('AC + ~A~C + A~B~C')
                elif nlr1[1]*nlr1[3]*nlr2[2]*nlr2[0]*nlr2[1]== 1:
                    ans = str('A~B + ABC + ~AB~C + ~A~BC')
                elif nlr1[1]*nlr1[2]*nlr2[2]*nlr2[3]*nlr2[1]== 1:
                    ans = str('C + ~A~B~C')
                elif nlr1[1]*nlr1[2]*nlr2[2]*nlr2[3]*nlr2[0]== 1:
                    ans = str('BC + ~A~B + A~B~C')
                elif nlr1[1]*nlr1[2]*nlr2[1]*nlr2[3]*nlr2[0]== 1:
                    ans = str('BC + A~B + ~A~B~C')
                elif nlr1[1]*nlr1[2]*nlr2[1]*nlr2[2]*nlr2[0]== 1:
                    ans = str('C + A~B~C')
                elif nlr1[1]*nlr1[2]*nlr1[3]*nlr2[2]*nlr2[3]== 1:
                    ans = str('~A + ABC')
                elif nlr1[1]*nlr1[2]*nlr1[3]*nlr2[1]*nlr2[3]== 1:
                    ans = str('BC + ~A~C + A~BC')
                elif nlr1[1]*nlr1[2]*nlr1[3]*nlr2[1]*nlr2[2]== 1:
                    ans = str('C + ~AB~C')
                elif nlr1[1]*nlr1[2]*nlr1[3]*nlr2[0]*nlr2[3]== 1:
                    ans = str('BC + ~A~C + A~B~C')
                elif nlr1[1]*nlr1[2]*nlr1[3]*nlr2[0]*nlr2[2]== 1:
                    ans = str('BC + ~AB~C + A~B~C + ~A~BC')
                elif nlr1[1]*nlr1[2]*nlr1[3]*nlr2[0]*nlr2[1]== 1:
                    ans = str('BC + A~B + ~AB~C')
                elif nlr1[0]*nlr1[3]*nlr2[3]*nlr2[2]*nlr2[1]== 1:
                    ans = str('~BC + ~A~C + AB~C')
                elif nlr1[0]*nlr1[3]*nlr2[3]*nlr2[2]*nlr2[0]== 1:
                    ans = str('A~C + ~A~C + ~A~BC')
                elif nlr1[0]*nlr1[3]*nlr2[3]*nlr2[1]*nlr2[0]== 1:
                    ans = str('A~C + ~A~C + A~BC')
                elif nlr1[0]*nlr1[3]*nlr2[2]*nlr2[1]*nlr2[0]== 1:
                    ans = str('A~B + B~C + ~A~BC')
                elif nlr1[0]*nlr1[2]*nlr2[2]*nlr2[1]*nlr2[3]== 1:
                    ans = str('~BC + AB~C + ~ABC + ~A~B~C')
                elif nlr1[0]*nlr1[2]*nlr2[2]*nlr2[0]*nlr2[3]== 1:
                    ans = str('A~C + ~AC + ~A~B~C')
                elif nlr1[0]*nlr1[2]*nlr2[0]*nlr2[1]*nlr2[3]== 1:
                    ans = str('A~B + AB~C + ~ABC + ~A~B~C')
                elif nlr1[0]*nlr1[2]*nlr2[0]*nlr2[1]*nlr2[2]== 1:
                    ans = str('A~B + ~AC + AB~C')
                elif nlr1[0]*nlr1[2]*nlr1[3]*nlr2[3]*nlr2[2]== 1:
                    ans = str('~A + AB~C')
                elif nlr1[0]*nlr1[2]*nlr1[3]*nlr2[3]*nlr2[1]== 1:
                    ans = str('~AB + AB~C + A~BC + ~A~B~C')
                elif nlr1[0]*nlr1[2]*nlr1[3]*nlr2[2]*nlr2[1]== 1:
                    ans = str('~AB + ~BC + AB~C')
                elif nlr1[0]*nlr1[2]*nlr1[3]*nlr2[0]*nlr2[3]== 1:
                    ans = str('~AB + ~B~C + AB~C')
                elif nlr1[0]*nlr1[2]*nlr1[3]*nlr2[0]*nlr2[2]== 1:
                    ans = str('~AB + A~C + ~AB~C')
                elif nlr1[0]*nlr1[2]*nlr1[3]*nlr2[0]*nlr2[1]== 1:
                    ans = str('~AB + A~B + AB~C')
                elif nlr1[0]*nlr1[1]*nlr2[3]*nlr2[2]*nlr2[1]== 1:
                    ans = str('AB + ~BC + ~A~B~C')
                elif nlr1[0]*nlr1[1]*nlr2[3]*nlr2[2]*nlr2[0]== 1:
                    ans = str('AB + ~A~B + A~B~C')
                elif nlr1[0]*nlr1[1]*nlr2[3]*nlr2[1]*nlr2[0]== 1:
                    ans = str('A + ~A~B~C')
                elif nlr1[0]*nlr1[1]*nlr2[2]*nlr2[1]*nlr2[0]== 1:
                    ans = str('A + ~A~BC')
                elif nlr1[0]*nlr1[1]*nlr1[3]*nlr2[2]*nlr2[3]== 1:
                    ans = str('AB + ~A~B + ~AB~C')
                elif nlr1[0]*nlr1[1]*nlr1[3]*nlr2[1]*nlr2[3]== 1:
                    ans = str('AB + ~A~C + A~BC')
                elif nlr1[0]*nlr1[1]*nlr1[3]*nlr2[1]*nlr2[2]== 1:
                    ans = str('AB + ~BC + ~AB~C')
                elif nlr1[0]*nlr1[1]*nlr1[3]*nlr2[0]*nlr2[3]== 1:
                    ans = str('AB + ~B~C + ~AB~C')
                elif nlr1[0]*nlr1[1]*nlr1[3]*nlr2[0]*nlr2[2]== 1:
                    ans = str('AB + ~AB~C + A~B~C + ~A~BC')
                elif nlr1[0]*nlr1[1]*nlr1[3]*nlr2[0]*nlr2[1]== 1:
                    ans = str('A + ~AB~C')
                elif nlr1[0]*nlr1[1]*nlr1[2]*nlr2[2]*nlr2[3]== 1:
                    ans = str('AB + ~A~B +~ABC')
                elif nlr1[0]*nlr1[1]*nlr1[2]*nlr2[1]*nlr2[3]== 1:
                    ans = str('AB + ~ABC + A~BC + ~A~B~C')
                elif nlr1[0]*nlr1[1]*nlr1[2]*nlr2[1]*nlr2[2]== 1:
                    ans = str('C + AB~C')
                elif nlr1[0]*nlr1[1]*nlr1[2]*nlr2[0]*nlr2[3]== 1:
                    ans = str('AB + ~B~C + ~ABC')
                elif nlr1[0]*nlr1[1]*nlr1[2]*nlr2[0]*nlr2[2]== 1:
                    ans = str('AB + ~AC + A~B~C')
                elif nlr1[0]*nlr1[1]*nlr1[2]*nlr2[0]*nlr2[1]== 1:
                    ans = str('A + ~ABC')
                else:
                    ans = str("Refer to Veitch Diagram")
        
    if count == 6:
        for i in range(0, len(lr1)-1):
            for i in range (0, len(lr2)-1):
                if int(lr1[0])*int(lr2[0])*int(lr1[1])*int(lr2[1])*int(lr1[2])*int(lr2[2])== 1:
                    ans = str('A + ~AC')
                else:
                    if int(lr1[0])*int(lr2[0])*int(lr1[1])*int(lr2[1])*int(lr1[3])*int(lr2[3])== 1:
                        ans = str('A + ~A~C')
                    if int(lr1[1])*int(lr2[1])*int(lr1[2])*int(lr2[2])*int(lr1[3])*int(lr2[3])== 1:
                        ans = str('C + ~A~C')
                    if int(lr1[0])*int(lr2[0])*int(lr1[2])*int(lr2[2])*int(lr1[3])*int(lr2[3])== 1:
                        ans = str('A~C + ~A')
                    if int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('~B + ~AB')
                    if int(lr1[1])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('~B + ABC + ~AB~C')
                    if int(lr1[0])*int(lr1[1])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('~B + AB')
                    if int(lr1[1])*int(lr1[2])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('~B + BC')
                    if int(lr1[0])*int(lr1[3])==1 and int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('~B + B~C')
                    if int(lr1[0])*int(lr1[2])==1 and int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('~B + AB~C + ~ABC')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])== 1:
                        ans = str('B + A~B')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[3])== 1:
                        ans = str('B + ~B~C')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[1])*int(lr2[2])== 1:
                        ans = str('B + ~BC')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[1])*int(lr2[3])== 1:
                        ans = str('B + A~BC +~A~B~C')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('B + ~A~B')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[0])*int(lr2[1])*int(lr2[3])== 1:
                        ans = str('A + ~ABC + ~A~B~C')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[2])== 1:
                        ans = str('B + A~B~C + ~A~BC')
                    if int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[2])*int(lr2[3])== 1:
                        ans = str('~A + ABC + A~B~C')
                    if int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[3])== 1:
                        ans = str('BC + A~B + ~A~C')
                    if int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])== 1:
                        ans = str('C + ~AB~C + A~B~C')
                    if int(lr1[0])*int(lr1[2])*int(lr1[3])*int(lr2[3])*int(lr2[1])*int(lr2[2])== 1:
                        ans = str('~A + AB~C + A~BC')
                    if int(lr1[0])*int(lr1[2])*int(lr1[3])*int(lr2[3])*int(lr2[1])*int(lr2[0])== 1:
                        ans = str('~AB + A~B + AB~C + ~A~B~C')
                    if int(lr1[0])*int(lr1[2])*int(lr1[3])*int(lr2[2])*int(lr2[1])*int(lr2[0])== 1:
                        ans = str('~AB + A~B + AB~C + ~A~BC')
                    if int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[2])*int(lr2[1])*int(lr2[3])== 1:
                        ans = str('AB + ~BC + ~A~C')
                    if int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[2])*int(lr2[0])*int(lr2[3])== 1:
                        ans = str('AB + ~A~B + ~AB~C + A~B~C')
                    if int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[2])*int(lr2[0])*int(lr2[1])== 1:
                        ans = str('A + ~AB~C + ~A~BC')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[2])*int(lr2[3])*int(lr2[1])== 1:
                        ans = str('C + AB~C + ~A~B~C')
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[2])*int(lr2[3])*int(lr2[0])== 1:
                        ans = str('AB + ~A~B + ~ABC + A~B~C')
                    

    if count == 7:
        if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])==1 and int(lr2[3])==0:
            ans = str("B + A~B + A~BC")
        else:
            if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[3])==1 and int(lr2[2])==0:
                ans = str("B + A~B + ~A~B~C")
            else:
                if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[2])*int(lr2[3])==1 and int(lr2[1])==0:
                    ans = str("B + ~A~B + A~B~C")
                else:
                    if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[1])*int(lr2[2])*int(lr2[3])==1 and int(lr2[0])==0:
                        ans = str("B + ~BC + ~A~B~C")
                    else:
                        if int(lr1[0])*int(lr1[1])*int(lr1[2])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])==1 and int(lr1[3])==0:
                            ans = str("~B + A~B + ~ABC")
                        else:
                            if int(lr1[0])*int(lr1[1])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])==1:
                                ans = str("~B + AB + ~AB~C")
                            else:
                                if int(lr1[0])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])==1:
                                    ans = str("~B + ~AB + AB~C")
                                else:
                                    if int(lr1[1])*int(lr1[2])*int(lr1[3])*int(lr2[0])*int(lr2[1])*int(lr2[2])*int(lr2[3])==1:
                                        ans = str("~B + BC + ~AB~C")

  
    return ans
                                        
         
input = ovcc_grader_strings.readinput()

ovcc_grader_strings.clearoutput()
for line in input:
    A = line[0]
    #B = line[1]
    ovcc_grader_strings.writeoutput(read_2digit_hex(A))