while (n=gets.to_i)!=0
    table = {}
    for i in 1..n
        a,b = gets.split(" ")
        table[a] = b
    end


    n = gets.to_i
    ans = ""
    for i in 1..n
        c = gets[0]
        if table[c]!=nil
            c = table[c]
        end
        ans += c[0]
    end

    puts(ans)
end