#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
int shu;
char zhen;
string ming;
};
bool cmp(node x,node y)
{
if(x.zhen != y.zhen)
return x.zhen < y.zhen;
else if(x.shu != y.shu)
return x.shu > y.shu;
}
int n,m;
node h[100001];
int main()
{
scanf("%d%d",&n,&m);
int t=m + n;
for(int i=1;i<=t;i++)
{
cin >> h[i].ming >> h[i].zhen >> h[i].shu;
}
sort(h+1,h+t+1,cmp);
cout << h[3].ming << "\n";
int u = n;
n += 5;
for(int i = u;i <= n ;i ++)
{
cout << h[i].ming << endl ;
}
return 0;
}