# Takoma Park M.S.
# Int-3
# Contest 3
# Evan Wang

varList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
newVarList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
expressionList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
lineList = []
firstVar = True
firstTerm = True
output = ""

def varCheck():
  global varList, newVarList, firstVar

  if firstVar == True:
    varList = newVarList
    firstVar = False
  else:
    for index1 in range(len(varList)):
      if varList[index1] == 1 and newVarList[index1] == 1:
        varList[index1] = 1
      else:
        varList[index1] = 0




f = open("3int_testdata.txt", "a")
f.close()

f = open("3int_testdata.txt", "r")

for line in f:
  lineList = line.split("+")
  lineList[-1] = lineList[-1].replace("\n", "")
  for index in range(len(lineList)):
    if "A" in lineList[index] and not("~A" in lineList[index]):
      newVarList = [1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
      varCheck()
    if "~A" in lineList[index]:
      newVarList = [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]
      varCheck()
    if "B" in lineList[index] and not("~B" in lineList[index]):
      newVarList = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
      varCheck()
    if "~B" in lineList[index]:
      newVarList = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
      varCheck()
    if "C" in lineList[index] and not("~C" in lineList[index]):
      newVarList = [0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0]
      varCheck()
    if "~C" in lineList[index]:
      newVarList = [1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1]
      varCheck()
    if "D" in lineList[index] and not("~D" in lineList[index]):
      newVarList = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
      varCheck()
    if "~D" in lineList[index]:
      newVarList = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
      varCheck()
    

    if firstTerm == True:
      expressionList = varList
      firstTerm = False
    else:
      for index2 in range(len(expressionList)):
        if expressionList[index2] == 1 or varList[index2] == 1:
          expressionList[index2] = 1

    varList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    newVarList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    firstVar = True
    
  for number in range(4):
    if expressionList[(number*4):((number+1)*4)] == [0, 0, 0, 0]:
      output += "0"
    elif expressionList[(number*4):((number+1)*4)] == [0, 0, 0, 1]:
      output += "1"
    elif expressionList[(number*4):((number+1)*4)] == [0, 0, 1, 0]:
      output += "2"
    elif expressionList[(number*4):((number+1)*4)] == [0, 0, 1, 1]:
      output += "3"
    elif expressionList[(number*4):((number+1)*4)] == [0, 1, 0, 0]:
      output += "4"
    elif expressionList[(number*4):((number+1)*4)] == [0, 1, 0, 1]:
      output += "5"
    elif expressionList[(number*4):((number+1)*4)] == [0, 1, 1, 0]:
      output += "6"
    elif expressionList[(number*4):((number+1)*4)] == [0, 1, 1, 1]:
      output += "7"
    elif expressionList[(number*4):((number+1)*4)] == [1, 0, 0, 0]:
      output += "8"
    elif expressionList[(number*4):((number+1)*4)] == [1, 0, 0, 1]:
      output += "9"
    elif expressionList[(number*4):((number+1)*4)] == [1, 0, 1, 0]:
      output += "A"
    elif expressionList[(number*4):((number+1)*4)] == [1, 0, 1, 1]:
      output += "B"
    elif expressionList[(number*4):((number+1)*4)] == [1, 1, 0, 0]:
      output += "C"
    elif expressionList[(number*4):((number+1)*4)] == [1, 1, 0, 1]:
      output += "D"
    elif expressionList[(number*4):((number+1)*4)] == [1, 1, 1, 0]:
      output += "E"
    elif expressionList[(number*4):((number+1)*4)] == [1, 1, 1, 1]:
      output += "F"
  
  print(output)

  output = ""
  firstVar = True
  firstTerm = True

f.close()