#include<iostream>
#include<map>
#include<string>
using namespace std;int main(){int n,m,p;string s,t;for(;cin>>n,n;){map<string,int>P;for(;n--;cin>>P[s])cin>>s;for(cin>>n;n--;){cin>>s>>m;for(p=0;m--;p+=P[t])cin>>t;P[s]=min(P[s],p);}cin>>s;cout<<P[s]<<endl;}}