#include<iostream>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<string>
#include<algorithm>
#include <cstdlib>
#include<stdlib.h>
#include<sstream>
#include<stack>
#include <climits>
#include<vector>
using namespace std;
typedef struct cs
{
string s;
char who;
long long q;
};
cs a[1300000];
int main()
{
long long n, m;
long long max = -1;
cin >> n >> m;
for (int i = 0; i < n + m; i++)
{
cin >> a[i].s>>a[i].who>>a[i].q;
}
int p;
for (int j = 1; j <= 5; j++)
{
max = -1;
for (int i = 1; i <= m + n; i++)
{
if (a[i].who == 'S'&&a[i].q>=max)
{
max = a[i].q;
p = i;
}
}
a[p].q = -5;
}
for (int j = 1; j <= 2; j++)
{
max = -1;
for (int i = 1; i <= m + n; i++)
{
if (a[i].who == 'H' && a[i].q >= max)
{
max = a[i].q;
p = i;
}
}
a[p].q = -5;
}
max = -1;
for (int i = 1; i <= 1; i++)
{
for (int j = 0; j < m + n; j++)
{
if (max < a[j].q&&a[j].who=='H')
{
max = a[j].q;
p = j;
}
}
cout << a[p].s<<"\n";
}
for (int i = 1; i <= 4; i++)
{
max = -1;
for (int j = 0; j < m + n; j++)
{
if (max < a[j].q&&a[j].who=='S')
{
max = a[j].q;
p = j;
}
}
a[p].q = -5;
cout << a[p].s<<"\n";
}
return 0;
}