#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9 + 10;
const double eps = 1e-9;
const int maxn = (int)500;
int a[maxn][maxn];
int main() {
  int n;
  scanf("%d\n", &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
  long long s = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < i; ++j) s += a[i][j];
  int k;
  scanf("%d", &k);
  for (int i = 0; i < k; ++i) {
    int l, r, c;
    scanf("%d %d %d", &l, &r, &c);
    for (int q = 1; q <= n; ++q)
      for (int p = 1; p <= n; ++p)
        a[p][q] =
            min(min(a[p][l] + c + a[r][q], a[p][r] + c + a[l][q]), a[p][q]);
    s = 0;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j < i; ++j) s += a[i][j];
    printf("%I64d\n", s);
  }
}
