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