#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_SIZE 5

int stack[MAX_SIZE];
int top = -1;

int isInteger(char *str) {
    if (*str == '-' || *str == '+') str++;
    if (!*str) return 0;
    while (*str) {
        if (!isdigit(*str)) return 0;
        str++;
    }
    return 1;
}

void push(int x) {
    if (top >= MAX_SIZE - 1) {
        printf("Stack overflow\n");
    } else {
        stack[top++] = x;
    }
}

void peek() {
    if (top == -1) {
        printf("Stack is empty\n");
    } else {
        printf("%d\n", stack[top]);
    }
}

int main() {
    int n;
    if (scanf("%d\n", &n) != 1 || n < 1 || n > 1000) {
        printf("Invalid input\n");
        return 0;
    }

    char line[100];
    for (int i = 0; i < n; i++) {
        if (scanf("%s",line[i]) !=1) {
            printf("Invalid input\n");
            continue;
        }

        char *cmd = strtok(line, " \n");
        if (!cmd) {
            printf("Invalid input\n");
            continue;
        }

        if (strcmp(cmd, "push") == 0) {
            char *numStr = strtok(NULL, " \n");
            if (!numStr || !isInteger(numStr)) {
                printf("Invalid input\n");
                continue;
            }
            int x = atoi(numStr);
            push(x);
        } else if (strcmp(cmd, "peek") == 0) {
            peek();
        } else {
            printf("Invalid input\n");
        }
    }

    return 0;
}