#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define CAPACITY 5

int stack[CAPACITY];
int top = -1;

void push(int value) {
    if (top == CAPACITY - 1) {
        printf("Stack overflow\n");
    } else {
        top++;
        stack[top] = value;
    }
}

void peek() {
    if (top == -1) {
        printf("Stack is empty\n");
    } else {
        printf("%d\n", stack[top]);
    }
}

int isInteger(char *str) {
    if (*str == '-' || *str == '+') str++;  
    if (!*str) return 0; 
    while (*str) {
        if (!isdigit(*str)) return 0;
        str++;
    }
    return 1;
}

int main() {
    int n;
    scanf("%d", &n);
    getchar(); 
    char command[100];
    for (int i = 0; i < n; i++) {
        fgets(command, sizeof(command), stdin);
        command[strcspn(command, "\n")] = '\0'; // remove newline

        char *token = strtok(command, " ");

        if (token == NULL) {
            printf("Invalid input\n");
            continue;
        }

        if (strcmp(token, "push") == 0) {
            char *numStr = strtok(NULL, " ");
            if (numStr == NULL || !isInteger(numStr)) {
                printf("Invalid input\n");
            } else {
                int value = atoi(numStr);
                push(value);
            }
        } 
        else if (strcmp(token, "peek") == 0) {
            char *extra = strtok(NULL, " "); // check if extra words present
            if (extra != NULL) {
                printf("Invalid input\n");
            } else {
                peek();
            }
        } 
        else {
            printf("Invalid input\n");
        }
    }

return 0;
}