#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const long long maxn=333;
long long map[maxn][maxn];
int main()
{
long long n,k;
while(scanf("%I64d",&n)!=EOF)
{
int i,j;
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%I64d",&map[i][j]);
}
}
scanf("%I64d",&k);
long long a,b,c,sum,p;
long long vis[maxn];//这边开成long long 结果看了数据还是213了一回。。
for(p=1;p<=k;p++)
{
scanf("%I64d%I64d%I64d",&a,&b,&c);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
map[i][j]=min(map[i][j],map[i][a]+map[b][j]+c);
map[i][j]=min(map[i][j],map[i][b]+map[a][j]+c);
}
}
sum=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
sum+=map[i][j];
}
}
vis[p]=(sum/2);
}
for(i=1;i<=k;i++)
{
if(i>1)
cout<<" ";
cout<<vis[i];
}
cout<<endl;
}
return 0;
}