#include<stdio.h>
#include<string.h>
#define N 30
#define inf 999999999
int map[N][N],next[N],visit[N];
int main() {
int a,b,i,j,cnt,sum,t,n,m;
char ch,c;
while(scanf("%d",&n),n) {
for(i=1;i<=n;i++) {
visit[i]=1;
for(j=1;j<=n;j++)
map[i][j]=inf;
}
m=n-1;
while(m--) {
getchar();
scanf("%c",&ch);
scanf("%d",&t);
while(t--) {
scanf(" %c%d",&c,&a);
map[ch-'A'+1][c-'A'+1]=map[c-'A'+1][ch-'A'+1]=a;
}
}
cnt=1;
visit[1]=0;
next[0]=1;
sum=0;
while(cnt!=n) {
b=inf;
for(i=0;i<cnt;i++)
for(j=1;j<=n;j++)
if(visit[j]&&map[next[i]][j]<b) {
next[cnt]=j;
b=map[next[i]][j];
}
visit[next[cnt]]=0;
sum+=b;
cnt++;
}
printf("%d
",sum);
}
return 0;
}