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

public class wiki_ab implements Runnable {

	private void solve() throws IOException {
		int n = in.nextInt();
		myAssert(n <= 50);
		Article[] a = new Article[n];
		for (int i = 0; i < n; ++i) {
			a[i] = new Article(in);
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (i == j) continue;
				a[i].makeLinks(a[j]);
			}
		}
		for (int i = 0; i < n; ++i) {
			a[i].print(out);
		}
	}

	HashSet<String> hm = new HashSet<String>();
	
	Comparator<String> wcmp = new Comparator<String>() {
		@Override
		public int compare(String a, String b) {
			if (a.length() != b.length()) return a.length() - b.length();
			if (a.length() == 0) return 0;
			String ta = a.substring(0, 1).toLowerCase() + a.substring(1);
			String tb = b.substring(0, 1).toLowerCase() + b.substring(1);
			return ta.compareTo(tb);
		}
	};
	
	class Article {
		ArrayList<String> text;
		String name;
		ArrayList<String> syn;
		
		public Article(SimpleScanner in) throws IOException {
			name = checkWord(in.readLine());
			int k = in.nextInt();
			myAssert(0 <= k && k <= 10);
			syn = new ArrayList<String>();
			for (int i = 0; i < k; ++i) {
				syn.add(checkWord(in.readLine()));
			}
			int l = in.nextInt();
			myAssert(1 <= l && l <= 10);
			text = new ArrayList<String>();
			for (int i = 0; i < l; ++i) {
				text.add(checkText(in.readLine()));
			}
		}
		
		public void print(PrintWriter out) {
			out.println(name);
			for (String s : text) {
				out.println(s);
			}
		}

		private String[] split(String s) {
			ArrayList<String> ans = new ArrayList<String>();
			String ts = "";
			for (char c : s.toCharArray()) {
				if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
					ts += c;
				} else {
					if (ts.length() > 0) {
						ans.add(ts);
					}
					ts = "";
					ans.add(c + "");
				}
			}
			if (ts.length() > 0) {
				ans.add(ts);
			}
			return ans.toArray(new String[ans.size()]);
		}
		
		public void makeLinks(Article o) {
			for (int i = 0; i < text.size(); ++i) {
				String[] sa = split(text.get(i));
				String ans = "";
				loop:
				for (String s : sa) {
					myAssert(s.length() <= 20);
					if (wcmp.compare(s, o.name) == 0) {
						ans += "[[" + s + "]]";
						continue;
					}
					for (String syn : o.syn) {
						if (wcmp.compare(s, syn) == 0) {
							ans += "[[" + o.name + "|" + s + "]]";
							continue loop;
						}
					}
					ans += s;
				}
				text.set(i, ans);
			}
		}
		
	}
	
	String checkText(String s) {
		myAssert(s.length() <= 80);
		for (char c : s.toCharArray()) {
			myAssert(32 <= c && c <= 126);
		}
		return s;
	}
	
	String invert(String s) {
		if (s.length() == 0) return s;
		if (Character.isLowerCase(s.charAt(0))) {
			return Character.toUpperCase(s.charAt(0)) + s.substring(1);
		} else {
			return Character.toLowerCase(s.charAt(0)) + s.substring(1);
		}
	}
	
	String checkWord(String s) {
		myAssert(!hm.contains(s) && !hm.contains(invert(s)));
		myAssert(s.length() <= 20);
		hm.add(s);
		for (char c : s.toCharArray()) {
			myAssert('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z');
		}
		return s;
	}
	
	void myAssert(boolean e) {
		if (!e) {
			throw new Error("Assertion failed");
		}
	}
	
	final String FILE_NAME = "wiki";

	SimpleScanner in;
	PrintWriter out;

	@Override
	public void run() {
		try {
			in = new SimpleScanner(new FileReader(FILE_NAME + ".in"));
			out = new PrintWriter(FILE_NAME + ".out");
			solve();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

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

	class SimpleScanner extends BufferedReader {

		private StringTokenizer st;
		private boolean eof;

		public SimpleScanner(Reader a) {
			super(a);
		}

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

		boolean seekEof() {
			String s = next();
			if ("".equals(s) && eof)
				return true;
			st = new StringTokenizer(s + " " + st.toString());
			return false;
		}

		private String cnv(String s) {
			if (s.length() == 0)
				return "0";
			return s;
		}

		int nextInt() {
			return Integer.parseInt(cnv(next()));
		}

		double nextDouble() {
			return Double.parseDouble(cnv(next()));
		}

		long nextLong() {
			return Long.parseLong(cnv(next()));
		}
	}
}