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

import com.sun.org.apache.bcel.internal.generic.NEW;

public class wiki_aa implements Runnable {

	public static void main(final String[] args) {
		new Thread(new wiki_aa()).start();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	boolean eof = false;
	Random rand = new Random(this.hashCode());

	public void run() {
		Locale.setDefault(Locale.US);
		try {
			br = new BufferedReader(new FileReader(FNAME + ".in"));
			out = new PrintWriter(FNAME + ".out");
			solve();
			out.close();
		} catch (final Throwable e) {
			e.printStackTrace();
			System.exit(566);
		}
	}

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

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

	void myAssert(final boolean u, final String message) {
		if (!u) {
			throw new Error("Assertion failed!!! " + message);
		}
	}

	int inBounds(final int x, final int l, final int r, final String name) {
		myAssert(l <= x && x <= r, name + " not in bounds!!! " + x
				+ " not in [" + l + ".." + r + "]");
		return x;
	}

	String FNAME = "wiki";

	private void solve() throws IOException {
		int n = nextInt();
		Article[] a = new Article[n];
		for (int i = 0; i < a.length; i++) {
			a[i] = nextArticle();
		}
		for (int i = 0; i < a.length; i++) {
			a[i].process();
		}
		for (int i = 0; i < a.length; i++) {
			out.println(a[i]);
		}
	}

	private Article nextArticle() throws IOException {
		Article a = new Article(nextToken());
		int k = nextInt();
		while (k-- > 0) {
			StringBuilder sb = new StringBuilder(nextToken());
			sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
			synonims.put(sb.toString(), a.name);
			sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
			synonims.put(sb.toString(), a.name);
		}
		StringBuilder sb = new StringBuilder(a.name);
		sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
		synonims.put(sb.toString(), a.name);
		sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		synonims.put(sb.toString(), a.name);
		a.text = new String[nextInt()];
		for (int i = 0; i < a.text.length; i++) {
			a.text[i] = br.readLine();
		}
		return a;
	}

	HashMap<String, String> synonims = new HashMap<String, String>();

	class Article {
		public Article(String s) {
			name = s;
		}

		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder();
			sb.append(name);
			for (int i = 0; i < text.length; i++) {
				sb.append("\n" + text[i]);
			}
			return sb.toString();
		}

		public void process() {
			for (int i = 0; i < text.length; i++) {
				char[] s = text[i].toCharArray();
				StringBuilder sb = new StringBuilder();
				for (int j = 0; j < s.length; j++) {
					if (('a' <= s[j] && s[j] <= 'z')
							|| ('A' <= s[j] && s[j] <= 'Z')) {
						StringBuilder word = new StringBuilder();
						while (j < s.length
								&& (('a' <= s[j] && s[j] <= 'z') || ('A' <= s[j] && s[j] <= 'Z'))) {
							word.append(s[j++]);
						}
						j--;
						if (synonims.containsKey(word.toString())) {
							String refName = synonims.get(word.toString());
							if (refName.equals(name)) {
								sb.append(word.toString());
								continue;
							}
							if (equal(refName, word.toString())) {
								sb.append("[[" + word.toString() + "]]");
							} else {
								sb.append("[[" + refName + "|"
										+ word.toString() + "]]");
							}
						} else {
							sb.append(word.toString());
						}
					} else {
						sb.append(s[j]);
					}
				}
				text[i] = sb.toString();
			}
		}

		String[] text;
		String name;
	}

	boolean equal(String s1, String s2) {
		return s1.equals(s2)
				|| (s1.length() >= 1
						&& s2.length() >= 1
						&& Character.toLowerCase(s1.charAt(0)) == Character
								.toLowerCase(s2.charAt(0)) && s1.substring(1)
						.equals(s2.substring(1)));
	}

}
