/*
分析:
最小生成树果题。
2012-07-30 14:17
*/
分析:
最小生成树果题。
2012-07-30 14:17
*/
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
int n;
struct A
{
int a,b;
int len;
}eage[1111];
int K;
int cmp(const void *a,const void *b)
{
struct A *c,*d;
c=(struct A *)a;
d=(struct A *)b;
return c->len-d->len;
}
int Prim()
{
int ans;
int i;
int hash[111];
memset(hash,0,sizeof(hash));
ans=0;
hash[eage[0].a]=1;
for(i=0;i<K;i++)
{
if(hash[eage[i].a]+hash[eage[i].b]==1)
{
hash[eage[i].a]=hash[eage[i].b]=1;
ans+=eage[i].len;
i=-1;
}
}
return ans;
}
int main()
{
int i;
int m;
int a,b,c;
char str[5];
while(scanf("%d",&n),n)
{
K=0;
for(i=1;i<n;i++)
{
scanf("%s",str);
a=str[0]-'A';
scanf("%d",&m);
while(m--)
{
scanf("%s%d",str,&c);
b=str[0]-'A';
eage[K].a=a;
eage[K].b=b;
eage[K++].len=c;
}
}
qsort(eage,K,sizeof(eage[0]),cmp);
printf("%d\n",Prim());
}
return 0;
}