#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
map<string, vector<int>> mmap;
int humannum, courseNum;
scanf("%d%d", &humannum, &courseNum);
for (int i = 0,course,h; i < courseNum; i++)
{
scanf("%d%d", &course,&h);
for (int j = 0; j < h; j++)
{
string name;
cin >> name;
mmap[name].push_back(course);
}
}
vector<int>* v = NULL;
for (int i = 0; i < humannum; i++)
{
string name;
cin >> name;
v = &mmap[name];
//排序
sort(v->begin(), v->end());
printf("%s %d", name.c_str(), v->size());
//指针遍历
for (auto lt = v->cbegin(); lt != v->cend(); lt++) {
printf(" %d", *lt);
}
cout << endl;
}
}