#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
#define s(t) scanf("%d",&t)
#define p(t) printf("%d\n",t)
int main()
{
int n,p,numbookstoremove;
char cb[16];
s(n);
stack<int> minbookstoremove;
stack<int> probstosolve;
stack<string> books;
string booktopick;
while(n--)
{
s(p);
if(p==-1)
{
numbookstoremove=minbookstoremove.top();
while(!minbookstoremove.empty() && minbookstoremove.top()!=0)
{
minbookstoremove.pop();
probstosolve.pop();
books.pop();
}
booktopick=books.top();
printf("%d %s\n",numbookstoremove,booktopick.c_str());
minbookstoremove.pop();
probstosolve.pop();
books.pop();
}
else
{
scanf("%s",cb);
if(p!=0)
{
string b(cb);
if(minbookstoremove.empty())
{
minbookstoremove.push(0);
books.push(b);
probstosolve.push(p);
}
else
{
if(p<probstosolve.top())
{
minbookstoremove.push(0);
books.push(b);
probstosolve.push(p);
}
else
{
minbookstoremove.push(minbookstoremove.top()+1);
books.push(b);
probstosolve.push(probstosolve.top());
}
}
}
}
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHN0ZGlvLmg+CiNpbmNsdWRlPHN0cmluZy5oPgojaW5jbHVkZTxzdGFjaz4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIHModCkgc2NhbmYoIiVkIiwmdCkKI2RlZmluZSBwKHQpIHByaW50ZigiJWRcbiIsdCkKCmludCBtYWluKCkKewogIGludCBuLHAsbnVtYm9va3N0b3JlbW92ZTsKICBjaGFyIGNiWzE2XTsKICBzKG4pOwogIHN0YWNrPGludD4gbWluYm9va3N0b3JlbW92ZTsKICBzdGFjazxpbnQ+IHByb2JzdG9zb2x2ZTsKICBzdGFjazxzdHJpbmc+IGJvb2tzOwogIHN0cmluZyBib29rdG9waWNrOwogIHdoaWxlKG4tLSkKICB7CiAgICBzKHApOwogICAgaWYocD09LTEpCiAgICB7CiAgICAgIG51bWJvb2tzdG9yZW1vdmU9bWluYm9va3N0b3JlbW92ZS50b3AoKTsKICAgICAgd2hpbGUoIW1pbmJvb2tzdG9yZW1vdmUuZW1wdHkoKSAmJiBtaW5ib29rc3RvcmVtb3ZlLnRvcCgpIT0wKQogICAgICB7CiAgICAgICAgbWluYm9va3N0b3JlbW92ZS5wb3AoKTsKICAgICAgICBwcm9ic3Rvc29sdmUucG9wKCk7CiAgICAgICAgYm9va3MucG9wKCk7CiAgICAgIH0KICAgICAgYm9va3RvcGljaz1ib29rcy50b3AoKTsKICAgICAgcHJpbnRmKCIlZCAlc1xuIixudW1ib29rc3RvcmVtb3ZlLGJvb2t0b3BpY2suY19zdHIoKSk7CiAgICAgIG1pbmJvb2tzdG9yZW1vdmUucG9wKCk7CiAgICAgIHByb2JzdG9zb2x2ZS5wb3AoKTsKICAgICAgYm9va3MucG9wKCk7CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgIHNjYW5mKCIlcyIsY2IpOwogICAgICBpZihwIT0wKQogICAgICB7CiAgICAgICAgc3RyaW5nIGIoY2IpOwogICAgICAgIGlmKG1pbmJvb2tzdG9yZW1vdmUuZW1wdHkoKSkKICAgICAgICB7CiAgICAgICAgICBtaW5ib29rc3RvcmVtb3ZlLnB1c2goMCk7CiAgICAgICAgICBib29rcy5wdXNoKGIpOwogICAgICAgICAgcHJvYnN0b3NvbHZlLnB1c2gocCk7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICBpZihwPHByb2JzdG9zb2x2ZS50b3AoKSkKICAgICAgICAgIHsKICAgICAgICAgICAgbWluYm9va3N0b3JlbW92ZS5wdXNoKDApOwogICAgICAgICAgICBib29rcy5wdXNoKGIpOwogICAgICAgICAgICBwcm9ic3Rvc29sdmUucHVzaChwKTsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UKICAgICAgICAgIHsKICAgICAgICAgICAgbWluYm9va3N0b3JlbW92ZS5wdXNoKG1pbmJvb2tzdG9yZW1vdmUudG9wKCkrMSk7CiAgICAgICAgICAgIGJvb2tzLnB1c2goYik7CiAgICAgICAgICAgIHByb2JzdG9zb2x2ZS5wdXNoKHByb2JzdG9zb2x2ZS50b3AoKSk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICB9CiAgfQogIHJldHVybiAwOwp9