#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 1000;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef map<string,int> msi;

struct Tool {
  string name, sup;
  int day1, day2, diff, i, si;

  void print() {
    printf("Tool: %s(%d):%d, %s(%d):%d\n",
	   name.c_str(), i, day1, sup.c_str(), si, day2);
  }
};

/* global variables */

int n;
Tool tools[MAX_N];
msi nhash;

bool used[MAX_N], tmpused[MAX_N];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0) break;

    nhash.clear();
    for (int i = 0; i < n; i++) {
      Tool& ti = tools[i];
      cin >> ti.name >> ti.day1 >> ti.sup >> ti.day2;
      ti.diff = ti.day1 - ti.day2;
      ti.i = i;
      ti.si = -1;
      nhash[ti.name] = i;
    }
    for (int i = 0; i < n; i++) {
      Tool& ti = tools[i];
      ti.si = nhash[ti.sup];
      //ti.print();
    }

    memset(used, false, sizeof(used));

    int minsum = 0;
    
    // find loops
    for (int i = 0; i < n; i++) {
      Tool& ti = tools[i];
      if (! used[i]) {
	memset(tmpused, false, sizeof(tmpused));
	tmpused[i] = true;
	int j = ti.si;
	for (; ! tmpused[j]; j = tools[j].si) {
	  if (used[j]) break;
	  tmpused[j] = true;
	}
	if (used[j]) continue;

	int sumloop = tools[j].day2;
	int mindiff = tools[j].diff;

	used[j] = true;
	for (int k = tools[j].si; k != j; k = tools[k].si) {
	  used[k] = true;
	  sumloop += tools[k].day2;
	  if (mindiff > tools[k].diff) mindiff = tools[k].diff;
	}

	minsum += sumloop + mindiff;
      }
    }
    //cout << ln << endl;

    // check leafs
    bool changed = true;
    while (changed) {
      changed = false;

      for (int i = 0; i < n; i++) {
	Tool& ti = tools[i];
	if (! used[i] && used[ti.si]) {
	  used[i] = true;
	  minsum += ti.day2;
	  changed = true;
	}
      }
    }
    
    cout << minsum << endl;
  }

  return 0;
}