// Arup Guha
// 1/18/2014
// Solution to 2006 MCPC Problem E: Frugal Search

import java.util.*;

public class e {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		String s = stdin.next();

		// Go through each case.
		while (!s.equals("#")) {

			// Build dictionary and sort it.
			ArrayList<String> dictionary = new ArrayList<String>();
			dictionary.add(s);
			s = stdin.next();
			while (!s.equals("*")) {
				dictionary.add(s);
				s = stdin.next();
			}
			Collections.sort(dictionary);

			// Process each query.
			String q = stdin.next();
			while (!q.equals("**")) {
				System.out.println(solve(dictionary, q));
				q = stdin.next();
			}

			// Move onto the next case.
			System.out.println("$");
			s = stdin.next();
		}
	}

	public static String solve(ArrayList<String> dictionary, String q) {

		StringTokenizer tok = new StringTokenizer(q, "|");

		String ans = null;
		
		// Try each individual query.
		while (tok.hasMoreTokens()) {

			String thisq = tok.nextToken();
			query question = new query(thisq);

			// Look for a match in the dictionary to this query and update best match, if necessary.
			for (int i=0; i<dictionary.size(); i++) {
				String w = dictionary.get(i);
				if (question.isMatch(w)) {
					if (ans == null || w.compareTo(ans) < 0 )
						ans = w;
				}
			}

		}
		
		// Return what we want to print.
		if (ans != null) return ans;
		return "NONE";
	}
}

class query {

	public boolean[] unsigned;
	public boolean[] positive;
	public boolean[] negative;

	public query(String s) {

		unsigned = new boolean[26];
		positive = new boolean[26];
		negative = new boolean[26];

		// Go through each item in the query string.
		boolean curPos = false, curNeg = false;
		for (int i=0; i<s.length(); i++) {
			char c = s.charAt(i);
			if (Character.isLetter(c)) {
				
				// Update the appropriate index of letter seen based on what came before.
				if (curPos) {
					positive[c-'a'] = true;
					curPos = false;
				}
				else if (curNeg) {
					negative[c-'a'] = true;
					curNeg = false;
				}
				else
					unsigned[c-'a'] = true;
			}
			
			// Set our type marker here.
			else if (c == '+')
				curPos = true;
			else if (c == '-')
				curNeg = true;
		}
	}

	public boolean isMatch(String s) {

		boolean[] letters = new boolean[26];
		boolean hasUnsigned = false;

		// Mark incidents of positive letters and note if an unsigned or negative occurs.
		for (int i=0; i<s.length(); i++) {
			int index = s.charAt(i) - 'a';
			if (negative[index]) return false;
			if (unsigned[index]) hasUnsigned = true;
			letters[index] = true;
		}

		// Can't be a match.
		if (!hasUnsigned) return false;
		
		// Must have each positive.
		for (int i=0; i<26; i++) {
			if (positive[i] && !letters[i])
				return false;
		}
		
		// It's a match if we get here.
		return true;
	}

	// For debugging.
	public String toString() {

		String ans = "unsigned: ";
		for (int i=0; i<unsigned.length; i++)
			if (unsigned[i])
				ans = ans + (char)('a'+i);
		ans = ans +"\npositive: ";
		for (int i=0; i<positive.length; i++)
			if (positive[i])
				ans = ans + (char)('a'+i);
		ans = ans +"\nnegative: ";
		for (int i=0; i<negative.length; i++)
			if (negative[i])
				ans = ans + (char)('a'+i);
		ans = ans +"\n";
		return ans;
	}
}