#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long mat[n + 1][n + 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> mat[i][j];
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    if (mat[a][b] > c) {
      mat[a][b] = c;
      mat[b][a] = c;
    }
    for (int j = 1; j <= n; j++) {
      for (int h = 1; h <= n; h++) {
        if (mat[j][a] + c + mat[b][h] < mat[j][h] && j != h) {
          mat[j][h] = mat[j][a] + c + mat[b][h];
          mat[h][j] = mat[j][a] + c + mat[b][h];
        }
      }
    }
    long long suma = 0;
    for (int j = 1; j <= n; j++)
      for (int h = 1; h <= n; h++) suma += mat[j][h];
    cout << suma / 2 << '\n';
  }
  return 0;
}
