#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

int main() {
    std::ios::sync_with_stdio(false); // Speedup (do not mix with scanf/prinf)
    std::cin.tie(NULL); // Speedup (do not mix with scanf/prinf)

    int n, k;
    cin >> n >> k;
    
    string items[n];
    rep(i, n) cin >> items[i];

    string order[k];
    rep(i, k) cin >> order[i];
    
    unordered_map<string, string> category_of_item;

    rep(i, k) {
        string cat;
        int m;
        cin >> cat >> m;
        rep(j, m) {
            string item;
            cin >> item;
            category_of_item.emplace(item, cat);
        }
    }

    rep(i, k) {
        rep(j, n) {
            if (category_of_item[items[j]].compare(order[i]) == 0) {
                cout << items[j] << " ";
            }
        }
    }
    cout << endl;

    return 0;
}
