#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int range = 26*26*26*10 + 5;
vector<int> Stu[range];int getId(char s[]){int res = 0;for(int i = 0; i < 3; i++){res = res * 26 + (s[i] -'A');}res = res * 10 + s[3] - '0';return res;
}int main() {char name[5];int N,M;scanf("%d%d", &N, &M);for(int i = 0; i < M; i++){int classId, chooseNum;scanf("%d%d", &classId, &chooseNum);for(int j = 0; j < chooseNum; j++){scanf("%s", name);Stu[getId(name)].push_back(classId);}}for(int i = 0; i < N; i++){scanf("%s", name);int id = getId(name);sort(Stu[id].begin(), Stu[id].end());printf("%s %d", name, Stu[id].size());for(int j = 0; j < Stu[id].size(); j++){printf(" %d", Stu[id][j]);}printf("\n");}return 0;
}