#include <bits/stdc++.h>
using namespace std;
const int maxn = 500;
int dist[maxn][maxn];
long long ans;
void modify(int u, int v, int w) {
  if (dist[u][v] > w) {
    ans -= (dist[v][u] - w);
    dist[u][v] = w;
  }
  return;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> dist[i][j];
      if (j > i) {
        ans += dist[i][j];
      }
    }
  }
  int k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    modify(u, v, w);
    if (dist[u][v] != dist[v][u]) {
      dist[v][u] = dist[u][v];
    }
    for (int p = 1; p <= n; p++) {
      for (int q = 1; q <= n; q++) {
        modify(p, q, dist[p][u] + dist[v][q] + w);
        if (dist[p][q] != dist[q][p]) {
          dist[q][p] = dist[p][u] + dist[v][q] + w;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
