#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