#include <bits/stdc++.h>
using namespace std;
long long g[300][300], n;
long long ans = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> g[i][j], ans += g[i][j];
  int k;
  cin >> k;
  ans /= 2;
  while (k--) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    if (g[a][b] < c) {
      cout << ans << " ";
      continue;
    }
    ans += c - g[a][b];
    g[a][b] = g[b][a] = c;
    int p = a;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (g[i][p] + g[p][j] < g[i][j]) {
          ans += g[i][p] + g[p][j] - g[i][j];
          g[i][j] = g[i][p] + g[p][j];
          g[j][i] = g[i][j];
        }
      }
    }
    p = b;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (g[i][p] + g[p][j] < g[i][j]) {
          ans += g[i][p] + g[p][j] - g[i][j];
          g[i][j] = g[i][p] + g[p][j];
          g[j][i] = g[i][j];
        }
      }
    }
    cout << ans << " ";
  }
  return 0;
}
