#include <bits/stdc++.h>
using namespace std;
long long arr[301][301];
int main() {
  long long n, m, a, b, d;
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> arr[i][j];
      if (j > i) {
        sum = sum + arr[i][j];
      }
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> d;
    for (int p = 1; p <= n; p++) {
      for (int q = 1; q <= n; q++) {
        arr[p][q] = min(arr[p][q], min((arr[p][a] + arr[b][q] + d),
                                       (arr[p][b] + arr[a][q] + d)));
      }
    }
    long long ans = 0;
    for (int p = 1; p <= n; p++) {
      for (int q = p + 1; q <= n; q++) {
        ans = ans + arr[p][q];
      }
    }
    cout << ans << " ";
  }
  return 0;
}
