#include<stdio.h>
typedef struct
{
char name[11],d;
int c;
}exam;
int main()
{
int x,y=0,z=0,i,j,sec=0;
exam arr[101]={0},arf[106],temp;
scanf("%d",&x);
for(i=1; i<=x; i++)
{
scanf("\n%c %d %s",&arr[i].d,&arr[i].c,arr[i].name);
sec=arr[i].c;
if(arr[i].d=='D')
{
arr[i].c=-1;
}
for(j=1; j<i; j++)
{
if(arr[j].c==sec)
{
arr[i].c=-1;
if(arr[i].d=='D')
{
arr[j].c=-1;
}
else
{
break;
}
}
}
}
for(i=1; i<=x; i++)
{
if(arr[i].c!=-1)
{
arf[++z]=arr[i];
}
}
for(i=1; i<=5; i++)
{
scanf("%d",&y);
arf[100+i]=arf[y];
}
for(i=1; i<=5; i++)
{
for(j=101; j<=106-i; j++)
{
if (arf[j].c < arf[j+1].c)
{
temp = arf[j];
arf[j] = arf[j+1];
arf[j+1] = temp;
}
}
printf("%d %s\n",arf[106-i].c,arf[106-i].name);
}
}