#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
  scanf("%d", &n);
  int AdjMat[n + 1][n + 1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &AdjMat[i][j]);
    }
  }
  scanf("%d", &m);
  for (int a = 0; a < m; a++) {
    int v1, v2, d;
    scanf("%d %d %d", &v1, &v2, &d);
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int newRoad = AdjMat[i][(v1 - 1)] + AdjMat[(v2 - 1)][j] + d;
        int newRoad2 = AdjMat[i][(v2 - 1)] + AdjMat[(v1 - 1)][j] + d;
        AdjMat[i][j] = min(AdjMat[i][j], min(newRoad, newRoad2));
        sum += AdjMat[i][j];
      }
    }
    cout << sum / 2;
    if (a != m - 1) {
      printf(" ");
    }
  }
  return 0;
}
