// Noah Anderson
// 3/3/20
// Int 5
// Enloe
// Potter
// Contest 3

#include 
#include 
#include 
#include 
#include 

using namespace std;

ifstream myfile("F:\\3int.txt");

char hex(string num)
{
    char hexNum;
    if (num == "0000")
        hexNum = '0';
    else if (num == "0001")
        hexNum = '1';
    else if (num == "0010")
        hexNum = '2';
    else if (num == "0011")
        hexNum = '3';
    else if (num == "0100")
        hexNum = '4';
    else if (num == "0101")
        hexNum = '5';
    else if (num == "0110")
        hexNum = '6';
    else if (num == "0111")
        hexNum = '7';
    else if (num == "1000")
        hexNum = '8';
    else if (num == "1001")
        hexNum = '9';
    else if (num == "1010")
        hexNum = 'A';
    else if (num == "1011")
        hexNum = 'B';
    else if (num == "1100")
        hexNum = 'C';
    else if (num == "1101")
        hexNum = 'D';
    else if (num == "1110")
        hexNum = 'E';
    else if (num == "1111")
        hexNum = 'F';

    return hexNum;
}
int main()
{
    for (int i = 0; i < 5; i++)
    {
        bool matrix[4][4];
        string input;
        myfile >> input;
        vector  terms; // substring vector to separate terms

        int nVal1, nVal2;
        bool vert, hor;
        string result = "abcd";
        string hexLine = "a";
        string hexFull = "aaaa";
        vector  results;


        while (input.find('+') < input.length() && input.find('+') > 0)
        {
            int pos = input.find('+');
            string sub = input.substr(0,pos);
            terms.push_back(sub);
            input = input.substr(pos + 1);
        }
        terms.push_back(input);
        for (int k = 0; k < terms.size(); k++)
        {
            string input = terms[k];
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    matrix[i][j] = 1;
                }
            }
            for (int i = 0; i < input.length(); i++)
            {
                vert = false;
                hor = false;
                if (input[i] == 'A')
                {
                    nVal1 = 2;
                    nVal2 = 3;

                    vert = true;
                }
                else if (input[i] == '~' && input[i + 1] == 'A')
                {
                    nVal1 = 0;
                    nVal2 = 1;

                    i++;
                    vert = true;
                }
                else if (input[i] == 'B')
                {
                    nVal1 = 2;
                    nVal2 = 3;
                    hor = true;
                }
                else if (input[i] == '~' && input[i + 1] == 'B')
                {
                    nVal1 = 0;
                    nVal2 = 1;
                    hor = true;
                    i++;
                }
                else if (input[i] == 'C')
                {
                    nVal1 = 0;
                    nVal2 = 3;
                    vert = true;
                }
                else if (input[i] == '~' && input[i + 1] == 'C')
                {
                    nVal1 = 1;
                    nVal2 = 2;
                    vert = true;
                    i++;
                }
                else if (input[i] == 'D')
                {
                    nVal1 = 0;
                    nVal2 = 3;
                    hor = true;
                }
                else
                {
                    nVal1 = 1;
                    nVal2 = 2;
                    hor = true;
                    i++;
                }

                if (vert == true)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        matrix[j][nVal1] = 0;
                        matrix[j][nVal2] = 0;
                    }
                }

                else
                {
                    for (int j = 0; j < 4; j++)
                    {
                        matrix[nVal1][j] = 0;
                        matrix[nVal2][j] = 0;
                    }
            }
            }
                    for (int p = 0; p < 4; p++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (matrix[p][j] == 1)
                        result[j] = '1';
                    else
                        result[j] = '0';
                }
                results.push_back(result);
        }
        }
        if (results.size() > 4)
        {
           for (int i = 0; i < results.size() - 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (results[i][j] == '1' || results[i + 4][j] == '1')
                    {
                        results[i][j] = '1';
                        results[i + 4][j] = '1';
                    }
                }
            }

        }

        for (int i = results.size() - 4; i < results.size(); i++)
        {
            cout << hex(results[i]);
        }
        cout << endl;
    }
}