#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

class recipe{
public:
	vector<string> src;
	string dst;
	int num;
};

int main(void){
	int n;
	
	while(cin >> n && n){
		int k;
		map<string,int> m;
		vector<recipe> rs;
		string str;
		
		int tmp;
		for(int i=0;i<n;i++){
			cin >> str >> tmp;
			m[str] = tmp;
		}
		cin >> n;
		for(int i=0;i<n;i++){
			recipe r;
			cin >> str;
			r.dst = str;
			cin >> k;
			r.num = k;
			for(int i=0;i<k;i++){
				cin >> str;
				r.src.push_back(str);
			}
			rs.push_back(r);
		}
		
		bool ended = false;
		while(!ended){
			ended = true;
			for(int i=0;i<rs.size();i++){
				tmp = 0;
				cout << rs[i].num << endl;
				for(int j=0;j<rs[i].num;j++){
					tmp += m[rs[i].src[j]];
				}
				if(m[rs[i].dst] > tmp){
					ended = false;
					m[rs[i].dst] = tmp;
				}
			}
		}
		cin >> str;
		cout << m[str] << endl;
	}
}