#include <iostream>
#include <map>
#include <string>
#include <algorithm>

int main()
{
    std::map<std::string, int> ItemData;
    int Item_num;
    
    while (std::cin >> Item_num) {
        if (Item_num == 0) break;
        
        
        for (int i = 0; i < Item_num; ++i) {
            std::string ItemName;
            int ItemPrice;
            
            std::cin >> ItemName >> ItemPrice;
            ItemData[ItemName] = ItemPrice;
        }
        
        int mix_num;
        
        std::cin >> mix_num;
        
        for (int i = 0; i < mix_num; ++i) {
            std::string ItemName;
            int Material_num;
            
            std::cin >> ItemName >> Material_num;
            
            int temp = 0;
            std::string MaterialName;

            for (int j = 0; j < Material_num; ++j) {
                std::cin >> MaterialName;
                temp += ItemData[MaterialName];
                ItemData[ItemName] = std::min(ItemData[ItemName], temp);
            }
        }
        
        std::string Material;
        
        std::cin >> Material;
        
        std::cout << ItemData[Material] << std::endl;
    }
    
    return 0;
}