import java.util.Scanner; 

class Node {
    String content; 
    Node successor; 
    
    public Node (String x, Node rest) {
        content = x;
        successor = rest; 
    }
}
public class LinkedList {
    Node startOfList; 
    public LinkedList () {
        startOfList = null; 
    }
    public boolean push (String x) {
        Node newNode = new Node(x, startOfList);
        startOfList = newNode; 
        return true;
    }
    public String pop () {
        String result = startOfList.content; 
        startOfList = startOfList.successor; // pointer wird einen Schritt zurueckverfolgt          
        return result;
    }




    

    public void printStack() {
        Node currentNode = startOfList;
        while (currentNode != null) {
            System.out.println(currentNode.content);
            currentNode = currentNode.successor; 
        }
        System.out.println ("end of stack");
    }



    public static void main (String args[]) {
        Scanner scanner = new Scanner (System.in);
        LinkedList stack = new LinkedList ();
        while (scanner.hasNext()) {
            String s = scanner.next();
            if (s.toLowerCase().equals("pop")) {
                System.out.println(stack.pop());
            }
            else if (s.toLowerCase().equals("push")) {
                String element = scanner.next();
                if (stack.push(element) == false) {
                    System.err.println("Stack capacity reached");
                }                
            }
            else if (s.toLowerCase().equals("print")) {
                stack.printStack();             
            }
            else {
                System.err.println("unknown command: " + s);
            }
        }
    }

}