2017.12.23
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX 10000
int pre[MAX];
int con[MAX];
void init(int n){
for(int i = 1; i <= n; i++)
pre[i] = i;
}
int find(int x){
int root = x;
while(root != pre[root])
root = pre[root];
while(x != root){
int t = pre[x];
pre[x] = root;
x = t;
}
return root;
}
bool join(int x, int y){
int xx = find(x);
int yy = find(y);
if(xx != yy){
pre[xx] = yy;
return 1;
}
return 0;
}
struct node{
int u;
int v;
int path;
};
node Edge[MAX];
bool cmp(node a, node b){
return a.path < b.path;
}
int main(){
int n;
int num;
int cnt, cost;
while(cin >> n && n){
init(n);
num = 0;
char u, v;
int path;
for(int i = 1; i < n; i++){
cin >> u >> cnt;
for(int j = 0; j < cnt; j++){
cin >> v >> cost;
Edge[num].u = int(u-64);
Edge[num].v = int(v-64);
Edge[num].path = cost;
num++;
}
}
sort(Edge, Edge+num, cmp);
int len = 0;
for(int i = 0; i < num; i++){
if(join(Edge[i].u, Edge[i].v)){
len += Edge[i].path;
}
}
cout << len << endl;
}
return 0;
}