#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
stack<int> st;
int cnt[100010]={0},table[316]={0};
void Push(int x){
cnt[x]++;
table[x/316]++;
st.push(x);
}
void Pop(){
int top=st.top();
st.pop();
cnt[top]--;
table[top/316]--;
cout<<top<<endl;
}
void Peek(int p){
int i=0,sum=0;
while(sum+table[i]<p) sum+=table[i++];
i=i*316;
while(sum+cnt[i]<p) sum+=cnt[i++];
cout<<i<<endl;
}
int main(){
char op[15];
int n,num;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",op);
if(strcmp(op,"Push")==0){
scanf("%d",&num);
Push(num);
}else if(strcmp(op,"Pop")==0){
if(st.empty())
cout<<"Invalid"<<endl;
else
Pop();
}else{
if(st.empty())
cout<<"Invalid"<<endl;
else
Peek((st.size()+1)/2);
}
}
return 0;
}