import java.io.*;
import java.util.*;
import java.math.*;
import static java.
lang.
Math.
*; import static java.
util.
Arrays.
*;
class B2 {
static final char PROB = B2.class.getSimpleName().charAt(0);
static final boolean PRAC = true;
static final int INF = 1 << 20;
static final int[] di = { -1, 0, 0, 1 };
static final int[] dj = { 0, -1, 1, 0 };
static Scanner sc
= new Scanner
(System.
in);
final int N, M;
final char[][] D;
final char[][] L;
int maxID;
public B2() {
N = sc.nextInt();
M = sc.nextInt();
D = new char[N][];
for (int i = 0; i < N; i++)
D[i] = sc.next().toCharArray();
L = new char[M][];
for (int i = 0; i < M; i++)
L[i] = sc.next().toCharArray();
}
int[][] mask = new int[N][26];
for (int i = 0; i < N; i++)
for (int j = 0; j < 26; j++) {
char c = (char) ('a' + j);
for (int k = 0; k < D[i].length; k++)
if (D[i][k] == c)
mask[i][j] |= 1 << k;
}
StringBuilder sb = new StringBuilder();
for (int k = 0; k < M; k++) {
if (k > 0)
sb.append(' ');
int[][] is = new int[N][27];
for (int i = 0; i < N; i++) {
is[i][0] = D[i].length;
for (int j = 0; j < 26; j++)
is[i][j + 1] = mask[i][L[k][j] - 'a'];
}
Dic[] dics = new Dic[N];
for (int i = 0; i < N; i++)
dics[i] = new Dic(is[i], i);
sort(dics);
maxID = 0;
dfs(dics, 0, 0, N);
// debug(dfs(dics, 0, 0, N), maxID);
sb.append(D[maxID]);
}
return sb.toString();
}
int dfs(Dic[] dics, int idx, int from, int to) {
if (to - from == 1) {
maxID = dics[from].idx;
return 0;
}
boolean check = false;
for (int i = from; i < to; i++)
check |= dics[i].pat[idx] > 0;
int max = -1, id = from;
for (int i = from, j = i + 1; i < to; i = j, j++) {
while (j < to && dics[i].pat[idx] == dics[j].pat[idx])
j++;
int h = dfs(dics, idx + 1, i, j) + (check && dics[i].pat[idx] == 0 ? 1 : 0);
if (h > max || (h == max && id > maxID)) {
max = h;
id = maxID;
}
}
maxID = id;
// debug(idx, from, to, max, maxID);
return max;
}
private class Dic implements Comparable<Dic> {
final int[] pat;
final int idx;
public Dic(int[] pat, int idx) {
this.pat = pat;
this.idx = idx;
}
@Override
public int compareTo(Dic o) {
for (int i = 0; i < 27; i++)
if (pat[i] != o.pat[i])
return pat[i] - o.pat[i];
return idx - o.idx;
}
}
public static void main
(String...
args) { // large();
int T
= Integer.
parseInt(sc.
nextLine()); for (int t = 1; t <= T; t++) {
System.
err.
printf("Case #%s%n", t
); System.
out.
printf("Case #%s: %s%n", t,
new B2
().
solve()); }
}
public static void small() {
String in
= PROB
+ "-small" + (PRAC
? "-practice" : "-attempt" + 0) + ".in"; String out
= PROB
+ "-small.out"; try {
if (!PRAC)
for (int i
= 1; new File(PROB
+ "-small" + "-attempt" + i
+ ".in").
exists(); i
++) in = PROB + "-small" + "-attempt" + i + ".in";
e.printStackTrace();
}
}
public static void large() {
String in
= PROB
+ "-large" + (PRAC
? "-practice" : "") + ".in"; String out
= PROB
+ "-large.out"; try {
e.printStackTrace();
}
}
private static void debug
(Object...
os) { System.
err.
println(deepToString
(os
)); }
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5tYXRoLio7CgppbXBvcnQgc3RhdGljIGphdmEubGFuZy5NYXRoLio7CmltcG9ydCBzdGF0aWMgamF2YS5sYW5nLkNoYXJhY3Rlci4qOwppbXBvcnQgc3RhdGljIGphdmEudXRpbC5BcnJheXMuKjsKaW1wb3J0IHN0YXRpYyBqYXZhLnV0aWwuQ29sbGVjdGlvbnMuKjsKaW1wb3J0IHN0YXRpYyBqYXZhLm1hdGguQmlnSW50ZWdlci4qOwoKY2xhc3MgQjIgewoJc3RhdGljIGZpbmFsIGNoYXIgUFJPQiA9IEIyLmNsYXNzLmdldFNpbXBsZU5hbWUoKS5jaGFyQXQoMCk7CglzdGF0aWMgZmluYWwgYm9vbGVhbiBQUkFDID0gdHJ1ZTsKCglzdGF0aWMgZmluYWwgaW50IElORiA9IDEgPDwgMjA7CglzdGF0aWMgZmluYWwgaW50W10gZGkg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