while (n = gets.to_i) != 0 do
	t = {}
	n.times do
		k, v = gets.split
		t[k] = v
	end

	r = ""

	m = gets.to_i
	m.times do
		s = gets.chomp
		if t.has_key?(s) then
			r << t[s]
		else
			r << s
		end
	end

	puts r
end