#include<bits/stdc++.h>
#define int long long
using namespace std;
struct s
{
string sum;
int ls,rs;
}no[2400005];
int tot,root[100005],len[100005],tot2;
int Newno()
{
return ++tot;
}
int build(int l,int r)
{
int nd=Newno();
if(l==r) return nd;
int mid=(l+r)/2;
no[nd].ls=build(l,mid);
no[nd].rs=build(mid+1,r);
return nd;
}
int add(int w,int l,int r,int wh,char x)
{
int nd=Newno();
if(l==r)
{
no[nd].sum+=x;
return nd;
}
int mid=(l+r)/2;
if(wh<=mid)
{
no[nd].ls=add(no[w].ls,l,mid,wh,x);
no[nd].rs=no[w].rs;
}
else
{
no[nd].ls=no[w].ls;
no[nd].rs=add(no[w].rs,mid+1,r,wh,x);
}
return nd;
}
string getsum(int w,int l,int r,int x)
{
if(l==r)return no[w].sum;
int mid=(l+r)/2;
if(x<=mid) return getsum(no[w].ls,l,mid,x);
else return getsum(no[w].rs,mid+1,r,x);
}
signed main()
{
int n;
cin>>n;
root[tot2]=build(1,n);
for(int i=1;i<=n;i++)
{
char op;
cin>>op;
if(op=='T')
{
char x;
cin>>x;
len[++tot2]=len[tot2-1]+1;
root[tot2]=add(root[tot2-1],1,n,len[tot2],x);
}
if(op=='U')
{
int x;
cin>>x;
root[++tot2]=root[tot2-x-1];
len[tot2]=len[tot2-x-1];
}
if(op=='Q')
{
int x;
cin>>x;
cout<<getsum(root[tot2],1,n,x)<<endl;
}
}
return 0;
}
求助啊各路大神