#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 30;
const int maxm = 500;
struct Edge{
int v1, v2;
int weight;
Edge(int _v1 = 0 , int _v2 = 0, int _weight = 0 ):v1(_v1),v2(_v2),weight(_weight){}
};
int n, ans, cnt;
int parent[maxn];
int Rank[maxn];
Edge E[maxm];
bool cmp(const Edge& lhs, const Edge& rhs ){
return lhs.weight < rhs.weight;
}
void Init(){
cnt = 0;
ans = 0;
for(int i = 0; i <= n ; i++){
parent[i] = i;
Rank[i] = 1;
}
}
int findRoot(int x){
int tempRoot;
int root = x;
while(root != parent[root])
root = parent[root];
while(x != root){
tempRoot = parent[x];
parent[x] = root;
x = tempRoot;
}
return root;
}
void Union(int x, int y){
int xRoot = findRoot(x);
int yRoot = findRoot(y);
if(xRoot == yRoot)
return ;
if(Rank[xRoot] < Rank[yRoot])
parent[xRoot] = yRoot;
else if(Rank[xRoot] > Rank[yRoot])
parent[yRoot] = xRoot;
else {
parent[xRoot] = parent[yRoot];
Rank[yRoot] ++;
}
}
void Kruskal(){
int edgeNum = 0;
for(int i = 0; i < cnt&& edgeNum != n - 1; ++ i){
if(findRoot(E[i].v1) == findRoot(E[i].v2))
continue;
else {
Union(E[i].v1,E[i].v2);
ans += E[i].weight;
edgeNum++;
}
}
}
int main(){
char v1, v2;
int weight, m;
while(cin>>n&&n){
Init();
n=n-1;
while(n--){
cin>>v1>>m;
while(m--){
cin>>v2>>weight;
E[cnt++] = Edge(v1-'A', v2-'A', weight);
}
}
sort(E,E+cnt,cmp);
Kruskal();
printf("%d\n",ans);
}
return 0;
}
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 30;
const int maxm = 500;
struct Edge{
int v1, v2;
int weight;
Edge(int _v1 = 0 , int _v2 = 0, int _weight = 0 ):v1(_v1),v2(_v2),weight(_weight){}
};
int n, ans, cnt;
int parent[maxn];
int Rank[maxn];
Edge E[maxm];
bool cmp(const Edge& lhs, const Edge& rhs ){
return lhs.weight < rhs.weight;
}
void Init(){
cnt = 0;
ans = 0;
for(int i = 0; i <= n ; i++){
parent[i] = i;
Rank[i] = 1;
}
}
int findRoot(int x){
int tempRoot;
int root = x;
while(root != parent[root])
root = parent[root];
while(x != root){
tempRoot = parent[x];
parent[x] = root;
x = tempRoot;
}
return root;
}
void Union(int x, int y){
int xRoot = findRoot(x);
int yRoot = findRoot(y);
if(xRoot == yRoot)
return ;
if(Rank[xRoot] < Rank[yRoot])
parent[xRoot] = yRoot;
else if(Rank[xRoot] > Rank[yRoot])
parent[yRoot] = xRoot;
else {
parent[xRoot] = parent[yRoot];
Rank[yRoot] ++;
}
}
void Kruskal(){
int edgeNum = 0;
for(int i = 0; i < cnt&& edgeNum != n - 1; ++ i){
if(findRoot(E[i].v1) == findRoot(E[i].v2))
continue;
else {
Union(E[i].v1,E[i].v2);
ans += E[i].weight;
edgeNum++;
}
}
}
int main(){
char v1, v2;
int weight, m;
while(cin>>n&&n){
Init();
n=n-1;
while(n--){
cin>>v1>>m;
while(m--){
cin>>v2>>weight;
E[cnt++] = Edge(v1-'A', v2-'A', weight);
}
}
sort(E,E+cnt,cmp);
Kruskal();
printf("%d\n",ans);
}
return 0;
}