#/***********************************************
#   School: Longfellow Middle School
#   Name: Teddy Winston
#   Grade: 8th
#   Division: INT-3
#   Program Language: Python
#*************************************************/




def splitByLetter(sec):
    endLis = []
    les = sec.split("~")
    if len(les) == 1:
        return list(les[0])
    if les[0] == "":
        endLis.append("~" + les[1][0])
        endLis.extend(les[1][1:])
        del les[1], les[0]
    else:
        endLis.extend(list(les[0]))
        del les[0]
    for i in range(len(les)):
        endLis.append("~"+les[i][0])
        les[i] = les[i][1:]
        endLis.extend(list(les[i]))
    return endLis
def veitch(v):
    diagram = [['1','1','1','1'],['1','1','1','1'],['1','1','1','1'],['1','1','1','1']]
    for i in v:
        if i == "B":
            diagram[2] = ['0','0','0','0']
            diagram[3] = ['0','0','0','0']
        elif i == "~B":
            diagram[0] = ['0','0','0','0']
            diagram[1] = ['0','0','0','0']
        elif i == "D":
            diagram[0] = ['0','0','0','0']
            diagram[3] = ['0','0','0','0']
        elif i == "~D":
            diagram[2] = ['0','0','0','0']
            diagram[1] = ['0','0','0','0']
        elif i == "A":
            diagram[0][2] = '0'
            diagram[1][2] = '0'
            diagram[2][2] = '0'
            diagram[3][2] = '0'
            diagram[0][3] = '0'
            diagram[1][3] = '0'
            diagram[2][3] = '0'
            diagram[3][3] = '0'
        elif i == "~A":
            diagram[0][0] = '0'
            diagram[1][0] = '0'
            diagram[2][0] = '0'
            diagram[3][0] = '0'
            diagram[0][1] = '0'
            diagram[1][1] = '0'
            diagram[2][1] = '0'
            diagram[3][1] = '0'
        elif i == "C":
            diagram[0][0] = '0'
            diagram[1][0] = '0'
            diagram[2][0] = '0'
            diagram[3][0] = '0'
            diagram[0][3] = '0'
            diagram[1][3] = '0'
            diagram[2][3] = '0'
            diagram[3][3] = '0'
        elif i == "~C":
            diagram[0][1] = '0'
            diagram[1][1] = '0'
            diagram[2][1] = '0'
            diagram[3][1] = '0'
            diagram[0][2] = '0'
            diagram[1][2] = '0'
            diagram[2][2] = '0'
            diagram[3][2] = '0'
    return diagram
inp = input("Please enter your input file: ")
op = open(inp,"r")
read = op.readlines()
op.close()

for line in read:
    diagramlis = []
    oldnew = line.split("+")
    for letter in oldnew:
        lta = ''
        split = splitByLetter(letter)
        diagram = veitch(split)
        for i in range(4):
            for j in range(4):
                lta += diagram[i][j]
        diagramlis.append(lta)
    final = '0000000000000000'
    finals = list(final)
    for g in diagramlis:
        for q in range(16):
            if g[q] == '1':
                finals[q] = '1'
    final = ''.join(finals)
    final = str(hex(int(final, base = 2)))[2:].upper()
    while len(final) < 4:
        afinal = list(final)
        afinal.insert(0,'0')
        final = ''.join(afinal)
    print(final)