#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
const int N = 40010;
const int M=26*26*26*10+1;
vector<int> selectCourse[M];
typedef long long ll;
int getId(char name[]){
int id=0;
for(int i=0;i<3;i++){
id=id*26+(name[i]-'A');
}
id=id*10+(name[3]-'0');
return id;
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
char name[5];
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<k;i++){
int course,x;
scanf("%d%d",&course,&x);
for(int i=0;i<x;i++){
scanf("%s",name);
int id=getId(name);
selectCourse[id].push_back(course);
}
}
for(int i=0;i<n;i++){
scanf("%s",name);
int id=getId(name);
sort(selectCourse[id].begin(),selectCourse[id].end());
printf("%s %d",name,selectCourse[id].size());
for(int j=0;j<selectCourse[id].size();j++){
printf(" %d",selectCourse[id][j]);
}
printf("\n");
}
return 0;
}