#include <iostream>
#include <unordered_map>
#include <unordered_set>
#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, unordered_set<string>> items_in_category;

    rep(i, k) {
        string cat;
        int m;
        cin >> cat >> m;
        items_in_category[cat] = {};
        rep(j, m) {
            string item;
            cin >> item;
            items_in_category[cat].insert(item);
        }
    }

    rep(i, k) {
        unordered_set<string> this_category = items_in_category[order[i]];
        rep(j, n) {
            if (this_category.find(items[j]) != this_category.end()) {
                cout << items[j] << " ";
            }
        }
    }
    cout << endl;

    return 0;
}
