#include <bits/stdc++.h>
using namespace std;
int n, d[300][300], K;
void run() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> d[i][j];
      d[j][i] = d[i][j];
    }
  cin >> K;
  for (int it = 0; it < K; it++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    d[a][b] = d[b][a] = min(d[a][b], c);
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      d[i][a] = d[a][i] = min(d[i][a], d[i][b] + c);
      d[i][b] = d[b][i] = min(d[i][b], d[i][b] + c);
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] =
            min(d[i][j], min(d[i][a] + c + d[b][j], d[i][b] + c + d[a][j]));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < i; j++) sum += d[i][j];
    cout << sum << (it == K - 1 ? "\n" : " ");
  }
}
int main() {
  run();
  return 0;
}
