// Nagasujan Ganesh Kumar
// 3/13/20
// Int 5
// Enloe
// Potter
// Contest #3

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    /// INPUT ///
    ifstream myfile;
    string input;
    //myfile.open("acslinput3.txt");
    myfile.open("F:\\3int.txt");
    string sentence;
    int z = 1;

    /// THE MEAT ///
    while (getline (myfile, sentence))
    {
        input = sentence;

        /// INITIALIZING GRID ///
        string grid[6][6]; //i is what row it is, j is what column it is
        grid[0][0] = " "; grid[0][5] = " "; grid[5][0] = " "; grid[5][5] = " ";
        grid[0][1] = "A"; grid[0][2] = "A"; grid[0][3] = "~A"; grid[0][4] = "~A";
        grid[1][0] = "B"; grid[2][0] = "B"; grid[3][0] = "~B"; grid[4][0] = "~B";
        grid[5][1] = "~C"; grid[5][2] = "C"; grid[5][3] = "C"; grid[5][4] = "~C";
        grid[1][5] = "~D"; grid[2][5] = "D"; grid[3][5] = "D"; grid[4][5] = "~D";

        for (int i = 1; i < 5; i++)
        {
            for (int j = 1; j < 5; j++)
            {
                grid[i][j] = "0";
            }
        }

        /// MAKING THE GRID ///
        int k = 0;
        while (k != input.length())
        {
            /// SETTING GRID UP BASED ON FIRST LETTER ///
            if (input[k] == '~')
            {
                k++;
                if (input[k] == 'A') // ~A
                {
                    for (int i = 1; i < 5; i++)
                    {
                        for (int j = 3; j < 5; j++)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                    k++;
                }
                else if (input[k] == 'B') // ~B
                {
                    for (int i = 3; i < 5; i++)
                    {
                        for (int j = 1; j < 5; j++)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                    k++;
                }
                else if (input[k] == 'C') // ~C
                {
                    for (int i = 1; i < 5; i++)
                    {
                        for (int j = 1; j < 5; j+=3)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                    k++;
                }
                else if (input[k] == 'D') // ~D
                {
                    for (int i = 1; i < 5; i+=3)
                    {
                        for (int j = 1; j < 5; j++)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                    k++;
                }
            }
            else
            {
                if (input[k] == 'A')
                {
                    for (int i = 1; i < 5; i++)
                    {
                        for (int j = 1; j < 3; j++)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                }
                else if (input[k] == 'B')
                {
                    for (int i = 1; i < 3; i++)
                    {
                        for (int j = 1; j < 5; j++)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                }
                else if (input[k] == 'C')
                {
                    for (int i = 1; i < 5; i++)
                    {
                        for (int j = 2; j < 4; j++)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                }
                else if (input[k] == 'D')
                {
                    for (int i = 2; i < 4; i++)
                    {
                        for (int j = 1; j < 5; j++)
                        {
                            if (grid[i][j] != "*")
                            {
                                grid[i][j] = "1";
                            }
                        }
                    }
                }
                k++;
            }

            /// ERASING ///
            int x = 0;
            while (x == 0)
            {
                if (k == input.length())
                {
                    x = 1;
                }
                else if (input[k] == '+')
                {
                    k++;
                    x = 1;
                }
                else if (input[k] == '~')
                {
                    k++;
                    for (int i = 1; i < 5; i++)
                    {
                        for (int j = 1; j < 5; j++)
                        {
                            if ((input[k] == 'A') && (j != 3) && (j != 4) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                            else if ((input[k] == 'B') && (i != 3) && (i != 4) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                            else if ((input[k] == 'C') && (j != 1) && (j != 4) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                            else if ((input[k] == 'D') && (i != 1) && (i != 4) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                        }
                    }
                    k++;
                }
                else
                {
                    for (int i = 1; i < 5; i++)
                    {
                        for (int j = 1; j < 5; j++)
                        {
                            if ((input[k] == 'A') && (j != 1) && (j != 2) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                            else if ((input[k] == 'B') && (i != 1) && (i != 2) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                            else if ((input[k] == 'C') && (j != 2) && (j != 3) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                            else if ((input[k] == 'D') && (i != 2) && (i != 3) && (grid[i][j] != "*"))
                            {
                                grid[i][j] = "0";
                            }
                        }
                    }
                    k++;
                }
                if (k == input.length())
                {
                    x = 1;
                }
            }

            for (int i = 1; i < 5; i++)
            {
                for (int j = 1; j < 5; j++)
                {
                    if (grid[i][j] == "1")
                    {
                        grid[i][j] = "*";
                    }
                }
            }
        }

        /*
        /// OUTPUT GRID ///
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                cout << grid[i][j] << '\t';
            }
            cout << endl;
        }
        cout << endl;
        */

        /// HEXA OUTPUT ///
        string output[4];
        for (int i = 1; i < 5; i++)
        {
            output[i-1] = "";
            for (int j = 1; j < 5; j++)
            {
                output[i-1] = output[i-1] + grid[i][j];
            }
            if (output[i-1] == "0000")
                output[i-1] = "0";
            else if (output[i-1] == "000*")
                output[i-1] = "1";
            else if (output[i-1] == "00*0")
                output[i-1] = "2";
            else if (output[i-1] == "00**")
                output[i-1] = "3";
            else if (output[i-1] == "0*00")
                output[i-1] = "4";
            else if (output[i-1] == "0*0*")
                output[i-1] = "5";
            else if (output[i-1] == "0**0")
                output[i-1] = "6";
            else if (output[i-1] == "0***")
                output[i-1] = "7";
            else if (output[i-1] == "*000")
                output[i-1] = "8";
            else if (output[i-1] == "*00*")
                output[i-1] = "9";
            else if (output[i-1] == "*0*0")
                output[i-1] = "A";
            else if (output[i-1] == "*0**")
                output[i-1] = "B";
            else if (output[i-1] == "**00")
                output[i-1] = "C";
            else if (output[i-1] == "**0*")
                output[i-1] = "D";
            else if (output[i-1] == "***0")
                output[i-1] = "E";
            else if (output[i-1] == "****")
                output[i-1] = "F";
        }
        string hexoutput = output[0] + output[1] + output[2] + output[3];
        cout << z << ". " << hexoutput << endl;
        z++;
    }
    return 0;
}