#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <map>

#define rep(i,n) for(int i=0; i<(n); i++)

using namespace std;

typedef vector<vector<int> > graph;

void getReversedGraph(graph& G, graph* rG) {
    *rG = graph(G.size());
    for (int i = 0; i < (int)G.size(); i++) {
        for (int j = 0; j < (int)G[i].size(); j++) {
            (*rG)[G[i][j]].push_back(i);
        }
    }
}

void getSCCGraph(graph& G, graph* SCCGraph, vector<int>* table) {
    const int V = G.size();
    graph rG;
    vector<vector<int> > SCCList;
    vector<int> postOrder;
    bool *used = new bool[V];
    int *nextIndex = new int[V];
    *table = vector<int>(V);
    getReversedGraph(G, &rG);

    // DFS
    for (int i = 0; i < V; i++) {
        used[i] = false;
        nextIndex[i] = 0;
    }

    for (int i = 0; i < V; i++) {
        if (used[i]) {
            continue;
        }
        used[i] = true;
        stack<int> st;
        st.push(i);
        while (!st.empty()) {
            int v = st.top();
            if (nextIndex[v] == (int)G[v].size()) {
                postOrder.push_back(v);
                st.pop();
            } else {
                int newv = G[v][nextIndex[v]];
                if (!used[newv]) {
                    st.push(newv);
                    used[newv] = true;
                }
                nextIndex[v]++;
            }
        }
    }
    // Reversed DFS
    for (int i = 0; i < V; i++) {
        used[i] = false;
        nextIndex[i] = 0;
    }

    int it = 0;
    for (int i = V - 1; i >= 0; i--) {
        int index = postOrder[i];
        if (used[index]) {
            continue;
        }
        used[index] = true;
        (*table)[index] = it;
        stack<int> st;
        vector<int> list;
        st.push(index);
        list.push_back(index);
        while (!st.empty()) {
            int v = st.top();
            if (nextIndex[v] == (int)rG[v].size()) {
                st.pop();
            } else {
                int newv = rG[v][nextIndex[v]];
                if (!used[newv]) {
                    st.push(newv);
                    list.push_back(newv);
                    used[newv] = true;
                    (*table)[newv] = it;
                }
                nextIndex[v]++;
            }
        }
        SCCList.push_back(list);
        it++;
    }

    // create new graph;
    const int compressedV = it;
    *SCCGraph = graph(compressedV);
 
    for (int i = 0; i < compressedV; i++) {
        map<int, bool> edgeUsed;
        for (int j = 0; j < (int)SCCList[i].size(); j++) {
            int v = SCCList[i][j];
            for (int k = 0; k < (int)G[v].size(); k++) {
                int w = G[v][k];
                // v -> w
                if (!edgeUsed[(*table)[w]]) {
                    edgeUsed[(*table)[w]] = true;
                    if (i != (*table)[w]) (*SCCGraph)[i].push_back((*table)[w]);
                }
            }
        }
    }

    delete[] used;
    delete[] nextIndex;
}

bool used[2005];

void check(int v, graph& G, bool* used) {
    if (used[v])return;
    used[v] = true;
    rep(i, G[v].size()) {
        check(G[v][i], G, used);
    }
}

int solve(int n) {
    string s1,s2;
    int d1,d2;
    graph G(n), sccG, rG;
    vector<int> table;
    map<string, int> toId;
    map<int, int> plus;
    int ans = 0;
    int id = 0;
    rep(i,n) {
        cin >> s1 >> d1 >> s2 >> d2;
        if (toId.find(s1) == toId.end()) {
            toId[s1] = id;
            id++;
        }
        if (toId.find(s2) == toId.end()) {
            toId[s2] = id;
            id++;
        }
        ans+= d2;
        if (s1 != s2)G[toId[s2]].push_back(toId[s1]);
        plus[toId[s1]] = d1 - d2;
    }
    
    getSCCGraph(G, &sccG, &table);
    rep(i, 2005)used[i] = false;
    rep(i, sccG.size()) {
        vector<int> p;
        if (used[i])continue;
        //cerr << i << endl;
        //cerr << "\t";
        rep(j, table.size()) {
            if (table[j] == i) {
                p.push_back(plus[j]);
                //cerr << j << ":" << p[j] << " ";
            }
        }
        //cerr << endl;
        sort(p.begin(), p.end());
        ans += p[0];
        check(i, sccG, used);
    }
    return ans;
}

int main() {
    int n;
    while(cin >> n, n) {
        cout << solve(n) << endl;
    }
    return 0;
}

Measured memory_bytes: 1584