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