#include <bits/stdc++.h>
#define int long long
#define ll __int128
#define db double
#define ldb long double
#define vo void
#define endl '\n'
#define il inline
#define re register
#define ve vector
#define p_q priority_queue
#define PII pair<int, int>
#define u_m unordered_map
using namespace std;
#ifdef O2
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
#endif
namespace OI {
template <typename T>
il T read() {
T x = 0, f = 1;
int ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
template <typename TE>
il void write(TE x) {
if (x < 0) {
x = -x;
putchar('-');
}
TE sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + '0');
}
il string read_with_string() {
string s = "";
char ch = getchar();
while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
s += ch;
ch = getchar();
}
return s;
}
il void write_with_string(string s) {
for (int i = 0; i < s.size(); i ++ ) putchar(s[i]);
}
}
namespace COMB {
int fact[200000];
int Triangle[1010][1010];
void Fact(int n, int mod) {
fact[0] = 1;
for (int i = 1; i <= n; i ++ ) fact[i] = ((fact[i - 1]) % mod * (i % mod)) % mod;
}
void Pascal_s_triangle(int n, int mod) {
for (int i = 0; i <= n; i ++ ) Triangle[i][0] = 1;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
Triangle[i][j] = (Triangle[i - 1][j] + Triangle[i - 1][j - 1]) % mod;
}
int pw(int x, int y, int mod) {
int res = 1;
while (y) {
if (y & 1) res = ((res % mod) * (x % mod)) % mod;
x = (x % mod) * (x % mod) % mod;
y >>= 1;
}
return res;
}
int pw(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res *= x;
x *= x;
y >>= 1;
}
return res;
}
int GCD(int x, int y, int mod) {
return __gcd(x, y) % mod;
}
int LCM(int x, int y, int mod) {
return (((x % mod) * (y % mod)) % mod / (GCD(x, y, mod) % mod)) % mod;
}
int C(int n, int m, int mod) {
if (m > n || m < 0) return 0;
return fact[n] * pw(fact[m], mod - 2, mod) % mod * pw(fact[n - m], mod - 2, mod) % mod;
}
int Ask_triangle(int x, int y) {
return Triangle[x][y];
}
}
using namespace OI;
using namespace COMB;
#define IOS 1
const int N = 2e5 + 10;
const int M = 4e5 + 10;
const int inf = 1e12;
#define PSS pair<string, string>
#define id kjrgtjghfjk
int n;
ve<PSS> str[N];
string Name[N];
map<string, int> Id;
int cnt;
int len[N];
int dq[N];
map<int, int> dz;
map<string, int> I;
int now;
map<string, int> qs;
map<int, string> zy;
int a[N], b[N], s[N];
int ccnt;
int sz[N];
il void Init() {
I["byte"] = 1;
I["short"] = 2;
I["int"] = 4;
I["long"] = 8;
cin >> n;
}
il bool ok(string s) {
if (s == "byte") return true;
if (s == "short") return true;
if (s == "int") return true;
if (s == "long") return true;
return false;
}
il void dfs(string lx, string name, string from, string nname) {
if (!ok(lx)) {
int id = Id[lx];
for (int i = 0; i < str[id].size(); i ++ ) {
string next_name = str[id][i].first;
dfs(next_name, name, lx, nname + "." + str[id][i].second);
if (i == 0) {
qs[nname] = b[ccnt];
}
}
} else {
ccnt++;
int id = Id[from];
int DQ = dq[id];
s[ccnt] = I[lx];
a[ccnt] = DQ;
if (ccnt == 1) b[1] = 0;
else
for (int i = 1; i; i ++ )
if (i % a[ccnt] == 0 && b[ccnt - 1] + s[ccnt - 1] <= i) {
b[ccnt] = i;
break;
}
qs[nname] = b[ccnt];
for (int i = b[ccnt]; i <= b[ccnt] + I[lx] - 1; i ++ ) dz[i] = 1, zy[i] = nname;
now = b[ccnt] + I[lx];
}
}
il void Solve() {
while (n -- ) {
int op;
cin >> op;
if (op == 1) {
string struct_name;
int struct_num;
cin >> struct_name >> struct_num;
Name[++cnt] = struct_name;
len[cnt] = struct_num;
Id[struct_name] = cnt;
int x1 = 0, x2 = 0;
for (int i = 1; i <= struct_num; i ++ ) {
string lx, name;
cin >> lx >> name;
str[cnt].push_back(make_pair(lx, name));
if (ok(lx)) dq[cnt] = max(dq[cnt], I[lx]);
else dq[cnt] = max(dq[cnt], dq[Id[lx]]);
if (ok(lx)) {
sz[cnt] += I[lx];
if (lx == "short") x2++;
else x1++;
} else {
sz[cnt] += sz[Id[lx]];
}
if (ok(lx)) {
if (sz[cnt] % I[lx]) sz[cnt] = (sz[cnt] / I[lx] + 1) * I[lx];
} else {
if (sz[cnt] % dq[Id[lx]]) sz[cnt] = (sz[cnt] / dq[Id[lx]] + 1) * dq[Id[lx]];
}
}
cout << sz[cnt] << " " << dq[cnt] << endl;
} else if (op == 2) {
string lx, name;
cin >> lx >> name;
if (!ok(lx)) {
cout << now << endl;
dfs(lx, name, "", name);
} else {
ccnt++;
int DQ = I[lx];
string nname = name;
s[ccnt] = I[lx];
a[ccnt] = DQ;
if (ccnt == 1) b[ccnt] = 0;
else
for (int i = 1; i; i ++ )
if (i % a[ccnt] == 0 && b[ccnt - 1] + s[ccnt - 1] <= i) {
b[ccnt] = i;
break;
}
qs[nname] = b[ccnt];
for (int i = b[ccnt]; i <= b[ccnt] + I[lx] - 1; i ++ ) dz[i] = 1, zy[i] = nname;
now = b[ccnt] + I[lx];
cout << b[ccnt] << endl;
}
} else if (op == 3) {
string s;
cin >> s;
cout << qs[s] << endl;
} else {
int addr;
cin >> addr;
if (dz[addr]) cout << zy[addr] << endl;
else cout << "ERR\n";
}
}
}
signed main() {
int T;
#ifdef IOS
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
#ifdef fre
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
#ifdef multitest
cin >> T;
#else
T = 1;
#endif
while (T--) {
Init();
Solve();
}
return 0;
}