def stack_simulation():
    t = int(input().strip())   # number of commands
    stack = []
    capacity = 100

    for _ in range(t):
        command = input().strip().split()

        if command[0] == "push":
            if len(command) != 2 or not command[1].lstrip('-').isdigit():
                print("Invalid input")
                continue
            x = int(command[1])
            if len(stack) < capacity:
                stack.append(x)
            else:
                print("Stack overflow")

        elif command[0] == "pop":
            if stack:
                print(stack.pop())
            else:
                print("Stack is empty")

        elif command[0] == "display":
            if stack:
                print(" ".join(map(str, reversed(stack))))
            else:
                print("Stack is empty")

        else:
            print("Invalid input")


# Run the program
if __name__ == "__main__":
    stack_simulation()