#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, k, l, n, q, x, y, dist, final_dist = 0;
  cin >> n;
  long long d[n][n];
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      cin >> l;
      d[i][j] = l;
      final_dist += l;
    }
  cin >> l;
  for (q = 0; q < l; q++) {
    cin >> x >> y >> dist;
    if (d[x - 1][y - 1] > dist) {
      d[x - 1][y - 1] = dist;
      d[y - 1][x - 1] = dist;
    } else
      goto anuj;
    i = x - 1;
    for (j = 0; j < n; j++) {
      d[i][j] = min(d[i][j], d[i][x - 1] + d[x - 1][y - 1] + d[y - 1][j]);
      d[j][i] = d[i][j];
    }
    j = y - 1;
    for (i = 0; i < n; i++) {
      d[i][j] = min(d[i][j], d[i][x - 1] + d[x - 1][y - 1] + d[y - 1][j]);
      d[j][i] = d[i][j];
    }
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][x - 1] + d[x - 1][y - 1] + d[y - 1][j]);
        d[j][i] = d[i][j];
      }
    final_dist = 0;
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++) final_dist += d[i][j];
  anuj:;
    cout << final_dist / 2 << " ";
  }
}
