while true
  n = gets.to_i
  break if n == 0
  c = Array.new(n){gets.split(" ")}
  m = gets.to_i
  str = ""
  cur = 0
  m.times do
    l = gets.chomp
    if l == c[cur][0] && cur<n
      l = c[cur][1]
      cur += 1
    end
    str += l
  end
  puts str
end