#include <bits/stdc++.h>
using namespace std;
vector < pair<bool,long long int> > que;
vector <long long int> sz;
int main()
{
// freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0);
int m;
cin>>m;
que.push_back(make_pair(false,1ll));
sz.push_back(1ll);
while(m--)
{
char ch;
cin>>ch;
if(ch == '+')
{
long long int k;
cin>>k;
if(k == 1)
{
if(que.back().first)
{
que.back().second++;
sz.back()++;
}
else
{
que.push_back(make_pair(true,1ll));
sz.push_back(1+sz.back());
}
}
else
{
que.push_back(make_pair(false,k));
sz.push_back(k*sz.back() + 1);
}
}
else
{
long long int org_x,org_y,x,y;
cin>>org_x>>org_y;
x = org_x;
y = org_y;
long long int sep_depth = 0;
bool found = false;
for (int i = que.size()-1; i >= 0 && !found; --i)
{
if(que[i].first)
{
if(x > que[i].second && y > que[i].second)
{
x-=que[i].second;
y-=que[i].second;
sep_depth+=que[i].second;
}
else
{
sep_depth+=min(x,y);
found = true;
}
}
else
{
if(x > 1ll && y > 1ll)
{
x--;
y--;
sep_depth++;
long long int v1 = (x-1ll)/sz[i-1];
long long int v2 = (y-1ll)/sz[i-1];
if(v1 == v2)
{
x = (x-1ll)%sz[i-1];
y = (y-1ll)%sz[i-1];
x++;
y++;
}
else
{
found = true;
}
}
else
{
sep_depth++;
found = true;
}
}
}
// cout<<sep_depth<<"\n";
x = org_x;
long long int x_depth = 0;
found = false;
for (int i = que.size()-1; i >= 0 && !found; --i)
{
if(que[i].first)
{
if(x > que[i].second)
{
x-=que[i].second;
x_depth+=que[i].second;
}
else
{
x_depth+=x;
found = true;
}
}
else
{
if(x > 1ll)
{
x--;
x_depth++;
x = (x-1ll)%sz[i-1];
x++;
}
else
{
x_depth++;
found = true;
}
}
}
// cout<<x_depth<<"\n";
y = org_y;
long long int y_depth = 0;
found = false;
for (int i = que.size()-1; i >= 0 && !found; --i)
{
// cout<<que[i].first<<" "<<que[i].second<<" "<<y<<"\n";
if(que[i].first)
{
if(y > que[i].second)
{
y-=que[i].second;
y_depth+=que[i].second;
}
else
{
y_depth+=y;
found = true;
}
}
else
{
if(y > 1ll)
{
y--;
y_depth++;
y = (y-1ll)%sz[i-1];
y++;
}
else
{
y_depth++;
found = true;
}
}
}
// cout<<y_depth<<"\n";
assert(sep_depth <= x_depth && sep_depth <= y_depth);
cout<<(x_depth-sep_depth)+(y_depth-sep_depth)<<"\n";
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3RvciA8IHBhaXI8Ym9vbCxsb25nIGxvbmcgaW50PiA+IHF1ZTsKdmVjdG9yIDxsb25nIGxvbmcgaW50PiBzejsKaW50IG1haW4oKQp7CgkvLyBmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CglpbnQgbTsKCWNpbj4+bTsKCXF1ZS5wdXNoX2JhY2sobWFrZV9wYWlyKGZhbHNlLDFsbCkpOwoJc3oucHVzaF9iYWNrKDFsbCk7Cgl3aGlsZShtLS0pCgl7CgkJY2hhciBjaDsKCQljaW4+PmNoOwoJCWlmKGNoID09ICcrJykKCQl7CgkJCWxvbmcgbG9uZyBpbnQgazsKCQkJY2luPj5rOwoJCQlpZihrID09IDEpCgkJCXsKCQkJCWlmKHF1ZS5iYWNrKCkuZmlyc3QpCgkJCQl7CgkJCQkJcXVlLmJhY2soKS5zZWNvbmQrKzsKCQkJCQlzei5iYWNrKCkrKzsKCQkJCX0KCQkJCWVsc2UKCQkJCXsKCQkJCQlxdWUucHVzaF9iYWNrKG1ha2VfcGFpcih0cnVlLDFsbCkpOwoJCQkJCXN6LnB1c2hfYmFjaygxK3N6LmJhY2soKSk7CgkJCQl9CgkJCX0KCQkJZWxzZQoJCQl7CgkJCQlxdWUucHVzaF9iYWNrKG1ha2VfcGFpcihmYWxzZSxrKSk7CgkJCQlzei5wdXNoX2JhY2soaypzei5iYWNrKCkgKyAxKTsK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