#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct GROUP{
	vector<int> nodes;
};


struct Info{
	int day1,sup_id,day2;
	char name[33],sup_name[33];
};

int N;
int height[1000],parent[1000],num_in[1000];

Info info[1000];
vector<int> G[1000];
vector<int> reverse_G[1000];

GROUP group[1000];
stack<int> S;

bool check[1000];
int table[1000];

int group_index;

bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}


void dfs(int node_id){
	check[node_id] = true;

	for(int i = 0; i < G[node_id].size(); i++){
		if(!check[G[node_id][i]])dfs(G[node_id][i]);
	}
	S.push(node_id);
}

void reverse_dfs(int node_id){
	check[node_id] = true;

	group[group_index].nodes.push_back(node_id);
	table[node_id] = group_index;

	for(int i = 0; i < reverse_G[node_id].size(); i++){
		if(!check[reverse_G[node_id][i]])reverse_dfs(reverse_G[node_id][i]);
	}
}

void func(){

	for(int i = 0; i < N; i++){
		group[i].nodes.clear();
		G[i].clear();
		reverse_G[i].clear();
	}

	for(int i = 0; i < N; i++){
		scanf("%s %d %s %d",info[i].name,&info[i].day1,info[i].sup_name,&info[i].day2);
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k < N; k++){
			if(strCmp(info[k].name,info[i].sup_name)){
				info[i].sup_id = k;

				if(i != k){
					G[k].push_back(i);
					reverse_G[i].push_back(k);
				}
				break;
			}
		}
	}

	for(int i = 0; i < N;i++)check[i] = false;

	for(int i = 0; i < N;i++){
		if(!check[i])dfs(i);
	}

	for(int i = 0; i < N;i++)check[i] = false;

	group_index = -1;

	while(!S.empty()){
		if(!check[S.top()]){
			group_index++;

			reverse_dfs(S.top());
		}
		S.pop();
	}

	for(int i = 0; i < N; i++)num_in[i] = 0;

	int to_group,min_index,minimum;

	for(int i = 0; i <= group_index; i++){
		for(int k = 0; k < group[i].nodes.size(); k++){
			for(int p = 0; p < G[group[i].nodes[k]].size(); p++){
				to_group = table[G[group[i].nodes[k]][p]];
				if(to_group != i)num_in[to_group]++;
			}
		}
	}

	int ans = 0;

	for(int i = 0; i <= group_index;i++){
		if(group[i].nodes.size() == 1){
			if(num_in[i] == 0){
				ans += info[group[i].nodes[0]].day1;
			}else{
				ans += info[group[i].nodes[0]].day2;
			}
		}else{

			if(num_in[i] == 0){
				min_index = 0;
				minimum = info[group[i].nodes[0]].day1 -info[group[i].nodes[0]].day2;

				for(int k = 1; k < group[i].nodes.size(); k++){
					if(info[group[i].nodes[k]].day1 -info[group[i].nodes[k]].day2 < minimum){
						minimum = info[group[i].nodes[k]].day1 -info[group[i].nodes[k]].day2;
						min_index = k;
					}
				}

				ans += info[group[i].nodes[min_index]].day1;
				for(int k = 0; k < group[i].nodes.size(); k++){
					if(k != min_index){
						ans += info[group[i].nodes[k]].day2;
					}
				}

			}else{
				for(int k = 0; k < group[i].nodes.size(); k++){
					ans += info[group[i].nodes[k]].day2;
				}
			}
		}
	}

	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 3556