
// @snippet<sh19910711/contest:headers.cpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <limits>
#include <complex>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

// @snippet<sh19910711/contest:table/trie.cpp>
namespace table {
    template <class ValueType> class Trie {
    public:
        typedef std::string::const_iterator StringIterator;
        static const int SIZE = 256;
        ValueType value;
        Trie *next[SIZE];
        Trie() { clear(); }
        void clear() { std::fill( next, next+SIZE, (Trie*)NULL ); }
        Trie& find( const std::string& s ) {
            Trie* res = this;
            for ( StringIterator it_i = s.begin(); it_i != s.end(); ++ it_i ) {
                const int& c = *it_i;
                if ( ! res->next[c] ) res->next[c] = new Trie;
                res = res->next[c];
            }
            return *res;
        }
        int count( const std::string& s ) {
            Trie* res = this;
            for ( StringIterator it_i = s.begin(); it_i != s.end(); ++ it_i ) {
                const int& c = *it_i;
                if ( ! res->next[c] ) return 0;
                res = res->next[c];
            }
            return 1;
        }
        ValueType& operator []( const std::string& s ) { return find(s).value; }
    };
}

// @snippet<sh19910711/contest:solution/interface.cpp>
namespace solution {
    class ISolution {
    public:
        virtual void init() {};
        virtual bool input() { return false; };
        virtual void output() {};
        virtual int run() = 0;
    };
}

// @snippet<sh19910711/contest:solution/solution.cpp>
namespace solution {
    using namespace std;
    class Solution: public ISolution {
    public:
        table::Trie<int> T;
        int n;
        void init() {
            T.clear();
        }
        bool input() {
            if ( ! ( cin >> n && n ) )
                return false;
            for ( int i = 0; i < n; ++ i ) {
                string item;
                int value;
                cin >> item >> value;
                T[item] = value;
            }
            return true;
        }
        void solve() {
            int m;
            cin >> m;
            for ( int i = 0; i < m; ++ i ) {
                string item;
                cin >> item;

                int k;
                cin >> k;

                int sum = 0;
                for ( int j = 0; j < k; ++ j ) {
                    string item;
                    cin >> item;
                    sum += T[item];
                }
                T[item] = min( T[item], sum );
            }
            string q;
            cin >> q;
            cout << T[q] << endl;
        }
        int run() {
            while ( init(), input() ) {
                solve();
                output();
            }
            return 0;
        }
    };
}


// @snippet<sh19910711/contest:main.cpp>

int main() {
    return solution::Solution().run();
}