Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"log"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		having := map[string]Entry{}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			name := buf[0]
			price, _ := strconv.Atoi(buf[1])
			having[name] = Entry{price, []string{}}
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			name_, price_ := buf_[0], INF
			if _, ok := having[name_]; ok {
				price_ = having[name_].price
			}
			having[name_] = Entry{price_, buf_[2:]}
		}
		scanner.Scan()
		fmt.Println(calculation(scanner.Text(), having))
		log.Printf("%+v\n", having)
	}
}

type Entry struct {
	price int
	elements []string
}

// メモ再帰的
func calculation(target string, having map[string]Entry) int {
	ans := INF
	if len(having[target].elements) > 0 {
		ans = 0
		for _, item := range having[target].elements {
			ans += calculation(item, having)
		}
	}
	return min(ans, having[target].price)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	while( cin >> n && n ){
		map< string, int > nameList;
		vector< long long int > valueList( n, 0 );
		for( int i = 0; i < n; ++i ){
			string s;
			int v;
			cin >> s >> v;
			nameList.insert( make_pair( s, i ) );
			valueList[ i ] = v;
		}
		int m;
		vector< vector< int > > recipeList( n, vector< int >() );
		cin >> m;
		for( int i = 0; i < m; ++i ){
			string o;
			int k;
			cin >> o >> k;
			map< string, int >::iterator itr = nameList.find( o );
			if( itr == nameList.end() ){
				continue;
			}
			int productId = itr->second;
			for( int j = 0; j < k; ++j ){
				string q;
				cin >> q;
				itr = nameList.find( q );
				if( itr == nameList.end() ){
					recipeList[ productId ] = vector< int >();
					break;
				}
				int materialId = itr->second;
				recipeList[ productId ].push_back( materialId );
			}
		}
		vector< int > depthList( n, -1 );
		queue< int > updateList;
		for( int i = 0; i < n; ++i ){
			updateList.push( i );
		}
		while( !updateList.empty() ){
			int id = updateList.front();
			updateList.pop();
			if( recipeList[ id ].empty() ){
				depthList[ id ] = 0;
				continue;
			}
			int size = recipeList[ id ].size();
			int d = -1;
			for( int i = 0; i < size; ++i ){
				int materialDepth = recipeList[ id ][ i ];
				if( materialDepth == -1 ){
					d = -1;
					break;
				}
				d = max( d, materialDepth );
			}
			if( d > -1 ){
				depthList[ id ] = d + 1;
			}else{
				updateList.push( id );
			}
		}
		vector< pair< int, int > > sortedList;
		for( int i = 0; i < n; ++i ){
			sortedList.push_back( make_pair( depthList[ i ], i ) );
		}
		sort( sortedList.begin(), sortedList.end() );

		for( int i = 0; i < (int)sortedList.size(); ++i ){
			int id = sortedList[ i ].second;
			vector< int > recipe = recipeList[ id ];
			if( recipe.empty() ){
				continue;
			}
			long long int value = 0;
			for( int j = 0; j < (int)recipe.size(); ++j ){
				value += valueList[ recipe[ j ] ];
			}
			if( value < valueList[ id ] ){
				valueList[ id ] = value;
			}
		}

		string ansName;
		cin >> ansName;
		cout << valueList[ nameList.find( ansName )->second ] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define all(a)  (a).begin(),(a).end()
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define exist(s,e) ((s).find(e)!=(s).end())
#define pb push_back
#define range(i,a,b) for(int (i)=(a);(i)<(b);++(i))
#define rep(i,n)  range(i,0,n)
#define dump(x)  cerr << #x << " = " << (x) << endl;

using namespace std;

typedef vector<int> vi;
typedef long long ll;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

const int vmax=110;
int value[vmax];
vector<int> graph[vmax];
string item[vmax];
map<string,int> convert;


int dfs(int v){
	if(graph[v].size()==0) return value[v];
	int sum=0;	
	for(auto &v2:graph[v]) sum+=dfs(v2);
	return min(value[v],sum);
}

int main(void){
	int n;
	while(cin >> n,n){
		// reset
		rep(i,vmax) graph[i].clear();
		convert.clear();

		rep(i,n) cin >> item[i] >> value[i];
		rep(i,n) convert[item[i]]=i;
		int m;
		cin >> m;
		rep(i,m){
			string parent;
			cin >> parent;
			int l;
			cin >> l;
			rep(j,l){
				string child;
				cin >> child;
				graph[convert[parent]].push_back(convert[child]);
			}
		}
		string target;
		cin >> target;
		cout << dfs(convert[target]) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int n, m;
map<string, int> items;
map<string, vector<string> > alc;

map<string, int> memo;
int minPrice(const string& t) {
	vector<string>& mats = alc[t];
	if (!mats.size()) return items[t];
	map<string, int>::iterator it = memo.find(t);
	if (it != memo.end()) return it->second;
	int ap = 0;
	for (unsigned i = 0; i < mats.size(); i++) {
		ap += minPrice(mats[i]);
	}
	return memo[t] = min(items[t], ap);
}

int main () {
	while (cin >> n, n) {
		items.clear();
		alc.clear();
		memo.clear();
		for (int i = 0; i < n; i++) {
			string name;
			int value;
			cin >> name >> value;
			items[name] = value;
		}
		cin >> m;
		for (int i = 0; i < m; i++) {
			string name;
			int k;
			cin >> name >> k;
			vector<string> mats;
			for (int j = 0; j < k; j++) {
				string mat;
				cin >> mat;
				mats.push_back(mat);
			}
			alc[name] = mats;
		}
		string target;
		cin >> target;
		cout << minPrice(target) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main(){

	map<string, int> end;
	int m;
	int cost; string material;
	while (cin >> m, m){
		for (; m > 0; m--){
			cin >> material >> cost;
			end.insert(map<string, int>::value_type(material, cost));
		}
		cin >> m;
		for (; m > 0; m--){
			string sozai; int kaisuu; int sum = 0;
			cin >> sozai >> kaisuu;
			for (; kaisuu > 0; kaisuu--){
				cin >> material;
				sum += end[material];
			}
			if (sum < end[sozai]) end[sozai] = sum;
			sum = 0;
		}
		string in;
		cin >> in;
		cout << end[in] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#define mod 1000000007
using namespace std;
map<string,int> cost,nz;
map<string,vector<string> > parent,child;
vector<string> v;
void DFS(string s){
	int ans=0;
	for(int i=0;i<child[s].size();i++){
		ans+=cost[child[s][i]];
	}
	ans=min(ans,cost[s]);
	if(child[s].size()>0)cost[s]=ans;
	for(int i=0;i<parent[s].size();i++){
		string to=parent[s][i];
		nz[to]--;
		if(nz[to]==0){
			DFS(to);
		}
	}
}
int main(){
	int n,m;
	while(cin>>n&&n){
		cost.clear();
		nz.clear();
		child.clear();
		parent.clear();
		v.clear();
		for(int i=0;i<n;i++){
			string s;int a;
			cin>>s>>a;
			cost[s]=a;
			nz[s]=0;
			v.push_back(s);
		}
		cin>>m;
		for(int i=0;i<m;i++){
			string s;int k;
			cin>>s>>k;
			nz[s]=k;
			for(int j=0;j<k;j++){
				string r;cin>>r;
				child[s].push_back(r);
				parent[r].push_back(s);
			}
		}
		for(int i=0;i<v.size();i++){
			if(nz[v[i]]==0)DFS(v[i]);
		}
		string s;cin>>s;
		cout<<cost[s]<<endl;
	}
	return 0;
}