import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class wiki_sm_correct1 {
	String DELIM = " \n";
	{
		for (int i = 33; i <= 126; i++) {
			if (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')) {
				continue;
			}
			DELIM += (char) i;
		}
	}

	class Article {
		String caption;
		String[] syn;

		String[] text;
		StringBuilder result;

		public Article() throws IOException {
			caption = br.readLine();
			checkWord(caption);
			add(caption, this);
			int k = nextInt(0, 10);
			syn = new String[k];
			for (int i = 0; i < k; i++) {
				syn[i] = br.readLine();
				checkWord(syn[i]);
				add(syn[i], this);
			}
			int l = nextInt(1, 10);
			text = new String[l];
			for (int i = 0; i < l; i++) {
				text[i] = br.readLine();
				myAssert(text[i].length() <= 80);
			}

		}

		public void transform() {
			result = new StringBuilder();
			for (int i = 0; i < text.length; i++) {
				StringTokenizer st = new StringTokenizer(text[i], DELIM, true);
				while (st.hasMoreTokens()) {
					String s = st.nextToken();
					if (isWord(s) && tm.containsKey(s) && tm.get(s) != this) {
						Article to = tm.get(s);
						if (isWikiEquals(to.caption, s)) {
							result.append("[[" + s + "]]");
						} else {
							result.append("[[" + ch(to.caption) + "|" + s
									+ "]]");
						}
					} else {
						result.append(s);
					}
				}
				result.append("\n");
			}
		}

		public void out() {
			out.println(caption);
			out.print(result.toString());
		}

	}

	Map<String, Article> tm;

	String ch(String s) {
		char f = s.charAt(0);
		s = s.substring(1);
		if (Character.isLowerCase(f)) {
			f = Character.toUpperCase(f);
		} else {
			f = Character.toLowerCase(f);
		}
		return f + s;
	}

	boolean isWikiEquals(String s1, String s2) {
		s1 = Character.toLowerCase(s1.charAt(0)) + s1.substring(1);
		s2 = Character.toLowerCase(s2.charAt(0)) + s2.substring(1);
		return s1.equals(s2);
	}

	void add(String s, Article a) {
		String s1 = Character.toLowerCase(s.charAt(0)) + s.substring(1);
		myAssert(!tm.containsKey(s1));
		tm.put(s1, a);
		String s2 = Character.toUpperCase(s.charAt(0)) + s.substring(1);
		myAssert(!tm.containsKey(s2));
		tm.put(s2, a);
	}

	private void solve() throws IOException {
		int n = nextInt(2, 50);
		Article[] wiki = new Article[n];
		tm = new HashMap<String, Article>();
		for (int i = 0; i < wiki.length; i++) {
			wiki[i] = new Article();
		}
		for (int i = 0; i < wiki.length; i++) {
			wiki[i].transform();
		}
		for (int i = 0; i < wiki.length; i++) {
			wiki[i].out();
		}
	}

	void checkWord(String s) {
		myAssert(isWord(s));
	}

	boolean isWord(String s) {
		if (!(s.length() <= 20)) {
			return false;
		}
		for (int i = 0; i < s.length(); i++) {
			if (!(('a' <= s.charAt(i) && s.charAt(i) <= 'z') || ('A' <= s
					.charAt(i) && s.charAt(i) <= 'Z'))) {
				return false;
			}
		}
		return true;
	}

	void myAssert(boolean b) {
		if (!b)
			throw new AssertionError();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;

	public void run() {
		try {
			br = new BufferedReader(new FileReader("wiki.in"));
			out = new PrintWriter("wiki.out");
			solve();
			out.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

	}

	int nextInt(int a, int b) {
		int x = Integer.parseInt(nextToken());
		if (x < a || x > b)
			throw new AssertionError();
		return x;
	}

	String nextToken() {
		try {
			while (st == null || !st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine());
			}
			return st.nextToken();
		} catch (IOException e) {
			return "0";
		}
	}

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