# Name: Bo Chi
# Team: We Sense
# Intermediate Division
# ACSL Contest #3
# Date of Program: 3/9/2020 
# Grade: 9
# Language: Python
# Input Data Method: Text File
def gen_diagram():
    diagram=[]
    for i in range(4):
        row=[]
        for j in range(4):
            line=[]
            if j<2:
                line.append("A")
            else:
                line.append("~A")
            if i<2:
                line.append("B")
            else:
                line.append("~B")
            if j%3==0:
                line.append("~C")
            else:
                line.append("C")
            if i%3==0:
                line.append("~D")
            else:
                line.append("D")
            line.append(0)
            row.append(line)
        diagram.append(row)
    return diagram
   
def veitch(line):
    line=line.split("+")
    interpreted=[]
    for n in line:
        string=[]
        current=""
        for c in range(0,len(n)):
            if n[c].isalpha()==True:
                current+=n[c]
                string.append(current)
                current=""
            else:
                current+=n[c]
        interpreted.append(string)
    veitch=gen_diagram()
    for testing in interpreted:
        inString=True
        for a in range(4):
            for b in range(4):
                isIn=True
                for s in testing:
                    if (s in veitch[a][b])==False:
                        isIn=False
                if isIn==True:
                    if veitch[a][b][4]==0:
                        veitch[a][b][4]=1
    h=""
    for a in range(4):
        r=""
        for b in range(4): 
            r+=str(veitch[a][b][4])
        h+=r
    h=hex(int(h,2))
    new=h[2:].upper()
    if len(new)<4:
        new="0"*(4-len(new))+new
    print(new)

with open("3int_testdata.txt" , 'r') as f:
    datas = f.readlines()
newdata=[]
for i in datas:
    newdata.append(i.rstrip())
for l in newdata:
    veitch(l)