#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
    int n;
    if (!(cin >> n) || n < 1 || n > 1000) {
        cout << "Invalid input" << endl;
        return 0;
    }
    cin.ignore(); // ignore remaining newline after n

    int stack[5];
    int top = -1;

    for (int i = 0; i < n; i++) {
        string line;
        getline(cin, line);

        if (line.substr(0, 5) == "push ") {
            string numStr = line.substr(5);
            int num;
            stringstream ss(numStr);
            if (!(ss >> num) || !(ss.eof())) {
                cout << "Invalid input" << endl;
                continue;
            }
            if (top == 4) {
                cout << "Stack overflow" << endl;
            } else {
                stack[++top] = num;
            }
        } else if (line == "peek") {
            if (top == -1) {
                cout << "Stack is empty" << endl;
            } else {
                cout << stack[top] << endl;
            }
        } else {
            cout << "Invalid input" << endl;
        }
    }

    return 0;
}