#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
#define maxn 1005
int f[maxn], val[maxn];
struct node
{
int u, v, len;
friend bool operator < (node a, node b)
{
return a.len > b.len;
}
};
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int N, u, v;
node s;
priority_queue<node> Q;
scanf("%d", &N);
for(int i=1; i<=N; i++)
{
scanf("%d", &val[i]);
f[i] = i;
}
for(s.u=1; s.u<=N; s.u++)
for(s.v=1; s.v<=N; s.v++)
{
scanf("%d", &s.len);
s.len += val[s.u]+val[s.v];
Q.push(s);
}
int ans = 0;
while(Q.size())
{
s = Q.top();Q.pop();
u = Find(s.u), v = Find(s.v);
if(u != v)
{
f[u] = v;
ans += s.len;
}
}
printf("%d\n", ans);
}
return 0;
}