while str = gets do
  num = str.chomp.to_i
  break if num == 0
  array = []
  num.times do |n|
    array << gets.chomp.split(" ")
  end
  snum = gets.chomp.to_i
  str = ""
  snum.times do |n|
    chr = gets.chomp
    change_str = array.find{|a| a[0] == chr}
    unless change_str.nil?
      chr = change_str[1]
    end
    str << chr
  end
  puts str
end