// Arup Guha
// 7/29/2026
// Solution to 1992 UCF HS Contest Problem: Three Letter Acronyms

import java.util.*;

public class tlas {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = Integer.parseInt(stdin.nextLine().trim());

		// Fill in hash map of substitutions.
		HashMap<String,String> map = new HashMap<String,String>();
		for (int i=0; i<n; i++) {
			String line = stdin.nextLine();
			map.put(line.substring(0, 3), line.substring(4));
		}

		// Build result here.
		String orig = "";

		// Read in each line, just as is and put into one string.
		while (stdin.hasNext()) {
			String line = stdin.nextLine();
			orig = orig + line;
			
			// Only in between lines, replacing '\n' with ' '.
			if (stdin.hasNext())
				orig = orig + " ";
		}
		
		// I have to do this manually, can't use replaceAll :(
		for (String key : map.keySet()) 
			orig = replace(orig, key, map.get(key));
		
		// Store output for each line.
		ArrayList<String> out = new ArrayList<String>();

		// Add next line to my current running buffer.
		String[] next = orig.split(" ");
		int printSpace = 0;
		String curLine = "";

		// Just go through all of the tokens. I increment in the loop body depending
		// on whether the word gets processed or not.
		for (int i=0; i<next.length;) {
					
			// We can add the next word to the current line.
			if (curLine.length() + next[i].length() + printSpace <= 70) {
				if (printSpace == 1) curLine = curLine+" ";
				curLine = curLine + next[i];
				printSpace = 1;
				i++;
			}
			
			// The current line is complete, let's save to our output buffer.
			else {
				out.add(curLine);
				curLine = "";
				printSpace = 0;
			}
		}
		
		// Just in case we haven't flushed the buffer.
		if (curLine.length() > 0) out.add(curLine);
		
		// Ta da!
		for (int i=0; i<out.size(); i++)
			System.out.println(out.get(i));
	}
	
	// Replaces TLA with sub in the text using all of their rules and returns the result.
	public static String replace(String text, String TLA, String sub) {
		
		// Just build it.
		String newS = "";
		int leftIdx = 0;
		
		// Go through each letter.
		for (int i=0; i<=text.length()-TLA.length(); i++) {
			
			// The substring we are trying to match with the TLA.
			String tmp = text.substring(i, i+3);
			
			// Here is where all of the action occurs.
			if (tmp.equals(TLA)) {
				
				// Probably a better way to write this, but I need to check it's by itself.
				boolean ok = true;
				
				// Need space to the left always.
				if (i>0 && text.charAt(i-1) != ' ') 
					ok = false;
				
				// Right can be space or period.
				if (i+TLA.length() < text.length() && text.charAt(i+TLA.length()) != ' '
				    && text.charAt(i+TLA.length()) != '.')
					ok = false;
				
				// Now we make a substitution.
				if (ok) {
					
					// We update the string we're building with the unchanged part.
					newS = newS + text.substring(leftIdx, i);
					
					// Check for capitalization.
					if (i==0 || text.charAt(i-2) == '.') 
						newS = newS + capFirst(sub);
					else
						newS = newS + sub;
					
					// Now update our leftIdx.
					leftIdx = i+TLA.length();
				}
			}
		}
		
		// The last bit.
		if (leftIdx < text.length())
			newS = newS + text.substring(leftIdx);
		
		// This should be the whole text with the correct substitutions for this TLA.
		return newS;
	}
	
	// Returns s with the first character, if lower case, changed to upper case. Otherwise,
	// s is returned.
	public static String capFirst(String s) {
		if (s.charAt(0) >= 'a' && s.charAt(0) <= 'z')
			return "" + (char)(s.charAt(0) - 'a' + 'A') + s.substring(1);
		return s;
	}
}