传送门
#include <iostream>
#include <cstring>
using namespace std;
const int N = 27, INF = 0x3f3f3f3f;
int dist[N], vis[N], g[N][N];
int n;
int prim(){
memset(dist, INF, sizeof dist);
memset(vis, 0, sizeof vis);
int res = 0;
for(int i = 0; i < n; i ++){
int t = -1;
for(int j = 1; j <= n; j ++)
if(!vis[j] && (t == -1 || dist[t] > dist[j]))
t = j;
if(i) res += dist[t];
for(int j = 1; j <= n; j ++)
dist[j] = min(dist[j], g[t][j]);
vis[t] = true;
//cout << "i = " << i << " res = " << res << endl;
}
return res;
}
int main(){
while(cin >> n){
if(!n)
break;
char op;
memset(g, INF, sizeof g);
for(int j = 1; j < n; j ++){
int k;
cin >> op >> k;
for(int i = 1; i <= k; i ++){
int w;
char c;
cin >> c >> w;
g[op - 'A' + 1][c - 'A' + 1] = g[c - 'A' + 1][op - 'A' + 1] = min(g[op - 'A' + 1][c - 'A' + 1], w);
}
}
cout << prim() << endl;
}
return 0;
}