#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
struct edge{
int f,t,w;
};
edge e[500];
bool cmp(edge x,edge y){
return x.w<y.w;
}
int t,tot,p[30];
int find(int x){
return p[x]==x?x:p[x]=find(p[x]);
}
int kruskal(){
int ans=0;
for(int i=0;i<t;i++)
p[i]=i;
sort(e,e+tot,cmp);
for(int i=0;i<tot;i++){
int x=find(e[i].f);
int y=find(e[i].t);
if(x!=y){
p[x]=y;
ans+=e[i].w;
}
}
return ans;
}
int main()
{
int n,d;
char c,cc;
while(cin>>t,t){
tot=0;
for(int i=0;i<t-1;i++){
scanf(" %c %d",&c,&n);
for(int j=0;j<n;j++){
scanf(" %c %d",&cc,&d);
e[tot].f=c-'A';
e[tot].t=cc-'A';
e[tot].w=d;
tot++;
}
}
printf("%d\n",kruskal());
}
return 0;
}