#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000;
int father[MAXN];
int height[MAXN];
struct Edge{int from;int to;int length;
};
Edge edge[MAXN*MAXN];
void init(int n){for(int i=0;i<=n;i++){father[i]=i;height[i]=0;}
}
bool bmp(Edge a,Edge b){if(a.length!=b.length){return a.length<b.length;}
}
int find(int x){if(x!=father[x]){x = find(father[x]);}return x;
}
void union1(int x,int y){int x1=find(x);int y1=find(y);if(x1!=y1){if(height[x1]<height[y1]){father[x1]=y1;}else if(height[x1]>height[y1]){father[y1]=x1;}else if(height[x1]==height[y1]){father[x1]=y1;height[y1]++;}}
}
int Kruscal(int n,int edgenum){init(n);int result=0;sort(edge,edge+edgenum,bmp);for(int i=0;i<edgenum;i++){Edge current=edge[i];if(find(current.from)!=find(current.to)){union1(current.from,current.to);result+=current.length;}}return result;
}
int main(){int n,edgenum;while(cin>>n){edgenum=n*(n-1)/2;if(n==0)break;for(int i=0;i<edgenum;i++){cin>>edge[i].from>>edge[i].to>>edge[i].length;}int result = Kruscal(n,edgenum);cout<<result<<endl;}return 0;
}