/*简单的最小生成树*/
#include<cstdio>
#include<cstring>#define INF 1<<30
int map[1001][1001],vis[1001],low[1001];
int n,m;
int prim()
{
int ans=0;
for(int i = 1; i <= n; i++)
{
vis[i] = 0;
low[i] = map[1][i];
}
vis[1] = 1;
for(int i = 1; i <= n; i++)
{
int temp = INF,pos=-1;
for(int j = 1; j <= n; j++)
if(!vis[j]&&temp>low[j])
{
temp = low[j];
pos = j;
}
if(pos==-1) continue;
int k = pos;
vis[k] = 1;
ans+=low[k];
for(int j = 1; j <= n; j++)
if(!vis[j]&&low[j]>map[k][j])
low[j] = map[k][j];
}
return ans;
}
int main()
{
int a,b,c;
while(scanf("%d",&n),n)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
map[i][j] = INF;
for(int i = 0; i < n*(n-1)/2; i++)
{
scanf("%d %d %d",&a,&b,&c);
map[a][b] = map[b][a] = c;
}
printf("%d\n",prim());
}
return 0;
}