import std.stdio;
import std.conv;
import std.string;

void main()
{
	while(true)
	{
		string[] input = split(chomp(readln()));
		int N = to!int(input[0]);
		if(N==0) break;

		char[] from = new char[N];
		char[] toto = new char[N];

		for(int i=0; i<N; i++) {
			input = split(chomp(readln()));
			from[i] = to!char(input[0]);
			toto[i] = to!char(input[1]);
		}

		input = split(chomp(readln()));
		int M = to!int(input[0]);

		string s;
		for(int i=0; i<M; i++) {
			input = split(chomp(readln()));
			s ~= input[0];
		}

		writeln(translate(s, maketrans(from, toto), ""));
	}
		
}