while (n = gets.to_i) != 0
  table = {}
  n.times do
    key,val = gets.chomp.split(" ")
    table[key] = val
  end

  m = gets.to_i
  ans = []*m
  0.upto(m-1) do |i|
    ch = gets.chomp
    unless table[ch].nil?
      ch = table[ch]
    end
    ans[i] = ch
  end

  puts ans.join
end