#include <iostream>
using namespace std;
#include <string>
#include <stack>
#include <queue>
#include <vector>
const int N = 1e5;
int q[N] ,hh= 0,tt = -1;
// 入队
//q[++tt] = x;
//
弹出对头
//
//hh++;
//
队头的值
//q[hh];
是否为空
//if(hh<=tt)
//{
//
//}
int main()
{
int n;
int op;
cin >> n;
while(n--)
{
cin>>op;
if(op==3)
{
cout<<tt-hh + 1<<endl;
}
else if(op==2)
{
if(hh<=tt)
{
cout<<q[hh++]<<endl;
}
else
{
cout<<"Invalid"<<endl;
}
}
else
{
int num;
cin >> num;
q[++tt] = num;
}
}
return 0;
}