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

public class supersub_ats_slow implements Runnable {
	public static void main(String[] args) {
		new supersub_ats_slow().run();
	}

	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}
		return in.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	final long prime = 37;
	final int inf = (int) 1e9;

	long[] textHash;
	long[] deg;

	long[] patHash;

	public long getHash(long[] hash, int left, int right) {
		if (left == 0) {
			return hash[right];
		}
		return hash[right] - hash[left - 1] * deg[right - left + 1];
	}

	public void calc(String s, long[] hash) {
		Arrays.fill(hash, 0);
		hash[0] = s.charAt(0);
		for (int i = 1; i < s.length(); i++) {
			hash[i] = hash[i - 1] * prime + s.charAt(i);
		}
	}

	final int maxlen = (int)1e6;
	
	public void solve() throws IOException {
		String text = nextToken();
		TreeSet<Long> h = new TreeSet<Long>();
		deg = new long[maxlen];
		deg[0] = 1;
		for (int i = 1; i < deg.length; i++) {
			deg[i] = deg[i - 1] * prime;
		}
		textHash = new long[text.length()];
		calc(text, textHash);
		for (int left = 0; left < text.length(); left++) {
			for (int right = left; right < text.length(); right++) {
				h.add(getHash(textHash, left, right));
			}
		}

		int n = nextInt();
		for (int i = 0; i < n; i++) {
			String pat = nextToken();
			patHash = new long[pat.length()];
			calc(pat, patHash);

			int[] t = new int[pat.length() + 1];
			int[] prev = new int[pat.length() + 1];

			t[pat.length()] = 0;
			prev[pat.length()] = -1;

			for (int j = pat.length() - 1; j >= 0; j--) {
				t[j] = inf;
				for (int k = j; k < pat.length(); k++) {
					if (h.contains(getHash(patHash, j, k))
							&& t[j] > t[k + 1] + 1) {
						t[j] = t[k + 1] + 1;
						prev[j] = k + 1;
					}
				}
			}

			if (t[0] == inf) {
				out.println("NO");
			} else {
				int pos = 0;
				while (true) {
					out.print(pat.substring(pos, prev[pos]));
					pos = prev[pos];
					if (prev[pos] != -1) {
						out.print("|");
					} else {
						break;
					}

				}
				out.println();
			}
		}

	}

	public void run() {
		try {
			br = new BufferedReader(new FileReader("supersub.in"));
			out = new PrintWriter("supersub.out");

			solve();

			br.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}
