#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
 
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+9
#define EPS 1e-9
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < (k); i++)
typedef long long ll;

int main(){
    int n;
    while(scanf("%d",&n),n){
        map<string, int> data;
        rep(i,n){
            string str;
            int val;
            cin >> str >> val;
            data[str] = val;
        } 
        int m;
        scanf("%d",&m);
        rep(i,m){
            string o;
            cin >> o;
            int k;
            scanf("%d",&k);
            int sum = 0;
            rep(i,k){
                string from;
                cin >> from;
                sum += data[from];
            }
            data[o] = min(data[o],sum);
        }
        string want;
        cin >> want;
        cout << data[want] << endl;
    }
    return 0;
}