#include 
#include 
#include 
#include 

using namespace std;

vector parts;
int full_grid[4][4] = {0};
int pb[4][4] = {0};

void parse_input (string full_input)
{

    parts.clear ();
    full_input += '\n';
    int part_start = 0;
    int current_length = 0;

    for (int i = 0; i < full_input.length (); i++)
    {

        if (full_input[i] == '+' || full_input[i] == '\n')
        {

            string pb = full_input.substr (part_start, current_length);
            parts.push_back (pb);
            part_start = i + 1;
            current_length = -1;

        }

        current_length++;

    }

}

void make_grid (string part)
{

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            pb[i][j] = 1;

    for (int i = 0; i < part.length (); i++)
    {

        if (part[i] == 'A')
        {

            for (int j = 0; j < 4; j++)
                for (int k = 2; k < 4; k++)
                    pb[j][k] = 0;

        }

        else if (part[i] == 'B')
        {

            for (int j = 2; j < 4; j++)
                for (int k = 0; k < 4; k++)
                    pb[j][k] = 0;

        }

        else if (part[i] == 'C')
        {

            for (int j = 0; j < 4; j++)
                for (int k = 0; k < 4; k += 3)
                    pb[j][k] = 0;

        }

        else if (part[i] == 'D')
        {

            for (int j = 0; j < 4; j += 3)
                for (int k = 0; k < 4; k++)
                    pb[j][k] = 0;

        }

        else if (part[i] == '~' && part[i + 1] == 'A')
        {

            i++;

            for (int j = 0; j < 4; j ++)
                for (int k = 0; k < 2; k++)
                    pb[j][k] = 0;

        }

        else if (part[i] == '~' && part[i + 1] == 'B')
        {

            i++;

            for (int j = 0; j < 2; j ++)
                for (int k = 0; k < 4; k++)
                    pb[j][k] = 0;


        }

        else if (part[i] == '~' && part[i + 1] == 'C')
        {

            i++;

            for (int j = 0; j < 4; j ++)
                for (int k = 1; k < 3; k++)
                    pb[j][k] = 0;

        }

        else if (part[i] == '~' && part[i + 1] == 'D')
        {

            i++;

            for (int j = 1; j < 3; j ++)
                for (int k = 0; k < 4; k++)
                    pb[j][k] = 0;

        }

    }

}

void merge_grid ()
{

    for (int i = 0; i < 4; i++)
    {

        for (int j = 0; j < 4; j++)
        {

            full_grid[i][j] = full_grid[i][j] || pb[i][j];

        }

    }

}

string to_hex ()
{

    string ans = "";

    for (int i = 0; i < 4; i++)
    {

        if (full_grid[i][0] == 0 && full_grid[i][1] == 0 && full_grid[i][2] == 0 && full_grid[i][3] == 0)
            ans += '0';

        else if (full_grid[i][0] == 0 && full_grid[i][1] == 0 && full_grid[i][2] == 0 && full_grid[i][3] == 1)
            ans += '1';

        else if (full_grid[i][0] == 0 && full_grid[i][1] == 0 && full_grid[i][2] == 1 && full_grid[i][3] == 0)
            ans += '2';

        else if (full_grid[i][0] == 0 && full_grid[i][1] == 0 && full_grid[i][2] == 1 && full_grid[i][3] == 1)
            ans += '3';

        else if (full_grid[i][0] == 0 && full_grid[i][1] == 1 && full_grid[i][2] == 0 && full_grid[i][3] == 0)
            ans += '4';

        else if (full_grid[i][0] == 0 && full_grid[i][1] == 1 && full_grid[i][2] == 0 && full_grid[i][3] == 1)
            ans += '5';

        else if (full_grid[i][0] == 0 && full_grid[i][1] == 1 && full_grid[i][2] == 1 && full_grid[i][3] == 0)
            ans += '6';

        else if (full_grid[i][0] == 0 && full_grid[i][1] == 1 && full_grid[i][2] == 1 && full_grid[i][3] == 1)
            ans += '7';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 0 && full_grid[i][2] == 0 && full_grid[i][3] == 0)
            ans += '8';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 0 && full_grid[i][2] == 0 && full_grid[i][3] == 1)
            ans += '9';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 0 && full_grid[i][2] == 1 && full_grid[i][3] == 0)
            ans += 'A';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 0 && full_grid[i][2] == 1 && full_grid[i][3] == 1)
            ans += 'B';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 1 && full_grid[i][2] == 0 && full_grid[i][3] == 0)
            ans += 'C';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 1 && full_grid[i][2] == 0 && full_grid[i][3] == 1)
            ans += 'D';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 1 && full_grid[i][2] == 1 && full_grid[i][3] == 0)
            ans += 'E';

        else if (full_grid[i][0] == 1 && full_grid[i][1] == 1 && full_grid[i][2] == 1 && full_grid[i][3] == 1)
            ans += 'F';

    }

    return ans;

}

void input_and_solve ()
{

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

    if (infile.is_open ())
    {

        while (infile.good ())
        {

            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    full_grid[i][j] = 0;

            string full_input;
            getline(infile, full_input);

            if (full_input != "")
            {

                parse_input (full_input);

                for (int i = 0; i < parts.size (); i++)
                {

                    make_grid (parts[i]);
                    merge_grid ();

                }

                cout << to_hex () << endl;

            }

        }

    }

    else
        cout << "ERROR: Could not find file :(";

}

int main ()
{

    input_and_solve ();

    return 0;

}