import java.util.*;
import java.io.*;
import java.math.*;

public class jeroen
{
	public static void main(String[] args) throws Exception
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String[] line = in.readLine().split(" ");
		int n = Integer.valueOf(line[0]);
		int m = Integer.valueOf(line[1]);
		HashMap<String, LinkedList<String>> dictionary = new HashMap<String, LinkedList<String>>();
		for(int i = 0; i < n; i++) {
			String w = in.readLine();
			String d = toDigits(w);
			if(dictionary.containsKey(d)) {
				dictionary.get(d).add(w);
			} else {
				LinkedList<String> lst = new LinkedList<String>();
				lst.add(w);
				dictionary.put(d, lst);
			}
		}
		for(int i = 0; i < m; i++) {
			String p = in.readLine();
			LinkedList<String> mbLst = dictionary.get(p);
			if(mbLst == null) {
				System.out.println("0");
			} else {
				// Slow output, but should be okay for this problem
				int len = mbLst.size();
				System.out.print(len);
				for(String w : mbLst) {
					System.out.print(" " + w);
				}
				System.out.println();
			}
		}
	}

	// string.map(toDigit), but I don't want to figure out the syntax
	static String toDigits(String s) {
		char[] x = new char[s.length()];
		for(int i = 0; i < s.length(); i++)
			x[i] = toDigit(s.charAt(i));
		return new String(x);
	}

	static char toDigit(char x) {
		if(x <= 'c') return '2';
		if(x <= 'f') return '3';
		if(x <= 'i') return '4';
		if(x <= 'l') return '5';
		if(x <= 'o') return '6';
		if(x <= 's') return '7';
		if(x <= 'v') return '8';
		return '9';
	}
}
