def exchange(str, bef, aft)
	if str == bef
		return aft
	else
		return str
	end
end
while n=gets.to_i do
	exit if n==0
	$results=[]
	ctn=0
	a,b=Array.new(n), Array.new(n)
	n.times do |i|
		a[i],b[i]=gets.chomp.split
	end

	gets.to_i.times do
		str=gets.chomp
		result=str
		while !(ctn==n||result!=str)
			result = exchange(str, a[ctn], b[ctn])
			ctn+=1
		end
		$results<<result
		ctn=0
	end
	puts $results.join
end