#include <bits/stdc++.h>
using namespace std;
long long n, d[500][500], dist[500][500];
long long t, a, c, b;
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> d[i][j];
      dist[i][j] = d[i][j];
    }
  }
  long long tong;
  cin >> t;
  while (t--) {
    cin >> a >> b >> c;
    tong = 0;
    dist[a][b] = dist[b][a] = min(dist[a][b], min(dist[b][a], c));
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], min(dist[i][a] + c + dist[b][j],
                                         dist[i][b] + c + dist[a][j]));
      }
    }
    for (long long i = 1; i <= n; i++) {
      for (long long j = i + 1; j <= n; j++) {
        tong += dist[i][j];
      }
    }
    cout << tong << " ";
  }
}
