// editor1

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 5
#define BUF 256

int stack[MAX];
int top = -1;

char *trim(char *s) {
    char *end;
    while (isspace((unsigned char)*s)) s++;
    if (*s == '\0') return s;
    end = s + strlen(s) - 1;
    while (end > s && isspace((unsigned char)*end)) end--;
    *(end + 1) = '\0';
    return s;
}

int main(void) {
    char buf[BUF];

    /* Read first line and parse n */
    if (!fgets(buf, sizeof(buf), stdin)) return 0;
    char *line = trim(buf);
    char *endptr;
    long n = strtol(line, &endptr, 10);
    /* ensure the whole trimmed line was
    while (isspace((unsigned char)*endptr)) endptr++;
    if (line == endptr || *endptr != '\0' || n < 1) {
        printf("Invalid input\n");
        return 0;
    }

    for (long i = 0; i < n; i++) {
        if (!fgets(buf, sizeof(buf), stdin)) {
    
            printf("Invalid input\n");
            return 0;
        }

        char *cmd = trim(buf);
        if (cmd[0] == '\0') {                
            printf("Invalid input\n");
            continue;
        }

    
        char *token = strtok(cmd, " \t");
        if (token == NULL) {
            printf("Invalid input\n");
            continue;
        }

        if (strcmp(token, "peek") == 0) {
        
            char *extra = strtok(NULL, " \t");
            if (extra != NULL) {
                printf("Invalid input\n");
            } else {
                if (top == -1) printf("Stack is empty\n");
                else printf("%d\n", stack[top]);
            }
        } else if (strcmp(token, "push") == 0) {
            
            char *arg = strtok(NULL, " \t");
            char *extra = strtok(NULL, " \t");
            if (arg == NULL || extra != NULL) {       
                printf("Invalid input\n");
                continue;
            }
            
            char *end2;
            long val = strtol(arg, &end2, 10);
            if (end2 != '\0') {         
                printf("Invalid input\n");
                continue;
            }
            
            if (top == MAX - 1) {
                printf("Stack overflow\n");
            } else {
                stack[++top] = (int)val;
            }
        } else {
            printf("Invalid input\n");
        }
    }

return 0;
}