//David Goldman

//Freehold H.S.

//Freehold, NJ

//School code : 1002

//Junior Division

//Language : Java

//Contest 4

//ACSL Stacks and Queues

//Description : The structure will initially have the items A, B, C, D, E in

// that order. The input items will also be those letters. Each line of input

// contains a S or Q telling the structure as stack or queue, followed by a list

// of commands. Each line will end with the PRT command, which will print the

// items required by the command.

 

import java.util.*;

 

 

public class DG_C4_JUN

{

 

            public static void main(String[] args)

           

            {

                        Scanner scan;

                        String command;

                        String type = "";

                        int x;

                        String duplicate = "";

                        String completed = "";

                        String n = "";

                        int length = 0;

                        final int MAX = 5;

                        String message = "ABCDE";

                       

                       

                       

                        for (int i = 1; i <= MAX; i++)

                        {

                                    x = 0;

                                    length = 0;

                                    message = "ABCDE";

                                    n = "";

                                    duplicate = "";

                                    completed = "";

                                    type = "";

                                    command = "";

                                   

                                    scan = new Scanner(System.in);

                                    System.out.print(i + ". ");

                                    scan = new Scanner(scan.nextLine());

                                   

                                   

                                    type = scan.next();

                                   

                                    while (scan.hasNext())

                                                {

                                                           

                                                            if (type.equalsIgnoreCase("S"))

                                                                        {         

                                                                                    command = scan.next();

                                               

                                                                                    if (command.equalsIgnoreCase("POP"))

                                                                                    {

                                                                                                x = scan.nextInt();

                                                                                                message = message.substring(0,(message.length()-x));

                                                                                    }

                                                                                    if (command.equalsIgnoreCase("PSH"))

                                                                                    {

                                                                                                n = scan.next();

                                                                                                n = n.toUpperCase();

                                                                                                message = message+n;

                                                                                    }         

                                                                                    if (command.equalsIgnoreCase("DUP"))

                                                                                    {

                                                                                                x= scan.nextInt();

                                                                                                duplicate = message.substring(0,x);

                                                                                                message = message + duplicate;

                                                                                    }

                                                                                    if (command.equalsIgnoreCase("SWP"))

                                                                                    {

                                                                                                x = scan.nextInt();

                                                                                                length = message.length();

                                                                                                message = message.substring(length-x,length) + message.substring(x, length-x)

                                                                                                + message.substring(0,x);

                                                                                    }

                                                           

                                                                                    if (command.equalsIgnoreCase("SWH"))

                                                                                                type = "Q";

                                                                                    if (command.equalsIgnoreCase("PRT"))

                                                                                    {

                                                                                                x = scan.nextInt();  

                                                                                                length = message.length();

                                                                                                message = message.substring(length-x,length);

                                                                                    }

                                                                        }

                       

                                                            if (type.equalsIgnoreCase("Q"))

                                                                        {

                                                                                    command = scan.next();

                                                                                    if (command.equalsIgnoreCase("POP"))

                                                                                    {

                                                                                                x = scan.nextInt();

                                                                                                length = message.length();

                                                                                                message = message.substring(x,length);

                                                                                    }

                                                                                    if (command.equalsIgnoreCase("PSH"))

                                                                                    {

                                                                                                n = scan.next();

                                                                                                n = n.toUpperCase();

                                                                                                message = message+n;

                                                                                    }         

                                                                                    if (command.equalsIgnoreCase("DUP"))

                                                                                    {

                                                                                                x= scan.nextInt();

                                                                                                duplicate = message.substring(0,x);

                                                                                                message = message + duplicate;

                                                                                    }

                                                                                    if (command.equalsIgnoreCase("SWP"))

                                                                                    {

                                                                                                x = scan.nextInt();

                                                                                                length = message.length();

                                                                                                message = message.substring(length-x,length) + message.substring(x, length-x)

                                                                                                + message.substring(0,x);

                                                                                    }

                                                           

                                               

                                                                                    if (command.equalsIgnoreCase("SWH"))

                                                                                                type = "S";

                                                                                    if (command.equalsIgnoreCase("PRT"))

                                                                                    {

                                                                                                x = scan.nextInt();  

                                                                                                message = message.substring(0,x);

                                                                                    }

                                                           

                                                                        }

                       

                                                }

                                   

                                    for (int count = 0; count <= message.length() -1 ; count++)

                                    {

                                                if (count != message.length()-1)

                                                            completed += message.charAt(count) + ", ";

                                                if (count == message.length() -1)

                                                            completed += message.charAt(count);

                                   

                                    }

                                    System.out.print(i + ". ");

                                    System.out.println(completed);

                        }

 

            }

 

}