// ACSL4-Patolli.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include 
using namespace std;

int checkprime(int num)
{
    for (int i = 2; i <= num / 2; i++)
    {
        if (num % i == 0)
            return 0;
    }
    return 1;
}

int checksquare(int num)
{
    for (int i = 3; i <= 7; i++)
    {
        if (num == i * i)
            return 1;           
    }
    return 0;
}

int check_HtoV(int num, int move)
{
    if (move >= 2)
    {
        for (int i = 0; i < move-1; i++)
        {
            num--;
            if (num == 7 || num == 12 || num == 17 || num == 22 || num == 27 || num == 35 || num == 40 || num == 45 || num == 50)
                return 1;
        }
    }
    return 0;
}

int Add(int num, int n1, int n2, int n3)
{
    for (int i = 1; i <= 6; i++)
    {
        if (num+i == n1 || num+i == n2 || num+i == n3)       
            return (num+i-1);
    }
    return (num+6);
}

int Backwards(int num, int n1, int n2, int n3)
{
    for (int i = 1; i <= 6; i++)
    {
        if (num-i == n1 || num-i == n2 || num-i == n3)
            return(num-i+1);
    }
    return (num-6);
}

int moveMultiple(int num, int n1, int n2, int n3, int move)
{
    for (int i = 1; i <= move; i++)
    {
        if ((num+i) % move == 0)
        {
            if (num+i != n1 && num+i != n2 && num+i != n3)               
                return(num+i);
        }
    }
    return (num);
}

int main()
{
    int O1[5], O2[5], O3[5], psn[5], n[5];
    int arr[5][10];
    for (int a = 0; a < 5; a++)
    {
        cin >> O1[a];  cin >> O2[a];  cin >> O3[a];
        cin >> psn[a];
        cin >> n[a];
        for (int j = 0; j < n[a]; j++)
            cin >> arr[a][j];
    }

    int position;
    cout << endl;

    for (int a = 0; a < 5; a++)
    {
        cout << a+1 << ". ";
        position = psn[a];     
        for (int i = 0; i < n[a]; i++)
        {       
            if (position == 52)                        
                break;
           
            position = position + arr[a][i];
            
            if (position == 52)
            {
                cout << "GAME OVER" << endl;
                break;
            }
                
            else if (position != O1[a] && position != O2[a] && position != O3[a])
            {

                if (position > 52)
                {
                    position = position - arr[a][i];
                }

                else if (checkprime(position) == 1)
                {
                    position = Add(position, O1[a], O2[a], O3[a]);
                }

                else if (checksquare(position) == 1)
                {
                    position = Backwards(position, O1[a], O2[a], O3[a]);
                }

                else if (checkprime(position) == 0 && checksquare(position) == 0)
                {
                    if (check_HtoV(position, arr[a][i]) == 1)
                    {
                        position = moveMultiple(position - arr[a][i], O1[a], O2[a], O3[a], arr[a][i]);
                    }
                }

                else
                    position = position;
            }

            else
                position = position - arr[a][i];
        }
        if (position != 52)
            cout << position << endl;
    }
    
 
    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file