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 <iomanip>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iterator>
#include <queue>
#include <stack>
#include <deque>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <complex>
#include <functional>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <cctype>
#include <bitset>

#define REP(i,m,n) for(int i=m;i<(int)n;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rrep(i,n) for(int i = 1; i <= n; i++)
#define drep(i,n) for(int i = n; i >= 0; i--)
#define rp(i,c) rep(i,(int)c.size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(u) (u).rbegin(),(u).rend()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl
#define v_delete(a,b) (a).erase(remove((a).begin(), (a).end(), b), (a).end())
#define v_unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define VV(T) vector<vector< T > >
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int inf=1<<28;
const double INF=1e10,EPS=1e-9;


template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}

template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }

int chmax(int& a,int b){
	if(b > a){
		a=b;
	}
	return a;
}
int chmin(int& a,int b){
	if(b < a){
		a=b;
	}
	return a;
}

int N, M;

map<string, int>dict;

int d[100+10];

vector<vector<int> >graph;

int dfs(int n){
	int a = d[n];
	int tmp=inf;
	rp(i, graph[n]){
		if(tmp==inf)
			tmp=0;
		tmp += dfs(graph[n][i]);
	}

	return min(a, tmp);
}

int main(int argc, char const *argv[])
{
	while(cin >> N, N){
		graph.clear();
		graph.resize(110);
		dict.clear();
		memset(d, 0, sizeof(d));
		rep(i, N){
			string s;int j;
			cin >> s >> j;
			d[i] = j;
			dict[s] = i;
		}
		cin >> M;
		rep(i, M){
			string s;
			int j;
			cin >> s >> j;
			rep(k,j){
				string _s;
				cin  >> _s;
				graph[dict[s]].pb(dict[_s]);
			}
		}
		string cue;
		cin >> cue;
		cout << dfs(dict[cue]) << endl;
	}




	return 0;
}

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

map<string,int> normal;
map<string,vector<string> > rec;

int f(string x){
	int ans = normal[x] , sum = 0;
	if( rec[x].size() == 0 ) return ans;
	for(int i = 0 ; i < rec[x].size() ; i++){
		sum += f(rec[x][i]);
	}
	return min(ans,sum);
}

int main(){
	int n;
	while(cin >> n && n){
		normal.clear();
		rec.clear();
		for(int i = 0 ; i < n ; i++){
			string a;
			int b;
			cin >> a >> b;
			normal[a] = b;
		}
		int m;
		cin >> m;
		for(int i = 0 ; i < m ; i++){
			string x,y;
			cin >> x;
			int t;
			cin >> t;
			for(int j = 0 ; j < t ; j++){
				cin >> y;
				rec[x].push_back(y);
			}
		}
		string want;
		cin >> want;
		cout << f(want) << endl;
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int func(string name,  map<string,int> &list,  map<string,vector<string> > &renkin){
  if(renkin.find(name)==renkin.end()) return list[name];
  int ret = 0;
  vector<string> mat = renkin[name];
  REP(i,mat.size()){
    ret += func(mat[i],list,renkin);
  }
  return min(list[name],ret);
}

int main(void){
  int n,m;
  while(cin>>n){
    if(n==0) break;
    map<string,int> list;
    map<string,vector<string> > renkin;
    REP(j,n){
      string i;
      int p;
      cin>>i>>p;
      list[i] = p;
    }
    cin>>m;
    REP(i,m){
      string o;
      int k;
      cin>>o>>k;
      vector<string> q(k);
      REP(j,k) cin>>q[j];
      renkin[o] = q;
    }
    string item;
    cin>>item;
    cout<<func(item,list,renkin)<<endl;
  }
  return 0;
}