#include <iostream>
#include <cstring>
using namespace std;
const int N = 510;
int n,g[N][N],dist[N],p[N];
bool st[N];
int prim()
{
memset(dist,0x3f,sizeof dist);
memset(st,0,sizeof st);
dist[1]=0;
int res=0;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
if(!st[j]&&(t==-1||dist[t]>dist[j]))
t=j;
res+=dist[t];
st[t]=true;
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],g[t][j]);
}
return res;
}
int main()
{
int T;cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++) cin>>p[i];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>g[i][j];
g[i][j]+=p[i]+p[j];
}
cout<<prim()<<endl;
}
return 0;
}