#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define int long long
#define oset tree < pair<int, int> , null_type , less<pair<int, int>> , rb_tree_tag , tree_order_statistics_node_update >
using namespace std;
const int N = 1e5;
signed main(){
//We have NAMES not IDs
//We need a data structures that can use strings as a key and int as the value
//Any ideas?
map<string, int> chefvotes;
map<string, int> countryvotes;
map<string, string> country;
int n, m;
cin>>n>>m;
for(int i =0 ;i<n;i++){
string chef, c;
cin>>chef>>c;
country[chef] = c;
}
//We need to store the max for both
string maxcountry;
string maxchef;
int maxcountryvotes = 0;
int maxchefvotes = 0;
for(int i = 0;i<m;i++){
string chef;
cin>>chef;
chefvotes[chef]++;
countryvotes[country[chef]]++;
if(chefvotes[chef]>maxchefvotes){
maxchef = chef;
maxchefvotes = chefvotes[chef];
}
else if(chefvotes[chef]==maxchefvotes){
maxchef = min(chef, maxchef);
}
if(countryvotes[country[chef]]>maxcountryvotes){
maxcountry = country[chef];
maxcountryvotes = countryvotes[country[chef]];
}
else if(countryvotes[country[chef]]==maxcountryvotes){
maxcountry = min(country[chef], maxcountry);
}
}
cout<<maxcountry<<endl;
cout<<maxchef<<endl;
}
//acdabcd
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwojZGVmaW5lIGludCBsb25nIGxvbmcKI2RlZmluZSBvc2V0IHRyZWUgPCBwYWlyPGludCwgaW50PiAsICBudWxsX3R5cGUgLCAgbGVzczxwYWlyPGludCwgaW50Pj4gLCAgcmJfdHJlZV90YWcgLCAgdHJlZV9vcmRlcl9zdGF0aXN0aWNzX25vZGVfdXBkYXRlID4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3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ZXMpewogICAgICAgICAgICBtYXhjaGVmID0gbWluKGNoZWYsIG1heGNoZWYpOwogICAgICAgIH0KICAgICAgICBpZihjb3VudHJ5dm90ZXNbY291bnRyeVtjaGVmXV0+bWF4Y291bnRyeXZvdGVzKXsKICAgICAgICAgICAgbWF4Y291bnRyeSA9IGNvdW50cnlbY2hlZl07CiAgICAgICAgICAgIG1heGNvdW50cnl2b3RlcyA9IGNvdW50cnl2b3Rlc1tjb3VudHJ5W2NoZWZdXTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihjb3VudHJ5dm90ZXNbY291bnRyeVtjaGVmXV09PW1heGNvdW50cnl2b3Rlcyl7CiAgICAgICAgICAgIG1heGNvdW50cnkgPSBtaW4oY291bnRyeVtjaGVmXSwgbWF4Y291bnRyeSk7CiAgICAgICAgfQogICAgfQogICAgY291dDw8bWF4Y291bnRyeTw8ZW5kbDsKICAgIGNvdXQ8PG1heGNoZWY8PGVuZGw7Cn0KLy9hY2RhYmNk