# Daniel Kim
# Bergen County Academies
# Senior Division

file_name = "4sr_sampledata.txt"

f = open(file_name, "r")

board = [False for i in range(53)]

primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
perfect_squares = [i*i for i in range(3,8)]
rule9_moves = [[6,7,8],[11,12,13],[16,17,18],[21,22,23],[26,27,28],[34,35,36],[39,40,41],[44,45,46],[49,50,51]]

def move(marker, roll):
    if (board[marker+roll]): return marker
    if (marker+roll > 52): return marker
    if (marker+roll in primes):
        for i in range(1, 7):
            if (marker+roll+i <= 52 and board[marker+roll+i]):
                board[marker+roll+i-1] = True
                board[marker] = False
                return marker+roll+i-1
        board[marker+roll+6] = True
        board[marker] = False
        return marker+roll+6
    elif (marker+roll in perfect_squares):
        for i in range(1, 7):
            if (board[marker+roll-i] and marker+roll-i != marker):
                board[marker+roll-i+1] = True
                board[marker] = False
                return marker+roll-i+1
        board[marker+roll-6] = True
        board[marker] = False
        return marker+roll-6
    else:
        moves = [i for i in range(marker, marker+roll+1)]
        for r in rule9_moves:
            if set(r).issubset(set(moves)):
                new_move = marker - (marker % roll) + roll
                if (board[new_move]):
                    return marker
                else:
                    board[new_move] = True
                    board[marker] = False
                    return new_move
        board[marker+roll] = True
        board[marker] = False
        return marker+roll

with open(file_name, "r") as test_cases:
    for line in test_cases:
        line = line.strip().split()
        opp_markers = [int(line[i]) for i in range(3)]
        for m in opp_markers: board[m] = True
        player_markers = [int(line[i]) for i in range(3, 6)]
        for m in player_markers: board[m] = True
        num_of_rolls = int(line[6])
        rolls = []
        for i in range(7,7+num_of_rolls): rolls.append(int(line[i]))
        turn = 0
        for roll in rolls:
            if (turn == 0):
                selected_marker = min(opp_markers)
                selected_marker_index = opp_markers.index(selected_marker)
                next_move = move(selected_marker, roll)
                if next_move >= 52:
                    opp_markers.remove(selected_marker)
                else:
                    opp_markers[selected_marker_index] = next_move
                turn = 1
            else:
                selected_marker = min(player_markers)
                selected_marker_index = player_markers.index(selected_marker)
                next_move = move(selected_marker, roll)
                if next_move >= 52:
                    player_markers.remove(selected_marker)
                else:
                    player_markers[selected_marker_index] = next_move
                turn = 0
        print(sum(opp_markers), sum(player_markers))
        board = [False for i in range(53)]