#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000 +7;
int n,m,k,idx,tot,endd;
struct _nd{
int cnt,derd,len;
string name[N];
int l[N],post[N],r[N];
}ned[N];
struct node{
string name;
int classic,begin,end;
}q[N];
map<string,int> mp,used;
string ch,posted;
signed main()
{
freopen("struct3.in","r",stdin);
freopen("struct.out","w",stdout);
mp["byte"]=1,mp["short"]=2,mp["int"]=3,mp["long"]=4;
ned[1].derd=1,ned[1].len=1;
ned[2].derd=2,ned[2].len=2;
ned[3].derd=4,ned[3].len=4;
ned[4].derd=8,ned[4].len=8;
idx=4;
scanf("%lld",&n);
for(int kl=1;kl<=n;kl++)
{
int opt; scanf("%lld",&opt);
if(opt==1)
{
cin>>ch; scanf("%lld",&k); string h=ch;
mp[ch]=++idx;
int len=0;
for(int i=1;i<=k;i++)
{
cin>>ch>>posted;
ned[idx].cnt++; int cnt=mp[ch];
ned[idx].name[ned[idx].cnt]=posted;
ned[idx].post[ned[idx].cnt]=mp[ch];
ned[idx].l[ned[idx].cnt]=ceil(len*1.000/ned[cnt].derd)*ned[cnt].derd;
ned[idx].r[ned[idx].cnt]=ned[idx].l[ned[idx].cnt]+ned[cnt].len-1;
ned[idx].derd=max(ned[idx].derd,ned[cnt].derd);
len=ned[idx].r[ned[idx].cnt]+1;
}
len=ceil(len*1.000/ned[idx].derd)*ned[idx].derd;
ned[idx].len=len;
printf("%lld %lld\n",len,ned[idx].derd);
}
else if(opt==2)
{
cin>>ch>>posted;
int tmp=mp[ch];
q[++tot].classic=tmp;
used[posted]=tot;
q[tot].name=posted;
q[tot].begin=ceil(endd*1.000/ned[tmp].derd)*ned[tmp].derd;
q[tot].end=q[tot].begin+ned[tmp].len-1;
endd=q[tot].end+1;
printf("%lld\n",q[tot].begin);
}
else if(opt==3)
{
cin>>ch;
int res=0,classic=0;
for(int i=0;i<(int)ch.size();i++)
{
int lo=i; posted.clear();
while(lo<(int)ch.size() && ch[lo]!='.') posted+=ch[lo],lo++;
if(i==0) res+=q[used[posted]].begin,classic=q[used[posted]].classic;
else
{
for(int j=1;j<=ned[classic].cnt;j++)
{
if(ned[classic].name[j]==posted)
{
res+=ned[classic].l[j];
classic=ned[classic].post[j];
}
}
}
posted.clear(); i=lo;
}
printf("%lld\n",res);
}
else if(opt==4)
{
scanf("%lld",&opt);
int classic=0; posted.clear();
for(int i=1;i<=tot;i++)
if(q[i].begin<=opt && opt<=q[i].end) { classic=i; break; }
if(classic==0)
{
puts("ERR");
continue;
}
opt-=q[classic].begin;
posted+=q[classic].name; classic=q[classic].classic; bool sds=0;
while(1)
{
int flag=0;
for(int i=1;i<=ned[classic].cnt;i++)
{
if(ned[classic].l[i]<=opt && opt<=ned[classic].r[i])
{
flag=1; sds=1;
posted+='.'; opt-=ned[classic].l[i];
posted+=ned[classic].name[i];
classic=ned[classic].post[i];
}
if(ned[classic].r[i-1]<opt && opt<ned[classic].l[i])
{
sds=0;
break;
}
}
if(ned[classic].cnt!=0 && ned[classic].r[ned[classic].cnt]<opt && !flag)
{
sds=0;
break;
}
if(!flag || !sds) break;
}
if(!sds) puts("ERR");
else cout<<posted<<endl;
}
}
return 0;
}