### main

loop do
  n = gets.strip.to_i
  break if n == 0

  tbl = {}
  n.times do
    c0, c1 = gets.strip.split(' ')
    tbl[c0] = c1
  end

  m = gets.strip.to_i

  input = ''
  output = ''

  m.times do
    c0 = gets.strip
    c1 = tbl[c0]

    input += c0
    output += c1.nil? ? c0 : c1
  end

  #puts "#{input} -> #{output}"
  puts output
end