// Arup Guha
// 8/1/2026
// Solution to 1989 UCF HS Programming Contest Problem: Mad Caps

import java.util.*;

public class madcaps {
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		
		int nW = Integer.parseInt(stdin.nextLine().trim());
		char[][] words = new char[nW][];
		
		// Read in the words.
		for (int i=0; i<nW; i++)
			words[i] = stdin.nextLine().trim().toCharArray();
		
		// Dummy line to throw away.
		stdin.nextLine();
		
		// Store all the next in advance.
		ArrayList<char[]> lines = new ArrayList<char[]>();
		while (stdin.hasNext()) 
			lines.add( (stdin.nextLine()).toCharArray() );
			
		// First fix capitalization of the first letter in each word and
		// make everything else lower case.
		fixFirst(lines);
		
		// Now fix our proper noun list.
		for (int i=0; i<nW; i++)
			apply(lines, words[i]);
			
		// Ta da!
		for (int i=0; i<lines.size(); i++)
			System.out.println(new String(lines.get(i)));
	}
	
	// Fixes capitalization based on sentences.
	/*** I assume sentences only contain letters, the punctuation listed and white space
	     and that each sentence has at least one letter, so you can't do something like
		 .? or .,. for example. I guess that wouldn't really mess with anything but it's
		 annoying and against the spirit of the problem in my opinion. Also, I am assuming
		 that the first non white-space character after the end of a sentence is a letter.
		 This isn't explicitly stated, but again, I believe that to be against the spirit
		 of the problem.
	***/
	public static void fixFirst(ArrayList<char[]> lines) {
		
		// Whether or not the last non-space character was the end of a sentence.
		boolean prevEnd = true;
		
		for (int i=0; i<lines.size(); i++) {
			for (int j=0; j<lines.get(i).length; j++) {
			
				// Current char.
				char c = lines.get(i)[j];
				
				// Check if it's a letter.
				if (Character.isLetter(c)) {
					
					// Here is our letter rule. It's clear.
					if (prevEnd) 	lines.get(i)[j] = Character.toUpperCase(c);
					else 			lines.get(i)[j] = Character.toLowerCase(c);
					
					// For next round. Only way we can turn this off.
					if (prevEnd) prevEnd = false;
				}
				
				// Mark the end of a sentence.
				else if (c == '.' || c == '?' || c == '!')
					prevEnd = true;
				
				// All other characters don't change any relevant state.
			}
		}
	}
	
	// This capitalizes word...by farming the work out to another method!
	public static void apply(ArrayList<char[]> lines, char[] word) {
		for (int i=0; i<lines.size(); i++) 
			applyLine(lines.get(i), word);
	}
	
	public static void applyLine(char[] line, char[] word) {
		
		// So we can use Java stuff.
		String sLine = new String(line);
		sLine = sLine.toLowerCase();
		String sWord = new String(word);
		sWord = sWord.toLowerCase();
		
		// Get first match.
		int loc = sLine.indexOf(sWord);
		
		// Go until no more matches.
		while (loc != -1) {
			
			// Capitalize if it's by itself.
			boolean ok = true;
			if (loc>0 && Character.isLetter(line[loc-1])) ok = false;
			int endLoc = loc+sWord.length();
			if (endLoc < line.length && Character.isLetter(line[endLoc])) ok = false;
			
			// Here is when we capitalize.
			if (ok) line[loc] = Character.toUpperCase(line[loc]);
			
			// Clear this word.
			while (loc < line.length && Character.isLetter(loc)) loc++;
			
			// Now go to next match.
			loc = sLine.indexOf(sWord, loc+sWord.length());
		}
	}
	
}