# Chris Chen
# Legion of Learners
# Senior Division
# Contest #1 2019-2020

import sys

prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

vertical = [1, 2, 7, 12, 17, 22, 23, 24, 25, 27, 28, 29, 30, 35, 40, 45, 50, 51]

verbose = False
def simulate(opponent, player, roll, current): # didn't realize it was only moving first player token
    marker = None
    if current < 0:
        marker = player[~current]
    else:
        marker = opponent[current]
    if verbose: print(f'Handling roll {roll} for {marker}.')
    if marker+roll in player or marker+roll in opponent or marker+roll > 52:
        if verbose: print(f'Marker will land on player or opponent marker or goes over 52. Marker unset.')
        return
    if marker+roll in prime:
        if verbose: print(f'Marker will land on {marker+roll}, a prime. Attempting to move 6 ahead.')
        for i in range(1, 7):
            if marker+roll+i in player or marker+roll+i in opponent or marker+roll+i > 52:
                marker = marker+roll+i-1
                if verbose: print(f'Blockage at {marker+1}, staying at {marker}.')
                break
    elif ((marker+roll)**(1/2)).is_integer() and (marker+roll) > 4:
        if verbose: print(f'Marker will land on a {marker+roll}, a perfect square. Attempting to move 6 back.')
        for i in range(-1, -7, -1):
            if marker+roll+i in player or marker+roll+i in opponent or marker+roll+i < 1:
                marker = marker+roll+i+1
                if verbose: print(f'Blockage at {marker-1}, staying at {marker}.')
                break
    elif any(((i not in vertical) and (i+1 in vertical)) for i in range(marker, marker+roll-1)):
        if verbose: print(f'Moved from horizontal to vertical.')
        multiple = (marker)//roll+1
        while multiple*roll <= marker+roll:
            if multiple*roll not in player and multiple*roll not in opponent and multiple*roll <= 52:
                if verbose: print(f'Square {multiple*roll} is suitable.')
                marker = multiple*roll
                break
            multiple += 1
    else:
        marker = marker+roll
    if verbose: print(f'Marker set to {marker}.')
    if current < 0:
        if marker == 52:
            player.pop(~current)
            return
        player[~current] = marker
    else:
        if marker == 52:
            player.pop(~current)
            return
        opponent[current] = marker

with open('testdata.txt', 'r') as sys.stdin:
    for i in range(5):
        numbers = [int(i) for i in input().split()]
        opponent, numbers = sorted(numbers[:3]), numbers[3:]
        player, numbers = sorted(numbers[:3]), numbers[3:]
        r, rolls = numbers[0], numbers[1:]
        for i, roll in enumerate(rolls):
            simulate(opponent, player, roll, -1)
            player.sort()
            while player[0] == -1:
                player.pop(0)
        print(*player)