#include <bits/stdc++.h>
using namespace std;
int n, d[333][333], k;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> d[i][j];
    }
  }
  cin >> k;
  while (k--) {
    int a = 0, b = 0, c = 0;
    cin >> a >> b >> c;
    d[a][b] = min(d[a][b], c);
    d[b][a] = min(d[b][a], c);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        d[i][j] = min(d[i][j], d[a][j] + d[i][b] + d[a][b]);
        d[i][j] = min(d[i][j], d[b][j] + d[i][a] + d[b][a]);
      }
    }
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        sum += d[i][j];
      }
    }
    cout << sum / 2 << endl;
  }
  return 0;
}
