#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;


struct Dscc {
public:

	//fst:belongs , scd:newedges
	pair<vector<vector<int>>, vector<vector<int>>>get(const vector<vector<int>>&edges) {
		nums.resize(edges.size());
		fill(nums.begin(), nums.end(), -1);
		vector<vector<int>>revedges(edges.size());
		for (int i = 0; i < edges.size(); ++i) {
			for (auto j : edges[i]) {
				revedges[j].push_back(i);
			}
		}
		int num = 0;
		for (int i = 0; i < edges.size(); ++i) {
			dfs(i, num, edges);
		}
		vector<int>big(nums.size());
		for (int i = 0; i < nums.size(); ++i) {
			big[nums[i]] = i;
		}
		reverse(big.begin(), big.end());
		unis.resize(edges.size());
		fill(unis.begin(), unis.end(), -1);
		num = 0;
		for (int i = 0; i < big.size(); ++i) {

			dfs2(big[i], num, revedges);
			num++;
		}
		vector<int>nums;
		for (int i = 0; i < unis.size(); ++i) {
			nums.push_back(unis[i]);
		}
		sort(nums.begin(), nums.end());
		nums.erase(unique(nums.begin(), nums.end()), nums.end());


		map<int, int>mp;
		for (int i = 0; i < nums.size(); ++i) {
			mp[nums[i]] = i;
		}
		for (int i = 0; i < unis.size(); ++i) {
			unis[i] = mp[unis[i]];
		}

		vector<vector<int>>belongs(nums.size());
		for (int i = 0; i < unis.size(); ++i) {
			belongs[unis[i]].push_back(i);
		}
		vector<vector<int>>newedges(nums.size());
		for (int i = 0; i < edges.size(); ++i) {
			for (auto j : edges[i]) {
				if (unis[i] != unis[j]) {
					newedges[unis[i]].push_back(unis[j]);
				}
			}
		}
		return make_pair(belongs, newedges);
	}
private:
	vector<int>nums;
	vector<int>unis;


	void dfs(const int id, int &num, const vector<vector<int>>&edges) {
		if (nums[id] != -1)return;
		else {
			nums[id] = -2;
			for (auto i : edges[id]) {
				dfs(i, num, edges);
			}
		}
		nums[id] = num++;
		return;
	}
	void dfs2(const int id, const int &num, const vector<vector<int>>&edges) {
		if (unis[id] != -1)return;
		else {
			unis[id] = -2;
			for (auto i : edges[id])
				dfs2(i, num, edges);
		}
		unis[id] = num;
		return;
	}

}dscc;

int get_id(string st,bool flag=false) {
	static map<string,int>mp;
	static int id=0;
	if (flag) {
		mp.clear();
		id=0;
		return 0;
	}
	if (mp.find(st) == mp.end()) {
		mp[st]=id++;
	}
	return mp[st];
}

int main() {
	while (true) {
		get_id("", true);
		int N;cin>>N;
		if(!N)break;

		vector<int>oris(N);
		vector<int>acomes(N);
		vector<vector<int>>edges(N);

		vector<int>ids(N);
		for (int i = 0; i < N; ++i) {
			string ast;
			int b;
			string cst;
			int d;
			cin>>ast>>b>>cst>>d;
			int a=get_id(ast);
			int c=get_id(cst);

			oris[i]=b;
			acomes[i]=d;
			if(a!=c)edges[c].push_back(a);

			ids[a]=i;
		}

		auto p=dscc.get(edges);

		vector<int>comes(p.second.size());
		for (auto es : p.second) {
			for (auto e : es) {
				comes[e]++;
			}
			
		}

		int ans=accumulate(acomes.begin(),acomes.end(),0);
		for(int i=0;i<p.first.size();++i){
			if(comes[i])continue;
			auto v(p.first[i]);
			int minus_max=-1e9;
			for (auto a : v) {
				minus_max=max(minus_max,acomes[ids[a]]-oris[ids[a]]);
			}
			ans-=minus_max;
		}
		cout<<ans<<endl;
		
	}
	return 0;
}


Measured memory_bytes: 3424