#include<bits/stdc++.h>
using namespace std;
bool mat[900][900];
int par[100];
struct data
{
int a,b,cost;
} arr[900];
int khoj_rep(int r)
{
if(par[r]==r)
{
return r;
}
else
{
return par[r]=khoj_rep(par[r]);
}
}
bool cmp(data lhs,data rhs)
{
if(lhs.cost==rhs.cost)
{
return lhs.a<rhs.a;
}
else
{
return lhs.cost<rhs.cost;
}
}
int main()
{
int test,koyta_city,i,j,k,x,cost,u,v,counter;
cin>>test;
for(x=1; x<=test; x++)
{
cin>>koyta_city;
for(i=65; i<=koyta_city+64; i++)
{
par[i]=i;
}
k=-1;
for(i=1; i<=koyta_city; i++)
{
for(j=1; j<=koyta_city; j++)
{
if(j>=1 && j<koyta_city)
{
scanf("%d, ",&cost);
}
else if(j==koyta_city)
{
scanf("%d",&cost);
}
if(cost>0 && mat[i][j]==0)
{
arr[++k].a=i+64;
arr[k].b=j+64;
arr[k].cost=cost;
mat[j][i]=1;
}
}
}
sort(arr,arr+k+1,cmp);
counter=0;
printf("Case %d:\n",x);
for(i=0; i<k; i++)
{
u=khoj_rep(arr[i].a);
v=khoj_rep(arr[i].b);
if(u!=v)
{
par[u]=v;
printf("%c-%c %d\n",arr[i].a,arr[i].b,arr[i].cost);
counter++;
if(counter==koyta_city-1)
{
break;
}
}
}
memset(par,0,sizeof(par));
memset(mat,0,sizeof(mat));
}
return 0;
}