#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 100 

int main() {
    int stack[MAX];
    int top = -1;
    int t;

    if (scanf("%d", &t) != 1 || t < 1 || t > 100000) {
        printf("Invalid input\n");
        return 0;
    }
    getchar(); 

    for (int i = 0; i < t; i++) {
        char command[100];
        if (fgets(command, sizeof(command), stdin) == NULL) {
            printf("Invalid input\n");
            continue;
        }

        command[strcspn(command, "\n")] = '\0';

        if (strncasecmp(command, "push ", 5) == 0) {
            char *numStr = command + 5;
            char *endptr;
            long val = strtol(numStr, &endptr, 10);

            if (*endptr != '\0') {
                printf("Invalid input\n");
                continue;
            }

            if (top == MAX - 1) {
                printf("Stack overflow\n");
            } else {
                stack[++top] = (int)val;
            }

        } else if (strcasecmp(command, "pop") == 0) {
            if (top == -1) {
                printf("Stack is empty\n");
            } else {
                printf("%d\n", stack[top--]); 
            }

        } else if (strcasecmp(command, "display") == 0) {
            if (top == -1) {
                printf("Stack is empty\n");
            } else {
                for (int j = top; j >= 0; j--) {
                    printf("%d", stack[j]);
                    if (j > 0) printf(" ");
                }
                printf("\n");
            }

        } else {
            printf("Invalid input\n");
        }
    }
    return 0;
}