#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
map<string,set<int>> tt;
vector<string> str;
int main()
{
int n,m;
cin >> n >> m;
for(int i = 0;i<m;i++)
{
int index,num;
cin >> index >> num;
for(int j = 0;j< num;j++)
{
string temp;
cin >> temp;
tt[temp].insert(index);
}
}
for(int v = 0;v<n;v++)
{
string t;
cin >> t;
str.push_back(t);
}
for(auto it = str.begin();it != str.end();it ++)
{
cout << *it <<" "<< tt[*it].size();
if(tt[*it].size() == 0)
{
cout << endl;
}
else
{
for(auto j = tt[*it].begin();j!= tt[*it].end();j++)
{
cout<<" "<< *j;
}
cout << endl;
}
}
return 0;
}