while n = gets.to_i
menu1=Hash.new{|item,price|}
menu2=Hash.new{|item,price|}
	break if n==0
	n.times{
    	item,price= gets.chomp.split(" ")
		menu1[item]= price.to_i
	}
	m = gets.to_i
	m.times{
		recipt1 = gets.chomp.split(" ")
		recipt2 = recipt1.dup
		recipt2.shift
		recipt2.shift
		recipt2=recipt2.map{|i| menu1[i]}
		
		if menu1[recipt1[0]]>recipt2.inject{|sum,n| sum+n}
			menu1[recipt1[0]]=recipt2.inject{|sum,n| sum+n}
		end
	}
create=gets.chomp
puts menu1[create]

end