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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
struct item
{
	string name;
	int cost;
	bool havelist;
	vector<string> le;
};
item lis[100];
int memo[100];
int mysearch(string s1)
{
	int i;
	for(i=0;i<n;i++)
	{
		if(lis[i].name==s1)return i;
	}
}
int getcost(int v)
{
	if(memo[v]!=-1)return memo[v];
	else
	{
		if(!(lis[v].havelist))return lis[v].cost;
		else
		{
			int ans=0;
			for(int i=0;i<lis[v].le.size();i++)
			{
				ans+=getcost(mysearch(lis[v].le[i]));
			}
			lis[v].cost=min(lis[v].cost,ans);
			memo[v]=lis[v].cost;
			return memo[v];
		}
	}
}
int main()
{
	while(1)
	{
		cin >> n;
		if(n==0)break;
		fill(memo,memo+100,-1);
		string s;
		int c;
		for(int i=0;i<n;i++)
		{
			cin >> s >> c;
			lis[i].name=s;
			lis[i].cost=c;
			lis[i].havelist=false;
		}
		cin >> m;
		for(int i=0;i<m;i++)
		{
			string st;
			int t,k;
			cin >> st;
			cin >> t;
			k = mysearch(st);
			for(int j=0;j<t;j++)
			{
				string str;
				cin >> str;
				lis[k].le.pb(str);
			}
			lis[k].havelist=true;
		}
		string stri;
		cin >> stri;
		cout << getcost(mysearch(stri)) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<map>
using namespace std;main(){map<string,int>T;int n,k,s,v;string z,x,y;while(cin>>n&&n>0){while(n--){cin>>z>>v;T[z]=v;}cin>>n;while(n--){cin>>z>>k;s=0;while(k--){cin>>x;s+=T[x];}T[z]=min(T[z],s);}cin>>y;cout<<T[y]<<endl;}}

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

int main(){
  int n, price, m, sum;
  string item, ingre, wantMake;
  while(cin >> n, n){
    sum = 0;
    map <string,int> itemPrice;
    while(n--){
      cin >> item >> price;
      itemPrice.insert(map<string,int>::value_type(item,price));
    }
    cin >> m;
    while(m--){
      sum = 0;
      cin >> item >> price;
      while(price--){
	cin >> ingre;
	if(itemPrice.find(ingre)!=itemPrice.end()){
          sum += itemPrice[ingre];
	}
      }
      if(itemPrice[item]>sum){
	itemPrice[item] = sum;
      }
    }
    cin >> wantMake;
    cout << itemPrice[wantMake] <<endl;
  }
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
map<string,int> data;
map<string,vector<string> >ren;

int res(string s){
	if(!ren.count(s))return data[s];
	int ans = 0;
	for(int i =0;i <ren[s].size();i++){
		ans +=(res(ren[s][i]));
	}
	if(data[s]>ans){
		data[s]=ans;
	}
	return data[s];
}

int main(){
	int n,m;
	while(cin>>n,n){
		for(int i =0;i < n;i++){
			string item;
			int cst;
			cin>>item>>cst;
			data[item] =cst;
		}
		cin>>m;
		int k;
		for(int i =0;i <m;i++){
			string renkin;
			cin>>renkin>>k;
			//int sum=0;
			for(int j =0;j <k;j++){
				string tmp;
				cin>>tmp;
				ren[renkin].push_back(tmp);
			}
			//data[renkin]=min(sum,data[renkin]);
		}
		string ans;
		cin>>ans;
		cout <<res(ans)<<endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <string.h>

struct d{char M[101];int p;int r;int rk[101];};
d t[101];
int v[101];

int func(int n){
int c=t[n].rk[0];
int a=0;
v[n]=1;
if(t[n].rk[0]==0){
return t[n].p;
}
for(int i=1;i<=c;i++){
if(v[t[n].rk[i]]==1){
a+=t[t[n].rk[i]].p;
}
else if(t[t[n].rk[i]].r==0){
a+=t[t[n].rk[i]].p;
}
else {
a+=func(t[n].rk[i]);
}
}
if(a<t[n].p)
t[n].p=a;

return t[n].p;
}
int main(void){
int n,i,j,l;
while(scanf("%d",&n),n){

int m,k,V;
char T[101];
for(i=0;i<n;i++){
scanf("%s %d",t[i].M,&t[i].p);
}
scanf("%d",&m);
for(i=0;i<m;i++){

scanf("%s",T);
for(j=0;j<n;j++){
if(!(strcmp(t[j].M,T))){
t[j].r=1;
break;
}
}
V=j;
scanf("%d",&k);
t[V].rk[0]=k;
for(j=0;j<k;j++){
scanf("%s",T);
for(l=0;l<n;l++){
if(!(strcmp(t[l].M,T))){
t[V].rk[j+1]=l;
break;
}
}
}
}
scanf("%s",T);
for(i=0;i<n;i++){
if(!(strcmp(t[i].M,T))){
break;
}
}
printf("%d\n",func(i));

for(i=0;i<101;i++){
v[i]=0;
t[i].r=0;
t[i].p=0;
}
}
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(){
	int n,cost[101],x,y,z,b,a,p;
	string name[101],str;
	while(cin>>n,n){
		for(int i=1;i<=n;i++)cin>>name[i]>>cost[i];
		cin>>x;
		for(int i=1;i<=x;i++){
			cin>>str>>y;
			for(int j=1;j<=n;j++)if(str==name[j])b=cost[j],p=j;
			a=0;
			for(int j=1;j<=y;j++){
				cin>>str;
				for(int k=1;k<=n;k++)if(str==name[k])a+=cost[k];
			}
			if(b>a)cost[p]=a;
		}
		cin>>str;
		for(int i=1;i<=n;i++)if(str==name[i])cout<<cost[i]<<endl;
	}
	return 0;
}