# Viju Matthew

# 4/09/10

# Enloe HS

# INT - 5

# Contest #4 - Stacks/Queues

# Potter

 

def PSH(ite,stringy):  # SAME BEGINS #ite=thing pushed, stringy=where it's pushed

    temp=ite+stringy

    return temp

 

def DUP(amt,stringy): #amt=number of things dup, stringy=thing dup from

    temp=stringy[-amt:]+stringy

    return temp

 

def SWP(amt,stringy): #amt=number of things swp, stringy=thing swp from

    temp2=stringy[-amt:]

    temp1=stringy[:amt]

    stringy=stringy[amt:]

    stringy=stringy[:-amt]

    temp=temp2+stringy+temp1

    return temp   

 

def PIN(ite,stringy): #ite=thing pushed, stringy=where it's pushed

    temp=stringy+ite

    return temp

 

def SWH(thingy):  #DIFF BEGINS #thingy is input list

    if (thingy[0]=='S'): temp='Q'

    if (thingy[0]=='Q'): temp='S'

    return temp

 

def POP(amt,thingy,stringy): #thingy=input list, stringy=stack/queue, amt=number

    if (thingy[0]=='S'): temp=stringy[amt:]

    if (thingy[0]=='Q'): temp=stringy[:-amt]

    return temp

 

def CRC(amt,thingy,stringy): #thingy=input list, stringy=stack/queue, amt=number

    if (thingy[0]=='S'): temp=stringy[amt:]+stringy[:amt]

    if (thingy[0]=='Q'): temp=stringy[-amt:]+stringy[:-amt]

    return temp

 

def PRT(amt,thingy,stringy):   

    if (thingy[0]=='S'):

        temp1=stringy[:amt]

        temp=temp1[::-1]

    if (thingy[0]=='Q'):

        temp1=stringy[-amt:]

        temp=temp1[::-1]

    return temp

 

def printer(stringy):

    temp=stringy[0]

    for i in range(1,len(stringy)):

        temp=temp+', ' +stringy[i]

    return temp

 

print('Enter Input. All caps please')

 

for i in range(0,5):

   

    qwe=raw_input()

    changer='EDCBA'

 

    for i in range(len(qwe)-1,0,-1):

        if (qwe[i]==' '):

            qwe=qwe[:i]+qwe[i+1:]

          

    qwe=qwe.split(',')

 

    for qwer in qwe:

        if (len(qwer)>3):

            if ((qwer[:3]==('PSH')) or qwer[:3]==('PIN')):

                numby=qwer[3:]

            else:

                numby=int(qwer[3:])

        if (len(qwer)>1):

            if (qwer[:3]=='POP'):

                changer=POP(numby,qwe,changer)

            if (qwer[:3]=='PSH'):

                changer=PSH(numby,changer)

            if (qwer[:3]=='DUP'):

                changer=DUP(numby,changer)

            if (qwer[:3]=='SWP'):

                changer=SWP(numby,changer)

            if (qwer[:3]=='SWH'):

                qwe[0]=SWH(qwe)

            if (qwer[:3]=='CRC'):

                changer=CRC(numby,qwe,changer)

            if (qwer[:3]=='PIN'):

                changer=PIN(numby,changer)

            if (qwer[:3]=='PRT'):

                qwerty=PRT(numby,qwe,changer)

 

 

    print(printer(qwerty))