// Student Name: Leelund Swanson

// School Name: Harmony School of Excellence

// ACSL division: Intermediate

 

import java.io.*;

import java.util.*;

 

public class stacks_queues {

 

    public static void main(String[] args) throws IOException

    {

        BufferedReader file=new BufferedReader(new FileReader("stacks.in"));

String holder;

        for(int q=0;q<5;q++)

        {

            holder="ABCDE";

 

            StringTokenizer st=new StringTokenizer(file.readLine(),",");

           boolean stack;

            if(st.nextToken().equals("S"))

            stack=true;

            else

            stack=false;

 

          

 

           while(st.hasMoreTokens())

           {

               String com=st.nextToken();

 

               if(com.equals("SWH"))

                   stack=swh(stack);

               else

               {

                   StringTokenizer st2=new StringTokenizer(com);

                   String com1=st2.nextToken();

                   String com2=st2.nextToken();

 

                   if(com1.equals("POP"))

                       holder=pop(holder,Integer.parseInt(com2),stack);

                   else if(com1.equals("PSH"))

                       holder=push(holder,com2);

                   else if(com1.equals("DUP"))

                       holder=dup(holder,Integer.parseInt(com2));

                   else if(com1.equals("SWP"))

                       holder=swap(holder,Integer.parseInt(com2),stack);

                   else if(com1.equals("CRC"))

                       holder=circ(holder,Integer.parseInt(com2),stack);

                   else if(com1.equals("PIN"))

                       holder=pin(holder,com2);

                   else if(com1.equals("PRT"))

                       print(holder,Integer.parseInt(com2),stack);

               }

 

           }

 

 

 

        }

 

    }

    public static String pop(String a,int num, boolean stack)

    {

        if(stack)

          a=a.substring(0, a.length()-num);

        else

          a=a.substring(num);

 

        return a;

    }

 

    public static String push(String a, String b)

    {

        a+=b;

 

        return a;

    }

 

    public static String dup(String a, int howMany)

    {

        a+=a.substring(0,howMany);

 

        return a;

    }

 

    public static String swap(String a, int howMany, boolean stack)

    {

        if(stack==true)

        {

            String temp=a.substring(a.length()-howMany);

            a=a.substring(0,a.length()-howMany);

            a+=a.substring(0, howMany);

            a=a.substring(howMany);

            a=temp+a;

        }

        else

        {

            String temp=a.substring(0,howMany);

            a=a.substring(howMany);

            a=a.substring(a.length()-howMany)+a.substring(0,a.length()-howMany);

            a+=temp;

        }

 

        return a;

   

    }

 

    public static boolean swh(boolean stack)

    {

        if(stack==true)

            return false;

        else

            return true;

    }

 

    public static String circ(String a, int howMany, boolean stack)

    {

        if(stack)

        {

            for(int i=0;i<howMany;i++)

            {

                String temp=a.substring(a.length()-1);

                a=a.substring(0, a.length()-1);

                a=temp+a;

            }

        }

        else

            for(int i=0;i<howMany;i++)

            {

                String temp=a.substring(0, 1);

                a=a.substring(1);

                a+=temp;

            }

        return a;

    }

 

    public static String pin(String a, String b)

    {

        b+=a;

       

        return b;

    }

 

    public static void print(String a,int howMany, boolean stack)

    {

        String temp="";

        if(stack)

            temp=a.substring(a.length()-howMany);

        else

            temp=a.substring(0,howMany);

        for(int i=0;i<temp.length();i++)

        {

            if(i!=temp.length()-1)

                System.out.print(temp.charAt(i)+",");

            else

                System.out.println(temp.charAt(i));

        }

    }

}