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