while (n1 = gets.to_i) != 0
  h = Hash.new
  n1.times do
    c1, c2 = gets.split(' ')
    h[c1] = c2
  end
  n2 = gets.to_i
  str = ''
  n2.times do
    c = gets.chomp!
    c = h[c] if h[c]
    str << c
  end
  puts str
end