#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, k, a[305][305], b, c, d, ij, isum = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  cin >> k;
  for (i = 1; i <= k; i++) {
    isum = 0;
    cin >> b >> c >> d;
    a[b][c] = min(a[b][c], d);
    a[c][b] = min(a[c][b], d);
    for (j = 1; j <= n; j++) {
      for (ij = 1; ij <= n; ij++) {
        if (j != ij) {
          a[j][ij] = min(a[j][ij], min(a[j][b] + a[b][c] + a[c][ij],
                                       a[j][c] + a[b][c] + a[b][ij]));
          isum += a[j][ij];
        }
      }
    }
    cout << isum / 2 << " ";
  }
}
