#John Kesler
#Cary Academy INT-3
#ACSL Contest #3

import re

inputs = [i.strip() for i in open('input.txt', 'r').readlines()]

def run_case(i):
    main_grid = set()
    for expression in i.split('+'):
        subgrids = [set()]
        for i in range(4):
            for j in range(4):
                subgrids[0].add((i, j))
        for term in re.findall(r'~*[ABCD]', expression):
            termgrid = set()

            char = term[-1]
            inv = '~' in term

            # oof
            rows_filling = {
                'A': {False: [0,1,2,3], True: [0,1,2,3]},
                'B': {False: [0,1], True: [2,3]},
                'C': {False: [0,1,2,3], True: [0,1,2,3]},
                'D': {False: [1,2], True: [0,3]}
            }[char][inv]
            cols_filling = {
                'A': {False: [0,1], True: [2,3]},
                'B': {False: [0,1,2,3], True: [0,1,2,3]},
                'C': {False: [1,2], True: [0,3]},
                'D': {False: [0,1,2,3], True: [0,1,2,3]}
            }[char][inv]

            for row in rows_filling:
                for col in cols_filling:
                    termgrid.add((row, col))
            subgrids += [termgrid]
        main_grid = main_grid | set.intersection(*subgrids)
    out = ""
    for row in range(4):
        truths = []
        out += f'{hex(sum(0 if (row, col) not in main_grid else 2**(3-col) for col in range(4)))}'[-1]
    return out

for case, i in enumerate(inputs):
    print(f'{case+1}: {run_case(i)}')