#include<cstdio>
#include<climits>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
struct S{int v1,v2,t;}s,t={0,0,-1};
const int N=1000;
int main()
{
	int n,i,j,k,r;
	char x[64],y[64];
	while(scanf("%d",&n),n)
	{
		map<string,int>m;
		vector<S>v;
		bool f[N]={0};
		for(i=0;i<n;++i)
		{
			scanf("%s%d%s%d",x,&s.v1,y,&s.v2);
			if(m.find(y)==m.end())m[y]=v.size(),v.push_back(t);
			s.t=m[y];if(m.find(x)==m.end())m[x]=v.size(),v.push_back(s);
			else v[m[x]]=s;
		}
		for(r=i=0;i<v.size();++i)
		{
			if(f[i])continue;
			if(i==v[i].t)r+=v[i].v1;
			else
			{
				bool c[N]={0};
				vector<int>u;
				for(j=i;!f[i]&&!c[j];j=v[j].t)c[j]=1,u.push_back(j);
				if(i==j)
				{
					int a=INT_MAX,b;
					for(j=0;j<u.size();++j)
					{
						b=v[u[0]].v1;for(k=1;k<u.size();++k)b+=v[u[k]].v2;a=min(a,b);
						f[u[0]]=1;		rotate(u.begin(),u.begin()+1,u.end());}
					r+=a;
				}
				else r+=v[i].v2;
			}
			f[i]=1;
		}
		printf("%d\n",r);
	}
	return 0;
}