answers = []

while (convert_num = gets.to_i) != 0
  convert_table = {}
  convert_num.times do
    befor_char, after_char = gets.chomp.split
    convert_table[befor_char] = after_char
  end

  output = ""
  gets.to_i.times do
    data = gets.chomp
    if (char = convert_table[data]) != nil
      output += char
    else
      output += data
    end
  end

  answers << output
end

puts answers