#include <bits/stdc++.h>
using namespace std;
int n, q;
long long d[311][311];
int main() {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) d[i][j] = 1e15;
  }
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) scanf("%lld", &d[i][j]);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      for (int k = 1; k <= n; ++k) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  scanf("%d", &q);
  while (q--) {
    int u, v;
    long long w;
    scanf("%d %d %lld", &u, &v, &w);
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        d[i][j] = min(d[i][j], d[i][u] + w + d[v][j]);
        d[i][j] = min(d[i][j], d[i][v] + w + d[u][j]);
      }
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = i + 1; j <= n; ++j) {
        if (i != j) ans += d[i][j];
      }
    }
    printf("%lld ", ans);
  }
}
