#include <bits/stdc++.h>
using namespace std;
int main() {
  int v;
  cin >> v;
  int d[v][v];
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < v; j++) cin >> d[i][j];
  }
  int n;
  cin >> n;
  for (int k = 0; k < n; k++) {
    int x, y, w;
    cin >> x >> y >> w;
    x--;
    y--;
    long long int ans = 0;
    for (int i = 0; i < v - 1; i++) {
      for (int j = i + 1; j < v; j++) {
        d[i][j] =
            min(d[i][j], min(d[i][x] + w + d[y][j], d[i][y] + w + d[x][j]));
        d[j][i] = d[i][j];
        ans += d[i][j];
      }
    }
    cout << ans << " ";
  }
  return 0;
}
