#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
struct N
{
int data;
char name[21];
N *next;
};
N *creat()
{
N *p = (struct N *)malloc(sizeof(struct N));
p->next = NULL;
return p;
}
void link(N *head,N *p)
{
N *q = head;
for(;q->next != NULL && q->next->data >= p->data; q = q->next);
p->next = q->next;
q->next = p;
}
int del(N *head,char *name)
{
N *p = head;
N *q = head->next;
for(;q->next != NULL && strcmp(name,q->name) != 0;p = p->next,q = q->next);
p->next = q->next;
return q->data;
}
void change(N *head,N *p)
{
p->data += del(head,p->name);
link(head,p);
}
void output_rank(N *head)
{
int sum,temp;
printf("#1 :");
for(head = head->next,temp = head->data;head != NULL;)
{
if(temp == head->data)
{
printf(" %s",head->name);
head = head->next;
}
else break;
}
if(head == NULL) return;
printf("\n#2 :");
for(temp = head->data,sum = 1;head != NULL;)
{
if(temp == head->data)
{
printf(" %s",head->name);
head = head->next;
}
else
{
sum++;
if(sum <= 2)
{
temp = head->data;
printf(" %s",head->name);
head = head->next;
}
else break;
}
}
if(head == NULL) return;
printf("\n#3 :");
for(temp = head->data,sum = 1;head != NULL;)
{
if(temp == head->data)
{
printf(" %s",head->name);
head = head->next;
}
else
{
sum++;
if(sum <= 3)
{
temp = head->data;
printf(" %s",head->name);
head = head->next;
}
else break;
}
}
printf("\n");
}
void output(N *head)
{
for(N *p = head->next; p != NULL; p = p->next)
cout<<p->name<<' '<<p->data<<endl;
}
int main()
{
int n;
N *head = creat();
cin>>n;
for(int i = 0;i < n; i++)
{
N *p = creat();
cin>>p->name>>p->data;
link(head,p);
}
char order;
while(cin>>order && order != 'O')
{
if(order == 'A')
{
N *p = creat();
cin>>p->name>>p->data;
link(head,p);
}
else if(order == 'Q')
{
char name[21];
cin>>name;
del(head,name);
}
else if(order == 'C')
{
N *p = creat();
cin>>p->name>>p->data;
change(head,p);
}
else if(order == 'S')
{
output(head);
}
}
output_rank(head);
return 0;
}