#!/usr/bin/env python3 
'''Around - round text display  Andy Harrington'''

def do(wait, inLines):
    '''inLines is input lines after first'''
    inLines = list(map(str.split, inLines))
    inLines.reverse() # so easy pop
    words = []  # will be list of all words 
    beats = []  # will be full list of beats/word
    lineTot = [] # will be cumulative times at end of each line
    tot = 0
    while inLines:
        words += inLines.pop()
        lineBeats = list(map(int, inLines.pop()))
        tot += sum(lineBeats)
        lineTot.append(tot)
        beats += lineBeats
    tup = [] # will contain list of (start time, voice 0/1, word) 
    time = 0
    for b, w in zip(beats, words):
        tup.append((time, 0, w))
        tup.append((time+wait, 1, w))
        time += b
    tup.sort()
    tup.reverse()     # for easy pop
    lineTot.reverse()
    timeLim = 0 # for last line at top of loop, then current line
    while lineTot:
        vDone = [[], []] #(time, col) seq for both voices
        timeLim = lineTot.pop()
        vStr = ['', ''] # parts of two voices on current line so far
        while tup and tup[-1][0] < timeLim: # more for this line
            t, vn, w = tup.pop()
            col = minShift(vStr, vDone, t, vn) # rule 3
            if tup and tup[-1][0] == t: # rule 2
               col = max(col, minShift(vStr, vDone, t, 1-vn)) #match other voice
            vStr[vn] += (col-len(vStr[vn]))*'+' + w  # add at col
            vDone[vn].append((t, col)) # record data for later rule 3

        for voice in vStr:
            print(voice) 

def minShift(vStr, vDone, t, vn): # rule 3
    
    m = len(vStr[vn]) # after part so far
    if m: m +=1 #need '+' if not at start
    for tOld, col in vDone[1-vn]:
        m = max(m, col + (t-tOld)) 
    return m

def hotCross(): # simple test
    hc ='''Hot cross buns! = Hot cross buns! =
    2 2 2 2 2 2 2 2
    One a pen- ny, Two a pen- ny, Hot cross buns!
    1 1 1 1 1 1 1 1 2 2 2 2'''
    do(8, hc.splitlines())

def fromInput():
    L, wait = map( int, input().split())
    do (wait, [input() for i in range(2*L)])

##hotCross()
fromInput()
