#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 5

int main() 
{
    int n;
    if (scanf("%d", &n) != 1) 
    {
        printf("Invalid input\n");
        return 0;
    }
    if(n<1||n>1000)
    {
        printf("Invalid input\n");
        return 0;
    }
    int stack[MAX];
    int top=-1;
    char command[20];
    for(int i=0;i<n;i++)
    {
        scanf("%s",command);
        if(strcmp(command,"push")==0)
        {
          int x;
          if(scanf("%d",&x)!=1)
          {
              printf("Invalid input\n");
              return 0;
          }
          if(top==MAX-1)
          {
              printf("stack overflow\n")
          }
          else
          {
              stack[++top]=x;
          }
          }
          else if(strcmp(command,"peek")==0)
          {
              if(top==-1)
              {
                  printf("stack is empty\n");
              }
              else
              {
                  printf("%d\n",stack[top]);
              }
              }else
              {
                  printf("Invalid input\n");
                  return 0;
              }
          }
          return 0;
        }
    }