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

public class supersub_nn {

	static final int ALPHABET = 10;
	static final int MAXN = 1000000;
	static final int MAXLEN = 1000000;

	void solve() {
		String t = nextToken();
		doAssert(1 <= t.length() && t.length() <= MAXLEN,
				"length of t is out of range");
		SuffixAutomaton sa = new SuffixAutomaton(t.length(), ALPHABET);
		for (int i = 0; i < t.length(); i++) {
			doAssert(Character.isLowerCase(t.charAt(i)), "t[" + (i + 1)
					+ "] is not in [a..z]");
			sa.append(t.charAt(i) - 'a');
		}
		int n = nextInt();
		doAssert(1 <= n && n <= MAXN, "n is out of range");
		for (int i = 0; i < n; i++) {
			String s = nextToken();
			doAssert(1 <= s.length() && s.length() <= MAXLEN,
					"length of s in query #" + (i + 1) + " is out of range");
			for (int j = 0; j < s.length(); j++) {
				doAssert(Character.isLowerCase(s.charAt(j)), "In query #"
						+ (i + 1) + ", s[" + (j + 1) + "] is not in [a..z]");
			}
			out.println(getDecomposition(sa, s));
		}
	}

	static String getDecomposition(SuffixAutomaton sa, String s) {
		int p = sa.root;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			int c = s.charAt(i) - 'a';
			if (sa.link[c][p] == -1) {
				p = sa.root;
				sb.appendCodePoint('|');
			}
			if (sa.link[c][p] == -1) {
				return "NO";
			}
			sb.appendCodePoint(s.charAt(i));
			p = sa.link[c][p];
		}
		return sb.toString();
	}

	static void doAssert(boolean ask, String msg) {
		if (!ask) {
			throw new AssertionError(msg);
		}
	}

	static class SuffixAutomaton {
		int[][] link;
		int[] sufLink;
		int[] length;
		int free;
		int root;
		int last;

		public SuffixAutomaton(int maxLength, int alphabet) {
			maxLength = maxLength * 2 + 1;
			link = new int[alphabet][maxLength];
			for (int[] d : link) {
				Arrays.fill(d, -1);
			}
			sufLink = new int[maxLength];
			length = new int[maxLength];
			free = 1;
			root = last = 0;
			sufLink[0] = -1;
		}

		void append(int c) {
			int newNode = free++;
			length[newNode] = length[last] + 1;
			while (last != -1 && link[c][last] == -1) {
				link[c][last] = newNode;
				last = sufLink[last];
			}
			if (last == -1) {
				sufLink[newNode] = root;
				last = newNode;
				return;
			}
			int q = link[c][last];
			if (length[q] == length[last] + 1) {
				sufLink[newNode] = q;
				last = newNode;
				return;
			}
			int copy = free++;
			length[copy] = length[last] + 1;
			for (int i = 0; i < link.length; i++) {
				link[i][copy] = link[i][q];
			}
			while (last != -1 && link[c][last] == q) {
				link[c][last] = copy;
				last = sufLink[last];
			}
			sufLink[copy] = sufLink[q];
			sufLink[newNode] = sufLink[q] = copy;
			last = newNode;
		}
	}

	FastScanner sc;
	PrintWriter out;

	public void run() {
		Locale.setDefault(Locale.US);
		try {
			sc = new FastScanner("supersub.in");
			out = new PrintWriter("supersub.out");
			solve();
			sc.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	int nextInt() {
		return sc.nextInt();
	}

	String nextToken() {
		return sc.nextToken();
	}

	long nextLong() {
		return sc.nextLong();
	}

	double nextDouble() {
		return sc.nextDouble();
	}

	BigInteger nextBigInteger() {
		return sc.nextBigInteger();
	}

	class FastScanner extends BufferedReader {
		StringTokenizer st;
		boolean eof;
		String buf;
		String curLine;
		boolean createST;

		public FastScanner(String fileName) throws FileNotFoundException {
			this(fileName, true);
		}

		public FastScanner(String fileName, boolean createST)
				throws FileNotFoundException {
			super(new FileReader(fileName));
			this.createST = createST;
			nextToken();
		}

		public FastScanner(InputStream stream) {
			this(stream, true);
		}

		public FastScanner(InputStream stream, boolean createST) {
			super(new InputStreamReader(stream));
			this.createST = createST;
			nextToken();
		}

		String nextLine() {
			String ret = curLine;
			if (createST) {
				st = null;
			}
			nextToken();
			return ret;
		}

		String nextToken() {
			if (!createST) {
				try {
					curLine = readLine();
				} catch (Exception e) {
					eof = true;
				}
				return null;
			}
			while (st == null || !st.hasMoreTokens()) {
				try {
					curLine = readLine();
					st = new StringTokenizer(curLine);
				} catch (Exception e) {
					eof = true;
					break;
				}
			}
			String ret = buf;
			buf = eof ? "-1" : st.nextToken();
			return ret;
		}

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

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

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

		BigInteger nextBigInteger() {
			return new BigInteger(nextToken());
		}

		public void close() {
			try {
				buf = null;
				st = null;
				curLine = null;
				super.close();
			} catch (Exception e) {

			}
		}

		boolean isEOF() {
			return eof;
		}
	}

	public static void main(String[] args) {
		new supersub_nn().run();
	}
}