#include <iostream>

using namespace std;

int command;

int main()
{
    for (int game = 1; game <= 1000; game++)
    {
        int card[5];
        cin >> card[1] >> card[2] >> card[3] >> card[4];

        int turn = 0;
        int start_card;
        while (1)
        {
            if (command == 4)
                break;
            if (turn == 0)
            {
                if (command == 0)
                    cin >> command;
                if (command >= 7)
                {
                    start_card = command;
                    while (1)
                    {
                        bool cut = false;
                        for (int i = 1; i <= 4; i++)
                        {
                            if (card[i] == start_card || ((start_card == 10 || start_card == 14) && (card[i] == 7)))
                            {
                                cout << card[i] << '\n';
                                card[i] = 0;
                                cut = true;
                                cout.flush();
                                break;
                            }
                        }
                        if (!cut)
                        {
                            cout << card[1] << '\n';
                            card[1] = 0;
                            cout.flush();
                        }
                        cin >> command;
                        if (command < 7)
                        {
                            if (cut)
                                turn = 1;
                            break;
                        }
                    }
                }
                else if (command == 2 || command == 3)
                {
                    int n;
                    cin >> n;
                    while (n--)
                    {
                        if (card[1] == 0)
                            cin >> card[1];
                        else if (card[2] == 0)
                            cin >> card[2];
                        else if (card[3] == 0)
                            cin >> card[3];
                        else if (card[4] == 0)
                            cin >> card[4];
                    }
                    if (command == 2)
                        turn = 0;
                    else
                        turn = 1;
                }
            }
            else
            {
                int start_card = card[1];
                cout << card[1] << '\n';
                card[1] = 0;
                cout.flush();
                while (1)
                {
                    cin >> command;
                    if (command == start_card || command == 7)
                    {
                        bool cut = false;
                        for (int i = 1; i <= 4; i++)
                            if (card[i] == start_card || card[i] == 7)
                            {
                                cout << card[i] << '\n';
                                card[i] = 0;
                                cout.flush();
                                cut = true;
                                break;
                            }
                        if (!cut)
                        {
                            cout << 0 << '\n';
                            cout.flush();
                            turn = 0;
                        }
                    }
                }
            }
        }
    }

    return 0;
}