#include <bits/stdc++.h>
int dist[305][305];
int main() {
  int n, k;
  long long sum = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &dist[i][j]);
      if (i > j) sum += dist[i][j];
    }
  }
  scanf("%d", &k);
  for (int i = 1, u, v, w; i <= k; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    if (w < dist[u][v]) {
      sum -= dist[u][v] - w;
      dist[u][v] = dist[v][u] = w;
      for (int j = 1; j <= n; ++j) {
        for (int k = 1; k <= n; ++k) {
          int cost = dist[j][u] + dist[u][v] + dist[v][k];
          if (cost < dist[j][k]) {
            sum -= dist[j][k] - cost;
            dist[j][k] = dist[k][j] = cost;
          }
        }
      }
    }
    printf("%I64d ", sum);
  }
  return 0;
}
