import std.stdio, std.array, std.conv, std.algorithm, std.string, std.numeric, std.range;
void main() {
        int n;
        while((n=readln.strip.to!int)!=0){
                char[char] con;
                foreach(i; 0..n){
                        auto r = readln.strip.split(' ');
                        con[r[0][0]] = r[1][0];
                }
                n = readln.strip.to!int;
                foreach(i; 0..n){
                        auto c = readln[0];
                        if(c in con){
                                write(con[c]);
                        }else{
                                write(c);
                        }
                }
                writeln();
        }
}