require 'scanf'

def find_lowest_cost(items,recipes,goal)
    if not recipes.has_key?goal then
        return items[goal]
    end
    cost_single = items[goal]
    cost_renkin = 0
    for i in recipes[goal] do
        cost_renkin = cost_renkin + find_lowest_cost(items,recipes,i)
    end
    [cost_single,cost_renkin].min
end

while str=STDIN.gets
    n_item=str.to_i
    exit 0 if n_item==0

    items = Hash.new

    for i in 0..n_item-1 do
        name,cost = STDIN.gets.scanf("%s %d")
        items[name] = cost
    end
    
    recipes = Hash.new
    n_recipe=STDIN.gets.to_i
    for i in 0..n_recipe-1 do
        str = STDIN.gets.chomp
        l = str.split
        recipes[l[0]] = l.slice(2..(l.length-1))
    end

    goal = STDIN.gets.chomp
    print find_lowest_cost(items,recipes,goal),"\n"
end