#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (auto i=(a); i<(b); i++)
#define iter(it,c) for (auto it = (c).begin(); it != (c).end(); ++it)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long long ll;

int main() {
    int n;
    int k;
    cin >> n;
    cin >> k;
    unordered_map<string, string > category_dictionary;
    std::vector<string> items_bought(n);

    for(int x = 0; x < n; x++){
        string item;
        cin >> item;
        items_bought[x] = item;
    }
    std::vector<string> distinct_categories(k);
    for(int x = 0; x < k; x++){
        string category;
        cin >> category;
        distinct_categories[x] = category;
    }


    for (int i=0; i < k; i++) {
        string category;
        cin >> category;
        //cout << category << "lmao" << endl;
        int count;
        cin >> count;

        for (int j=0; j < count; j++) {
            // get category element, print it to user and save it in dictionary
            string category_element;
            cin >> category_element;
            //cout << category_element << endl;
            category_dictionary[category_element] = category;
        } 
    }

    
    for(int x = 0; x < k; x++){
        for(int y = 0; y < n; y++){
            if(category_dictionary[items_bought[y]] == distinct_categories[x]){
                cout << items_bought[y] + " ";
            }
        }
    }
    //Solution: Go through each category by the preferred order of sorting. 
    //For each category go through every item in the random line of items and print it if it is contained in the category (use a hashmmap for O(1)).

    return 0;
}


