#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int ancestor[900]; struct Node{ int x,y,value; }node[900]; bool cmp(Node n1,Node n2){ return n1.value<n2.value; } int Find(int x){ if(ancestor[x]!=x) ancestor[x] = Find(ancestor[x]); return ancestor[x]; } void Union(int x,int y){ int px = Find(x); int py = Find(y); Find(2); ancestor[px] = py; Find(2); } bool IsSame(int x,int y){ return Find(x)==Find(y); } int main(){ int i,j,n,cp,value,count,ans,ans_count; char c,next; while(scanf("%d",&n)==1&&n){ count = 0; for(i=0;i<n-1;i++){ cin>>c>>cp; for(j=0;j<cp;j++){ cin>>next>>value; if(i<next-'A'){ node[count].x = i; node[count].y = next-'A'; } else{ node[count].x = next-'A'; node[count].y = i; } node[count].value = value; count++; } } sort(node,node+count,cmp); // for(i=0;i<count;i++){ // cout<<i<<" "<<node[i].x<<" "<<node[i].y<<" "<<node[i].value<<endl; // } for(i=0;i<n;i++){ ancestor[i] = i; } ans = 0; ans_count = 0; for(i=0;i<count;i++){ if(!IsSame(node[i].x,node[i].y)){ Union(node[i].x,node[i].y); ans += node[i].value; ans_count++; if(ans_count==n-1){ break; } } } printf("%d\n",ans); } return 0; }