#include<stdio.h>
typedef struct
{
char name[11],d;
int c;
}exam;
int main()
{
int x,y=0,z,i,j,sec=0;
exam arr[101]={0},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)
{
for(j=1; j<x; j++)
{
arr[j]=arr[j+1];
}
}
}
for(i=1; i<=5; i++)
{
scanf("%d",&y);
arr[i]=arr[y];
}
for(i=1; i<=5; i++)
{
for(j=1; j<=5-i; j++)
{
if (arr[j].c < arr[j+1].c)
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
printf("%d %s\n",arr[6-i].c,arr[6-i].name);
}
}