'''
Sebastian Wittrock
Bergen Country Academies
Senior 5
'''

def main():
    file = open("4sr_sampledata.txt", "r")

    for line in file:
        x = line.split()
        for i in range(len(x)):
            x[i] = int(x[i])
        print(findSum(x))


def findSum(line):

    opponent = [line[0], line[1], line[2]]
    player = [line[3], line[4], line[5]]
    num_moves = line[6]
    rolls = []

    for i in range(num_moves):
        rolls.append(line[7+i])

    for i in range(num_moves):
        if i % 2 == 0:
            nextMove(opponent, player, rolls[i])
        else:
            nextMove(player, opponent, rolls[i])
    return str(sum(opponent)) + " " + str(sum(player))

def nextMove(curplayer, otherplayer, roll):

    prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
    squares = [9, 16, 25, 36, 49]

    lowest = min(curplayer)
    index = curplayer.index(lowest)

    move = roll + lowest
    if move < 52:

        if move in prime:
            curplayer[index] = move
            for i in range(move + 1, move + 7):
                if i in curplayer or i in otherplayer:
                    break
                else:
                    curplayer[index] += 1
            if curplayer[index] == 52:
                curplayer.remove(curplayer[index])
            elif curplayer[index] > 52:
                curplayer[index] = move
        elif move in squares:
            curplayer[index] = move
            for i in range(move - 1, move - 7, -1):
                if i in curplayer or i in otherplayer:
                    break
                else:
                    curplayer[index] -= 1

        elif horizontal(lowest, roll):
            curplayer[index] = checkMultiple(curplayer, otherplayer, roll)
            if curplayer[index] == 52:
                curplayer.remove(curplayer[index])

        elif move not in curplayer and move not in otherplayer:
            curplayer[index] = move

def horizontal(marker, roll):
    special = [7, 12, 17, 22, 27, 35, 40, 45, 50]
    for i in range(len(special)):
        if marker < special[i] and marker + roll > special[i]:
            return True
    return False

def checkMultiple(opp, player, roll):
    for i in range(min(opp) + roll, min(opp), -1):
        if i % roll == 0 and i not in player and i not in opp and i <= 52:
            return i
    return min(opp)
main()