# Here your code !
until (n = gets.to_i) == 0 do

    dic = Array.new
    
    for i in 0...n do
        dic[i] = gets.split(" ").map(&:to_s)
    end
    
    m = gets.to_i
    
    for j in 0...m do
        printed = false
        str = gets.to_s.strip
        
        for i in 0...n do
            if dic[i][0] == str then
                printf("#{dic[i][1]}")
                printed = true
            end
        end
        if !printed then
            printf("#{str}")
        end
    end
    printf("\n")
end