#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;}}