# Anthony Li
# Bergen County Academies
# Intermediate

T = 0b1111111111111111
values = {
    'A': 0b1100110011001100,
    'B': 0b1111111100000000,
    'C': 0b0110011001100110,
    'D': 0b0000111111110000
}

def get_var(it):
    ch = next(it, None)
    if ch is None:
        return None

    if ch == '~':
        return T - get_var(it)
    else:
        return values[ch]

def solve_term(term):
    result = T
    it = iter(term)

    current = get_var(it)
    while current is not None:
        result &= current
        current = get_var(it)
    
    return result

def solve(expression):
    result = 0
    for x in expression.split('+'):
        result |= solve_term(x)
    return result

file = open("3int_sampledata.txt", "r")
for i in range(5):
    print("%04X" % solve(file.readline().replace("\n", "").replace("\r", "").replace(" ", "")))

file.close()