1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<cassert>
7 using namespace std;
8 typedef long long int64;
9 char ch; bool ok;
10 void read(int &x){
11 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
12 for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
13 if (ok) x=-x;
14 }
15 void read(int64 &x){
16 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
17 for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
18 if (ok) x=-x;
19 }
20 const int maxn=600005;
21 const int64 inf=9223372036854775807LL;
22 int q,idx,list[maxn];
23 int64 x;
24 char op,tmp[12],name[maxn][12];
25 struct Hash{
26 static const int base1=97;
27 static const int mod1=99997;
28 static const int base2=103;
29 static const int mod2=3777777;
30 int tot,now[mod1],pre[maxn],key[maxn];
31 void init(){tot=0;memset(now,0,sizeof(now));}
32 int find(){
33 int u=0,k=0;
34 for (int i=1;tmp[i];i++) u=(u*base1+tmp[i])%mod1,k=(k*base2+tmp[i])%mod2;
35 for (int p=now[u];p;p=pre[p]) if (key[p]==k) return p;
36 pre[++tot]=now[u],now[u]=tot,key[tot]=k;
37 for (int i=1;tmp[i];i++) name[tot][i]=tmp[i];
38 return tot;
39 }
40 }data;
41 struct Splay{
42 int tot,root,son[maxn][2],fa[maxn],siz[maxn],pos[maxn];
43 int64 val[maxn];
44 int which(int x){return son[fa[x]][1]==x;}
45 void updata(int x){siz[x]=siz[son[x][0]]+1+siz[son[x][1]];}
46 void rotate(int x){
47 int y=fa[x],z=fa[y],d=which(x),dd=which(y);
48 son[y][d]=son[x][d^1],fa[son[x][d^1]]=y,fa[x]=z;
49 if (z) son[z][dd]=x;
50 son[x][d^1]=y,fa[y]=x,updata(y),updata(x);
51 }
52 void splay(int x){
53 while (fa[x]){
54 if (!fa[fa[x]]) rotate(x);
55 else if (which(fa[x])==which(x)) rotate(fa[x]),rotate(x);
56 else rotate(x),rotate(x);
57 }
58 root=x;
59 }
60 void init(){
61 memset(siz,0,sizeof(siz)),memset(pos,0,sizeof(pos));
62 data.init();
63 root=q+1;
64 siz[q+1]=2,fa[q+1]=0,son[q+1][0]=0,son[q+1][1]=q+2,val[q+1]=inf;
65 siz[q+2]=1,fa[q+2]=q+1,son[q+2][0]=son[q+2][1]=0,val[q+2]=-inf;
66 }
67 int find_left(int x){
68 for (;son[x][0];x=son[x][0]);
69 return x;
70 }
71 void del(int x){
72 splay(x);
73 int y=find_left(son[x][1]);
74 fa[son[x][0]]=fa[son[x][1]]=0;
75 splay(y),son[y][0]=son[x][0],fa[son[x][0]]=y,updata(y);
76 }
77 void insert(int x){
78 int f,t;
79 for (f=t=root;t;f=t,t=son[t][val[x]<=val[t]]);
80 assert(f!=0);
81 fa[x]=f,son[f][val[x]<=val[f]]=x,splay(x);
82 }
83 void push(int64 v){
84 int id=data.find();
85 if (pos[id]==0) pos[id]=++tot;
86 else del(pos[id]);
87 val[id]=v,siz[id]=1,fa[id]=son[id][0]=son[id][1]=0,insert(pos[id]);
88 }
89 int find(int x,int rank){
90 if (siz[son[x][0]]>=rank) return find(son[x][0],rank);
91 if (siz[son[x][0]]+1==rank) return x;
92 return find(son[x][1],rank-siz[son[x][0]]-1);
93 }
94 void answer(int x,int rest){
95 if (rest==0||x==0) return;
96 answer(son[x][0],rest);
97 if (siz[son[x][0]]<rest){
98 list[++idx]=x;
99 answer(son[x][1],rest-siz[son[x][0]]-1);
100 }
101 }
102 void query_list(int rank){
103 int id=find(root,rank+1);
104 splay(id);
105 list[idx=1]=id;
106 answer(son[id][1],9);
107 if (!name[list[idx]][1]) idx--;
108 for (int i=1;i<idx;i++) printf("%s ",name[list[i]]+1);
109 printf("%s\n",name[list[idx]]+1);
110 }
111 void query_rank(){
112 int id=data.find();
113 splay(id),printf("%d\n",siz[son[id][0]]);
114 }
115 }T;
116 int main(){
117 for (read(q),T.init();q;q--){
118 for (op=getchar();op!='+'&&op!='?';op=getchar());
119 if (op=='+') scanf("%s",tmp+1),read(x),T.push(x);
120 else if (op=='?'){
121 scanf("%s",tmp+1);
122 if (isdigit(tmp[1])){
123 x=0;
124 for (int i=1;tmp[i];i++) x=x*10+tmp[i]-'0';
125 T.query_list(x);
126 }
127 else T.query_rank();
128 }
129 }
130 return 0;
131 }