// Arup Guha
// 6/2/2018
// Solution to 2013 KTH Problem A: Car Game

import java.util.*;
import java.io.*;

public class cargame {

	public static int nWords;
	public static String[] words;

	// These store the first and last index of each of the 26 letters(2nd idx) in each string (1st idx).
	public static int[][] first;	// sID, let
	public static int[][] last;		// sID, let

	// next[i][j][k] stores the first occurrence of letter k in string i after index j.
	public static int[][][] next; 	// sID, pos, let

	public static void main(String[] args) throws Exception {

		// Read input.
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		nWords = Integer.parseInt(tok.nextToken());
		int nPatterns = Integer.parseInt(tok.nextToken());
		words = new String[nWords];
		for (int i=0; i<nWords; i++) words[i] = stdin.readLine().trim();

		// Set up these arrays.
		first = new int[nWords][26];
		last = new int[nWords][26];
		next = new int[nWords][][];

		// Build first and last.
		for (int i=0; i<nWords; i++) {

			// Initialize...
			int len = words[i].length();
			Arrays.fill(first[i], -1);
			Arrays.fill(last[i], -1);
			next[i] = new int[len][26];

			// Now go through letter by letter.
			for (int j=0; j<len; j++) {
				int idx = words[i].charAt(j)-'a';
				if (first[i][idx] == -1) first[i][idx] = j;
				last[i][idx] = j;
			}
		}

		// Now build next.
		for (int i=0; i<nWords; i++) {

			int len = words[i].length();

			// Now start at each letter.
			for (int j=0; j<len; j++) {
				Arrays.fill(next[i][j], -1);

				// Build info looking at all future letters.
				for (int k=j+1; k<len; k++)
					if (next[i][j][words[i].charAt(k)-'a'] == -1)
						next[i][j][words[i].charAt(k)-'a'] = k;
			}
		}

		// Store answer here.
		StringBuilder sb = new StringBuilder();

		// Could do this later.
		for (int i=0; i<nPatterns; i++) {

			// Extract out three letter values.
			String s = stdin.readLine().trim();
			int c1 = s.charAt(0)-'A';
			int c2 = s.charAt(1)-'A';
			int c3 = s.charAt(2)-'A';

			boolean found = false;

			// Try each word.
			for (int j=0; j<nWords; j++) {

				// All letters have to be present.
				if (first[j][c1] == -1 || first[j][c2] == -1 || first[j][c3] == -1) continue;

				// Another simple check.
				if (first[j][c1] > last[j][c3]) continue;

				// Only way this works is if the next appearance of c2 after c1 occurs before the last appearance of c3.
				if (next[j][first[j][c1]][c2] != -1 && next[j][first[j][c1]][c2] < last[j][c3]) {
					found = true;
					sb.append(words[j]+"\n");
					break;
				}
			}

			// Failure case.
			if (!found) sb.append("No valid word\n");
		}

		// Ta da!
		System.out.print(sb);
	}
}