/**
 * Author: Jason Cobb
 * School: Triangle Math and Science Academy, Grade 11
 * Division: I-3
 * Contest: 2019-20, #4 (Patolli)
 */

import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.toImmutableSet
import kotlin.math.sqrt

private infix fun Int.isMultipleOf(divisor: Int): Boolean = (this % divisor) == 0

private fun Int.isSquare(): Boolean {
    val fSqrt = sqrt(this.toFloat())
    val iSqrt = fSqrt.toInt()
    return (iSqrt * iSqrt) == this
}

private fun Int.isPrime(): Boolean {
    if (this <= 1) return false

    for (i in 2 until this) {
        if (this isMultipleOf i) return false
    }

    return true
}

private data class BoardIndex(val raw: Int) : Comparable {
    override fun compareTo(other: BoardIndex): Int {
        return (this.raw).compareTo(other.raw)
    }
}

private fun BoardIndex.previous() = BoardIndex(this.raw - 1)
private fun BoardIndex.next() = BoardIndex(this.raw + 1)

private data class BoardSquares(val raw: Int)

private operator fun BoardIndex.plus(other: BoardSquares) = BoardIndex((this.raw).plus(other.raw))
private operator fun BoardIndex.minus(other: BoardSquares) = BoardIndex((this.raw).minus(other.raw))
private operator fun BoardIndex.minus(other: BoardIndex) = BoardSquares((this.raw).minus(other.raw))

private fun BoardIndex.triggersRule7() = raw.isPrime()
private fun BoardIndex.triggersRule8() = (raw > 4) && (raw.isSquare())

private data class DiceRoll(val raw: Int)

private data class GameState(
    val opponentPositions: ImmutableSet,
    val playerPositions: ImmutableSet
) {
    companion object {
        private val MIN_INDEX = BoardIndex(1)
        private val MAX_INDEX = BoardIndex(52)

        // A move travels one horizontal followed by one vertical if it starts _strictly before_ and ends
        // _strictly after_ any one of these indices
        private val RULE_9_TRIGGERS =
            listOf(7, 12, 17, 22, 27, 35, 40, 45, 50).map { BoardIndex(it) }
    }

    private fun occupiedPositions() = opponentPositions + playerPositions
    private fun BoardIndex.isOccupied() = occupiedPositions().contains(this)
    private fun BoardIndex.isUnoccupied() = !isOccupied()
    private fun BoardIndex.overrunsBoard() = this > MAX_INDEX

    private fun doLastWhere(start: BoardIndex, limit: BoardIndex, after: (BoardIndex) -> BoardIndex, filter: (BoardIndex) -> Boolean): BoardIndex {
        var current = start
        while (current != limit && filter(after(current))) current = after(current)
        return current
    }

    private fun backwardsLastUnoccupied(start: BoardIndex, min: BoardIndex = MIN_INDEX): BoardIndex {
        require(min <= start)
        return doLastWhere(start = start, limit = min, after = { it.previous() }, filter = { it.isUnoccupied() })
    }

    private fun forwardsLastUnoccupied(start: BoardIndex, max: BoardIndex = MIN_INDEX): BoardIndex {
        require(start <= max)
        return doLastWhere(start = start, limit = max, after = { it.next() }, filter = { it.isUnoccupied() })
    }

    private fun movingPiece() = playerPositions.min() ?: error("Expected position to exist")

    private fun indexAfterRule7(indexBefore: BoardIndex): BoardIndex {
        return forwardsLastUnoccupied(start = indexBefore, max = indexBefore + BoardSquares(6))
    }

    private fun indexAfterRule8(indexBefore: BoardIndex): BoardIndex {
        return backwardsLastUnoccupied(start = indexBefore, min = indexBefore - BoardSquares(6))
    }

    private fun moveTriggersRule9(start: BoardIndex, end: BoardIndex): Boolean {
        for (switchIndex in RULE_9_TRIGGERS) {
            if (start < switchIndex && end > switchIndex) return true
        }

        return false
    }

    private fun indexAfterRule9(start: BoardIndex, roll: DiceRoll): BoardIndex {
        val candidateIndices = (start.raw)..(start.raw + roll.raw)
        val multipleIndices = candidateIndices.filter { it.isMultipleOf(roll.raw) && BoardIndex(it).isUnoccupied() }

        if (multipleIndices.isEmpty()) return start
        return BoardIndex(multipleIndices.max() ?: error(""))
    }

    fun stateAfterRoll(roll: DiceRoll): GameState {
        // If there are no player positions remaining, the game is over, and the state will never change
        if (playerPositions.isEmpty()) return this

        val startPosition = movingPiece()

        fun stateWithNewIndex(endIndex: BoardIndex): GameState {
            val positionsWithoutStart: ImmutableSet = (playerPositions - startPosition).toImmutableSet()

            if (endIndex.overrunsBoard()) return this // Cannot overrun, state unchanged

            // If square reaches the end, the game is over
            if (endIndex == MAX_INDEX)
                return GameState(
                    opponentPositions = opponentPositions,
                    playerPositions = positionsWithoutStart
                )

            return GameState(
                opponentPositions = opponentPositions,
                playerPositions = (positionsWithoutStart + endIndex).toImmutableSet()
            )
        }

        val provisionalEnd = startPosition + BoardSquares(roll.raw)

        if (provisionalEnd.isOccupied()) return this // State unchanged
        if (provisionalEnd.overrunsBoard()) return this // State unchanged

        if (provisionalEnd.triggersRule7()) return stateWithNewIndex(indexAfterRule7(provisionalEnd))
        if (provisionalEnd.triggersRule8()) return stateWithNewIndex(indexAfterRule8(provisionalEnd))

        if (moveTriggersRule9(start = startPosition, end = provisionalEnd)) return stateWithNewIndex(indexAfterRule9(start = startPosition, roll = roll))

        // Provisional end turned out to be right
        return stateWithNewIndex(provisionalEnd)
    }
}

private fun formatOutput(state: GameState): String {
    val playerPositions = state.playerPositions
    return if (playerPositions.isEmpty()) "GAME OVER" else (playerPositions.map { it.raw }.sorted().joinToString(" "))
}

fun main() {
    repeat(5) { _ ->
        val line = readLine() ?: error("Expected input")
        val intParts = line.split(" ").map { it.toInt() }

        val opponentPositions = intParts.subList(0, 3).map { BoardIndex(it) }.toImmutableSet()
        val playerPositions = intParts.subList(3, 6).map { BoardIndex(it) }.toImmutableSet()

        val numDiceRolls = intParts[6]
        val diceRolls = intParts.subList(7, 7 + numDiceRolls).map { DiceRoll(it) }

        val startState =
            GameState(
                opponentPositions = opponentPositions,
                playerPositions = playerPositions
            )

        var currentState = startState

        for (roll in diceRolls) {
            currentState = currentState.stateAfterRoll(roll)
        }

        println(formatOutput(currentState))
    }
}