#include <bits/stdc++.h>
using namespace std;
int mat[305][305];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> mat[i][j];
      sum += mat[i][j];
    }
  int q;
  cin >> q;
  for (int tk = 0; tk < q; tk++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    sum -= mat[a][b];
    mat[a][b] = min(mat[a][b], c);
    sum += mat[a][b];
    sum -= mat[b][a];
    mat[b][a] = min(mat[b][a], c);
    sum += mat[b][a];
    for (int i = 0; i < n; i++) {
      sum -= mat[i][b];
      mat[i][b] = min(mat[i][b], mat[i][a] + mat[a][b]);
      sum += mat[i][b];
      sum -= mat[i][a];
      mat[i][a] = min(mat[i][a], mat[i][b] + mat[b][a]);
      sum += mat[i][a];
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        sum -= mat[i][j];
        mat[i][j] = min(mat[i][j], mat[i][a] + mat[a][j]);
        sum += mat[i][j];
        sum -= mat[i][j];
        mat[i][j] = min(mat[i][j], mat[i][b] + mat[b][j]);
        sum += mat[i][j];
      }
    cout << sum / 2 << " ";
  }
  cout << endl;
}
