1 #include<iostream>
2 #include<algorithm>
3 using namespace std;
4
5 const int maxn = 10005;
6 int n, m;
7 int fa[28];
8 struct node
9 {
10 int x, y;
11 int cost;
12 }arr[maxn];
13
14 void init()
15 {
16 for (int i = 0; i <= maxn; i++)
17 {
18 fa[i] = i;
19 }
20 }
21
22 int find(int x)
23 {
24 if (x != fa[x])
25 {
26 return find(fa[x]);
27 }
28 else
29 return fa[x];
30 }
31
32 bool cmp(node a, node b)
33 {
34 return a.cost<b.cost;
35 }
36
37 int main()
38 {
39 char c1, c2;
40 int k, c;
41 while (cin >> n)
42 {
43 if (n == 0) break;
44 int j = 0; //表示边的数量
45 init();
46 for (int i = 1; i<n; i++)
47 {
48 cin >> c1 >> k;
49 while (k--)
50 {
51 cin >> c2 >> c;
52 arr[j].x = c1 - 'A';
53 arr[j].y = c2 - 'A';
54 arr[j].cost = c;
55 j++;
56 }
57 }
58 sort(arr, arr + j, cmp); //排序
59 int ans = 0;
60 for (int i = 0; i<j; i++)
61 {
62 int fx, fy;
63 fx = find(arr[i].x);
64 fy = find(arr[i].y);
65 if (fx != fy)
66 {
67 ans += arr[i].cost;
68 fa[fy] = fx;
69 }
70 }
71 cout << ans << endl;
72 }
73 return 0;
74 }