#!/usr/bin/env python3

(n, m), W = map(int, input().split()), __import__('collections').defaultdict(list)
for s in map(input, ' ' * n): W[''.join(str((ord(c) - 91 - (c > 'r') - (c > 'y')) // 3) for c in s)] += [s]
for p in map(input, ' ' * m): print(len(W[p]), *W[p])
