双倍经验qaq:https://blog.csdn.net/icefox_zhx/article/details/79880557
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
#define uint unsigned int
#define inf 1LL<<60
#define N 250010
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline ll read(){
ll x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
char s[15],mp[N][15];
int n,m,fa[N],c[N][2],h[N],num=0,X,rt=0,sz[N];ll v[N];
struct Hash_table{
uint key;int val,next;
}data[N];
inline int hs(uint key){
int x=key%N;
for(int i=h[x];i;i=data[i].next){
if(data[i].key==key) return data[i].val;
}data[++num].next=h[x];h[x]=num;data[num].key=key;data[num].val=++n;return n;
}
inline int get_S(){
char ch=gc();int op=0,len=0;
while(ch!='+'&&ch!='?') ch=gc();
if(ch=='+'){
uint x=0;
ch=gc();while(ch>='A'&&ch<='Z') x=x*27+ch-'A'+1,s[++len]=ch,ch=gc();
X=hs(x);s[++len]=0;memcpy(mp[X],s,sizeof(s));
return 0;
}ch=gc();if(ch>='0'&&ch<='9'){
X=0;while(ch>='0'&&ch<='9') X=X*10+ch-'0',ch=gc();return 1;
}else{
uint x=0;
while(ch>='A'&&ch<='Z') x=x*27+ch-'A'+1,ch=gc();
X=hs(x);return 2;
}
}
inline void update(int x){
sz[x]=sz[c[x][0]]+sz[c[x][1]]+1;
}
inline void rotate(int x,int &k){
int y=fa[x],z=fa[y],l=x==c[y][1],r=l^1;
if(y==k) k=x;
else c[z][y==c[z][1]]=x;
fa[c[x][r]]=y;fa[y]=x;fa[x]=z;
c[y][l]=c[x][r];c[x][r]=y;update(y);update(x);
}
inline void splay(int x,int &k){
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k){
if(x==c[y][1]^y==c[z][1]) rotate(x,k);
else rotate(y,k);
}rotate(x,k);
}
}
inline void ins(int &p,int x,int Fa){
if(!p){p=x;fa[p]=Fa;sz[p]=1;splay(p,rt);return;}
if(v[x]>v[p]) ins(c[p][0],x,p);
else ins(c[p][1],x,p);
}
inline int qrk(int x){
splay(x,rt);return sz[c[x][0]]+1;
}
inline int find(int p,int k){
if(sz[c[p][0]]+1==k) return p;
if(k<=sz[c[p][0]]) return find(c[p][0],k);
return find(c[p][1],k-sz[c[p][0]]-1);
}
inline void del(int x){
splay(x,rt);int pre=c[x][0],succ=c[x][1];
while(c[pre][1]) pre=c[pre][1];
while(c[succ][0]) succ=c[succ][0];
splay(pre,rt);splay(succ,c[rt][1]);fa[x]=c[succ][0]=0;
update(succ);update(pre);
}
void dfs(int x){
if(c[x][0]) dfs(c[x][0]);
for(int i=1;mp[x][i];++i) putchar(mp[x][i]);putchar(' ');
if(c[x][1]) dfs(c[x][1]);
}
inline void solve(int l,int r){
int x=find(rt,l-1),y=find(rt,r+1);
splay(x,rt);splay(y,c[x][1]);
dfs(c[y][0]);puts("");
}
int main(){
// freopen("a.in","r",stdin);
m=read();v[++n]=-1;ins(rt,n,0);v[++n]=inf;ins(rt,n,0);
for(int i=1;i<=m;++i){
int op=get_S(),x=X;
if(!op){//+
if(v[x]) del(x);
v[x]=read();ins(rt,x,0);continue;
}if(op==1){//?111
int y=min(x+10-1,n-2);solve(x+1,y+1);continue;
}printf("%d\n",qrk(x)-1);
}return 0;
}