/**
 * Author: Jason Cobb
 * School: Triangle Math and Science Academy
 * Division: I-3
 * Contest: 3 (Veitch)
 */

import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableMap

private inline fun printExceptions(f: () -> Unit) {
    try {
        f()
    } catch (t: Throwable) {
        t.printStackTrace()
    }
}

private enum class Variable(private val charRep: Char) {
    A('A'), B('B'), C('C'), D('D');

    companion object {
        fun fromChar(c: Char): Variable {
            val uppercaseChar = c.toUpperCase()
            return values().firstOrNull { it.charRep == uppercaseChar } ?: throw IllegalArgumentException("Invalid variable char: $c")
        }
    }
}


private data class LogicalExpressionValue(val raw: Boolean)

private data class VariableMapping(private val map: ImmutableMap) {
    constructor(map: Map) : this(map.toImmutableMap())

    init {
        require(map.keys.containsAll(Variable.values().toList()))
    }

    operator fun get(variable: Variable) = map[variable] ?: error("Should contain all Variables")
}

private interface LogicalExpression {
    fun evaluate(variableMapping: VariableMapping): LogicalExpressionValue
}

private data class VariableExpression(private val variable: Variable) : LogicalExpression {
    override fun evaluate(variableMapping: VariableMapping): LogicalExpressionValue {
        return variableMapping[variable]
    }
}

private data class DisjunctionExpression(private val subExpressions: ImmutableList) : LogicalExpression {
    constructor(subExpressions: List) : this(subExpressions.toImmutableList())

    override fun evaluate(variableMapping: VariableMapping): LogicalExpressionValue {
        return LogicalExpressionValue(subExpressions.any { it.evaluate(variableMapping).raw })
    }
}

private data class ConjunctionExpression(private val subExpressions: ImmutableList) : LogicalExpression {
    constructor(subExpressions: List) : this(subExpressions.toImmutableList())

    override fun evaluate(variableMapping: VariableMapping): LogicalExpressionValue {
        return LogicalExpressionValue(subExpressions.all { it.evaluate(variableMapping).raw })
    }
}

private data class NegationExpression(private val subExpression: LogicalExpression) : LogicalExpression {
    override fun evaluate(variableMapping: VariableMapping): LogicalExpressionValue {
        return LogicalExpressionValue(subExpression.evaluate(variableMapping).raw.not())
    }
}

private fun parseConjunctionExpression(string: String): LogicalExpression {
    check(!string.contains("+"))

    val parts = mutableListOf()
    var isNegated: Boolean = false

    for (c in string) {
        if (c == '~') {
            check(!isNegated) // There should not be two ~s in a row
            isNegated = true
        } else {
            val variable = Variable.fromChar(c)
            val variableExpr = VariableExpression(variable)

            parts += (if (isNegated) NegationExpression(variableExpr) else variableExpr)
            isNegated = false
        }
    }

    check(!isNegated) // Any negation should be gone by the end, since it should have a variable after it
    return ConjunctionExpression(parts)
}

private fun parseLogicalExpression(string: String): LogicalExpression {
    val parts = string.replace(" ", "").split("+")
    return DisjunctionExpression(parts.map { parseConjunctionExpression(it) })
}

/**
 * (0, 0) is the top left, (4, 4) is the top right
 */
private fun logicalExpressionToTable(expression: LogicalExpression): List> {
    fun valueA(row: Int, col: Int) = LogicalExpressionValue(col in listOf(0, 1))
    fun valueB(row: Int, col: Int) = LogicalExpressionValue(row in listOf(0, 1))
    fun valueC(row: Int, col: Int) = LogicalExpressionValue(col in listOf(1, 2))
    fun valueD(row: Int, col: Int) = LogicalExpressionValue(row in listOf(1, 2))

    val results = MutableList(4) { MutableList(4) { null } }

    for (row in 0..3) {
        for (col in 0..3) {
            results[row][col] =
                expression.evaluate(
                    VariableMapping(
                        mapOf(
                            Variable.A to valueA(row, col),
                            Variable.B to valueB(row, col),
                            Variable.C to valueC(row, col),
                            Variable.D to valueD(row, col)
                        )
                    )
                ).raw
        }
    }

    return results.map { row -> row.map { it!! } }
}

private fun Double.isIntegral() = this.toInt().toDouble() == this

private fun pow(base: Int, exp: Int): Int {
    val doubleVal = Math.pow(base.toDouble(), exp.toDouble())

    require(doubleVal >= Int.MIN_VALUE && doubleVal <= Int.MAX_VALUE)
    require(doubleVal.isIntegral())

    return doubleVal.toInt()
}

private fun tableRowToNumber(row: List): Int {
    // Reverse because leftmost bit in table is most significant bit, so put it at the highest index
    val reversedRow = row.reversed()
    return reversedRow.mapIndexed { index, value -> (if (value) 1 else 0) * (pow(2, index)) }.sum()
}

fun main() {
    repeat(5) { _ ->
        printExceptions {
            val line = readLine() ?: error("Input is required")
            val expression = parseLogicalExpression(line)
            val table = logicalExpressionToTable(expression)

            println(table.map { row -> tableRowToNumber(row).toString(radix = 16).toUpperCase() }.joinToString(separator = ""))
        }
    }
}

/*

AB+~AB+~A~B
AB~C~D+AB~CD+~A~B~CD
AB~C~D+~AB~C~D+A~B~C~D
B~D+~B~D
~A~BD+~A~B~D
B~D+~A~BD+A~B~C
~B~C+BCD+B~C~D
A~C+ACD+~A~CD
AB~D+~ABD+A~BD+~A~B~D
B~D+~A~CD+~A~B~C~D

 */