require 'set'

class Graph
	def initialize
		@nodes = Set.new
		@edges = Set.new
	end

	def add_node(label = nil)
		Node.new(label)
	end

	def add_edge(u, v)
		e = Edge.new(u, v)
		u.outgoings << e
		v.incomings << e
		e
	end
	
	def del_edge(e)
		u, v = e.from, e.to
		u.outgoings.delete(e)
		v.incomings.delete(e)
		e
	end

	class Node
		def initialize(label)
			@label = label
			@outgoings = Set.new
			@incomings = Set.new
		end
		
		def to_s
			@label
		end
		attr_accessor :incomings, :outgoings, :label
	end

	class Edge
		def initialize(u, v)
			@from = u	
			@to = v
		end
		attr_reader :from, :to
	end

	def get_path_dfs(s, t, reached = Set.new)
		reached << s	
		s.outgoings.each{|e|
			v = e.to
			next if reached.include?(v)
			return [e] if v == t
			
			path = get_path_dfs(v, t, reached) 
			return [e] + path if path
		}
		nil
	end
end

def get_ints
	gets.split.map(&:to_i)
end

def min_cost(v, reached = Set.new)
	reached << v
	return v.label if v.incomings.empty? || v.incomings.any?{|e| reached.include?(e.from)}
	compose_cost = v.incomings.map{|e| min_cost(e.from, reached)}.inject(&:+)
	[v.label, compose_cost].min
end

loop {
	n = gets.to_i
	break if n == 0

	g = Graph.new
	nodes = {}

	n.times {
		name, price = gets.split
		price = price.to_i
		nodes[name] = g.add_node(price)
	}
	p nodes

	m = gets.to_i
	m.times {
		a = gets.split
		v = nodes[a.shift]
		a.shift
		a.each {|name| g.add_edge(nodes[name], v)}
	}

	target = gets.chomp
	p min_cost(nodes[target])
}