until (n = gets.to_i).zero?
  costs = n.times.map { gets.split.tap {|a| a[1] = a[1].to_i} }.to_h
  
  m = gets.to_i
  recipe = Hash.new {|hash, key| hash[key] = []}
  m.times do
    given = gets.split
    recipe[given[0]] += given[2..-1]
  end
  goal = gets.chomp
  
  try = ->(parent) {
    r = recipe[parent]
    c = costs[parent]
    if r.empty?
      c
    else
      made = r.map {|child| try.(child)}.sum
      [made, c].min
    end
  }
  puts try.(goal)
end
