//Jungle Roads
#include<iostream>
#include<algorithm>
using namespace std;
int Tree[1000];
int findRoot(int x){
if(Tree[x]==-1) return -1;
else{
int tmp=findRoot(Tree[x]);
Tree[x]=tmp;
return tmp;
}
}
struct Edge{
int a,b;
int cost;
bool operator<(const Edge&A)const{
return cost<A.cost;
}
}edge[4000];
int main(){
int n;
char a;int m;
char s[100];int d[100];
while(cin>>n&&n!=0){
int size=0;
for(int i=0;i<n-1;i++){
cin>>a>>m;
for(int j=0;j<m;j++){
cin>>s[j]>>d[j];
edge[size].a=a-'A';
edge[size].b=s[j]-'A';
edge[size].cost=d[j];
size++;
}
}
sort(edge,edge+size);
int ans=0;
for(int i=0;i<size;i++) Tree[i]=-1;
for(int i=0;i<size;i++){
int a=findRoot(edge[i].a);
int b=findRoot(edge[i].b);
cout<<a<<endl;
cout<<b<<endl;
if(a!=b) {
Tree[a]=b;
ans+=edge[i].cost;
}
}
cout<<ans<<endl;
}
}
#include<iostream>
#include<algorithm>
using namespace std;
int Tree[1000];
int findRoot(int x){
if(Tree[x]==-1) return -1;
else{
int tmp=findRoot(Tree[x]);
Tree[x]=tmp;
return tmp;
}
}
struct Edge{
int a,b;
int cost;
bool operator<(const Edge&A)const{
return cost<A.cost;
}
}edge[4000];
int main(){
int n;
char a;int m;
char s[100];int d[100];
while(cin>>n&&n!=0){
int size=0;
for(int i=0;i<n-1;i++){
cin>>a>>m;
for(int j=0;j<m;j++){
cin>>s[j]>>d[j];
edge[size].a=a-'A';
edge[size].b=s[j]-'A';
edge[size].cost=d[j];
size++;
}
}
sort(edge,edge+size);
int ans=0;
for(int i=0;i<size;i++) Tree[i]=-1;
for(int i=0;i<size;i++){
int a=findRoot(edge[i].a);
int b=findRoot(edge[i].b);
cout<<a<<endl;
cout<<b<<endl;
if(a!=b) {
Tree[a]=b;
ans+=edge[i].cost;
}
}
cout<<ans<<endl;
}
}